99import javax .ws .rs .core .HttpHeaders ;
1010import javax .ws .rs .core .MediaType ;
1111
12+ import java .io .UnsupportedEncodingException ;
13+ import java .nio .charset .StandardCharsets ;
1214import java .time .Instant ;
1315import java .time .ZonedDateTime ;
1416import java .util .Collections ;
1517import java .util .List ;
18+ import java .util .Locale ;
1619import java .util .Map ;
1720
1821import static org .junit .Assert .*;
@@ -29,11 +32,6 @@ public class AwsProxyHttpServletRequestTest {
2932 private static final String REFERER = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox" ;
3033 private static ZonedDateTime REQUEST_DATE = ZonedDateTime .now ();
3134
32- private static final AwsProxyRequest REQUEST_WITH_HEADERS = new AwsProxyRequestBuilder ("/hello" , "GET" )
33- .header (CUSTOM_HEADER_KEY , CUSTOM_HEADER_VALUE )
34- .header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON )
35- .header (AwsProxyHttpServletRequest .CF_PROTOCOL_HEADER_NAME , REQUEST_SCHEME_HTTP )
36- .build ();
3735 private static final AwsProxyRequest REQUEST_FORM_URLENCODED = new AwsProxyRequestBuilder ("/hello" , "POST" )
3836 .form (FORM_PARAM_NAME , FORM_PARAM_NAME_VALUE ).build ();
3937 private static final AwsProxyRequest REQUEST_INVALID_FORM_URLENCODED = new AwsProxyRequestBuilder ("/hello" , "GET" )
@@ -54,10 +52,11 @@ public class AwsProxyHttpServletRequestTest {
5452 private static final AwsProxyRequest REQUEST_USER_AGENT_REFERER = new AwsProxyRequestBuilder ("/hello" , "POST" )
5553 .userAgent (USER_AGENT )
5654 .referer (REFERER ).build ();
57-
5855 private static final AwsProxyRequest REQUEST_WITH_DATE = new AwsProxyRequestBuilder ("/hello" , "GET" )
5956 .header (HttpHeaders .DATE , AwsHttpServletRequest .dateFormatter .format (REQUEST_DATE ))
6057 .build ();
58+ private static final AwsProxyRequest REQUEST_WITH_LOWERCASE_HEADER = new AwsProxyRequestBuilder ("/hello" , "POST" )
59+ .header (HttpHeaders .CONTENT_TYPE .toLowerCase (Locale .getDefault ()), MediaType .APPLICATION_JSON ).build ();
6160
6261 private static final AwsProxyRequest REQUEST_NULL_QUERY_STRING ;
6362 static {
@@ -72,7 +71,7 @@ public class AwsProxyHttpServletRequestTest {
7271
7372 @ Test
7473 public void headers_getHeader_validRequest () {
75- HttpServletRequest request = new AwsProxyHttpServletRequest (REQUEST_WITH_HEADERS , null , null );
74+ HttpServletRequest request = new AwsProxyHttpServletRequest (getRequestWithHeaders () , null , null );
7675 assertNotNull (request .getHeader (CUSTOM_HEADER_KEY ));
7776 assertEquals (CUSTOM_HEADER_VALUE , request .getHeader (CUSTOM_HEADER_KEY ));
7877 assertEquals (MediaType .APPLICATION_JSON , request .getContentType ());
@@ -147,15 +146,15 @@ public void scheme_getScheme_https() {
147146
148147 @ Test
149148 public void scheme_getScheme_http () {
150- HttpServletRequest request = new AwsProxyHttpServletRequest (REQUEST_WITH_HEADERS , null , null );
149+ HttpServletRequest request = new AwsProxyHttpServletRequest (getRequestWithHeaders () , null , null );
151150 assertNotNull (request );
152151 assertNotNull (request .getScheme ());
153152 assertEquals (REQUEST_SCHEME_HTTP , request .getScheme ());
154153 }
155154
156155 @ Test
157156 public void cookie_getCookies_noCookies () {
158- HttpServletRequest request = new AwsProxyHttpServletRequest (REQUEST_WITH_HEADERS , null , null );
157+ HttpServletRequest request = new AwsProxyHttpServletRequest (getRequestWithHeaders () , null , null );
159158 assertNotNull (request );
160159 assertNotNull (request .getCookies ());
161160 assertEquals (0 , request .getCookies ().length );
@@ -243,4 +242,106 @@ public void queryParameter_getParameterMap_avoidDuplicationOnMultipleCalls() {
243242 assertNotNull (params .get (FORM_PARAM_TEST ));
244243 assertEquals (1 , params .get (FORM_PARAM_TEST ).length );
245244 }
245+
246+ @ Test
247+ public void charEncoding_getEncoding_expectNoEncodingWithoutContentType () {
248+ HttpServletRequest request = new AwsProxyHttpServletRequest (REQUEST_SINGLE_COOKIE , null , null );
249+ try {
250+ request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
251+ // we have not specified a content type so the encoding will not be set
252+ assertEquals (null , request .getCharacterEncoding ());
253+ assertEquals (null , request .getContentType ());
254+ } catch (UnsupportedEncodingException e ) {
255+ fail ("Unsupported encoding" );
256+ e .printStackTrace ();
257+ }
258+ }
259+
260+ @ Test
261+ public void charEncoding_getEncoding_expectContentTypeOnly () {
262+ HttpServletRequest request = new AwsProxyHttpServletRequest (getRequestWithHeaders (), null , null );
263+ // we have not specified a content type so the encoding will not be set
264+ assertEquals (null , request .getCharacterEncoding ());
265+ assertEquals (MediaType .APPLICATION_JSON , request .getContentType ());
266+ try {
267+ request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
268+ String newHeaderValue = MediaType .APPLICATION_JSON + "; charset=" + StandardCharsets .UTF_8 .name ();
269+ assertEquals (newHeaderValue , request .getHeader (HttpHeaders .CONTENT_TYPE ));
270+ assertEquals (newHeaderValue , request .getContentType ());
271+ assertEquals (StandardCharsets .UTF_8 .name (), request .getCharacterEncoding ());
272+ } catch (UnsupportedEncodingException e ) {
273+ fail ("Unsupported encoding" );
274+ e .printStackTrace ();
275+ }
276+ }
277+
278+ @ Test
279+ public void charEncoding_addCharEncodingTwice_expectSingleMediaTypeAndEncoding () {
280+ HttpServletRequest request = new AwsProxyHttpServletRequest (getRequestWithHeaders (), null , null );
281+ // we have not specified a content type so the encoding will not be set
282+ assertEquals (null , request .getCharacterEncoding ());
283+ assertEquals (MediaType .APPLICATION_JSON , request .getContentType ());
284+
285+ try {
286+ request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
287+ String newHeaderValue = MediaType .APPLICATION_JSON + "; charset=" + StandardCharsets .UTF_8 .name ();
288+ assertEquals (newHeaderValue , request .getHeader (HttpHeaders .CONTENT_TYPE ));
289+ assertEquals (newHeaderValue , request .getContentType ());
290+ assertEquals (StandardCharsets .UTF_8 .name (), request .getCharacterEncoding ());
291+
292+
293+ request .setCharacterEncoding (StandardCharsets .ISO_8859_1 .name ());
294+ newHeaderValue = MediaType .APPLICATION_JSON + "; charset=" + StandardCharsets .ISO_8859_1 .name ();
295+ assertEquals (newHeaderValue , request .getHeader (HttpHeaders .CONTENT_TYPE ));
296+ assertEquals (newHeaderValue , request .getContentType ());
297+ assertEquals (StandardCharsets .ISO_8859_1 .name (), request .getCharacterEncoding ());
298+ } catch (UnsupportedEncodingException e ) {
299+ fail ("Unsupported encoding" );
300+ e .printStackTrace ();
301+ }
302+ }
303+
304+ @ Test
305+ public void contentType_lowerCaseHeaderKey_expectUpdatedMediaType () {
306+ HttpServletRequest request = new AwsProxyHttpServletRequest (REQUEST_WITH_LOWERCASE_HEADER , null , null );
307+ try {
308+ request .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
309+ String newHeaderValue = MediaType .APPLICATION_JSON + "; charset=" + StandardCharsets .UTF_8 .name ();
310+ assertEquals (newHeaderValue , request .getHeader (HttpHeaders .CONTENT_TYPE ));
311+ assertEquals (newHeaderValue , request .getContentType ());
312+ assertEquals (StandardCharsets .UTF_8 .name (), request .getCharacterEncoding ());
313+
314+ } catch (UnsupportedEncodingException e ) {
315+ fail ("Unsupported encoding" );
316+ e .printStackTrace ();
317+ }
318+ }
319+
320+ @ Test
321+ public void contentType_duplicateCase_expectSingleContentTypeHeader () {
322+ AwsProxyRequest proxyRequest = getRequestWithHeaders ();
323+ HttpServletRequest request = new AwsProxyHttpServletRequest (proxyRequest , null , null );
324+
325+ try {
326+ request .setCharacterEncoding (StandardCharsets .ISO_8859_1 .name ());
327+ assertNotNull (request .getHeader (HttpHeaders .CONTENT_TYPE ));
328+ assertNotNull (request .getHeader (HttpHeaders .CONTENT_TYPE .toLowerCase (Locale .getDefault ())));
329+
330+ assertFalse (proxyRequest .getHeaders ().containsKey (HttpHeaders .CONTENT_TYPE ) && proxyRequest .getHeaders ().containsKey (HttpHeaders .CONTENT_TYPE .toLowerCase (Locale .getDefault ())));
331+ } catch (UnsupportedEncodingException e ) {
332+ fail ("Unsupported encoding" );
333+ e .printStackTrace ();
334+ }
335+
336+
337+
338+ }
339+
340+ private AwsProxyRequest getRequestWithHeaders () {
341+ return new AwsProxyRequestBuilder ("/hello" , "GET" )
342+ .header (CUSTOM_HEADER_KEY , CUSTOM_HEADER_VALUE )
343+ .header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON )
344+ .header (AwsProxyHttpServletRequest .CF_PROTOCOL_HEADER_NAME , REQUEST_SCHEME_HTTP )
345+ .build ();
346+ }
246347}
0 commit comments