@Test public void getServerPortViaHostHeaderAsIpv6AddressWithPort() { String testServer = "[2001:db8:0:1]"; int testPort = 9999; request.addHeader(HOST, testServer + ":" + testPort); assertEquals(testPort, request.getServerPort()); }
@Test public void isSecureWithHttpsSchemeAndSecureFlagIsTrue() { assertFalse(request.isSecure()); request.setScheme("https"); request.setSecure(true); assertTrue(request.isSecure()); }
@Test public void getRequestURLWithDefaultsAndHttps() { request.setScheme("https"); request.setServerPort(443); StringBuffer requestURL = request.getRequestURL(); assertEquals("https://localhost", requestURL.toString()); }
@Test public void getServerPortViaHostHeaderWithPort() { String testServer = "test.server"; int testPort = 9999; request.addHeader(HOST, testServer + ":" + testPort); assertEquals(testPort, request.getServerPort()); }
@Test public void isSecureWithHttpSchemeAndSecureFlagIsFalse() { assertFalse(request.isSecure()); request.setScheme("http"); request.setSecure(false); assertFalse(request.isSecure()); }
@Test public void contentTypeHeader() { String contentType = "test/plain"; request.addHeader("Content-Type", contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertNull(request.getCharacterEncoding()); }
@Test public void contentTypeHeaderUTF8() { String contentType = "test/plain;charset=UTF-8"; request.addHeader("Content-Type", contentType); assertEquals(contentType, request.getContentType()); assertEquals(contentType, request.getHeader("Content-Type")); assertEquals("UTF-8", request.getCharacterEncoding()); }
@Test public void setCharacterEncodingThenContentType() { request.setCharacterEncoding("UTF-8"); request.setContentType("test/plain"); assertEquals("test/plain", request.getContentType()); assertEquals("test/plain;charset=UTF-8", request.getHeader("Content-Type")); assertEquals("UTF-8", request.getCharacterEncoding()); }
@Test public void httpHeaderNameCasingIsPreserved() throws Exception { String headerName = "Header1"; request.addHeader(headerName, "value1"); Enumeration<String> requestHeaders = request.getHeaderNames(); assertNotNull(requestHeaders); assertEquals( "HTTP header casing not being preserved", headerName, requestHeaders.nextElement()); }
@Test public void content() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertNotNull(request.getInputStream()); assertEquals( "body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())); }
@Test public void removeAllParameters() { request.setParameter("key1", "value1"); Map<String, Object> params = new HashMap<String, Object>(2); params.put("key2", "value2"); params.put("key3", new String[] {"value3A", "value3B"}); request.addParameters(params); assertEquals(3, request.getParameterMap().size()); request.removeAllParameters(); assertEquals(0, request.getParameterMap().size()); }
@Test public void defaultLocale() { Locale originalDefaultLocale = Locale.getDefault(); try { Locale newDefaultLocale = originalDefaultLocale.equals(Locale.GERMANY) ? Locale.FRANCE : Locale.GERMANY; Locale.setDefault(newDefaultLocale); // Create the request after changing the default locale. MockHttpServletRequest request = new MockHttpServletRequest(); assertFalse(newDefaultLocale.equals(request.getLocale())); assertEquals(Locale.ENGLISH, request.getLocale()); } finally { Locale.setDefault(originalDefaultLocale); } }
@Test public void addMultipleParameters() { request.setParameter("key1", "value1"); request.setParameter("key2", "value2"); Map<String, Object> params = new HashMap<String, Object>(2); params.put("key1", "newValue1"); params.put("key3", new String[] {"value3A", "value3B"}); request.addParameters(params); String[] values1 = request.getParameterValues("key1"); assertEquals(2, values1.length); assertEquals("value1", values1[0]); assertEquals("newValue1", values1[1]); assertEquals("value2", request.getParameter("key2")); String[] values3 = request.getParameterValues("key3"); assertEquals(2, values3.length); assertEquals("value3A", values3[0]); assertEquals("value3B", values3[1]); }
@Test public void getRequestURL() { request.setServerPort(8080); request.setRequestURI("/path"); assertEquals("http://localhost:8080/path", request.getRequestURL().toString()); request.setScheme("https"); request.setServerName("example.com"); request.setServerPort(8443); assertEquals("https://example.com:8443/path", request.getRequestURL().toString()); }
@Test(expected = IllegalArgumentException.class) public void setPreferredLocalesWithNullList() { request.setPreferredLocales(null); }
@Test public void getServerNameViaHostHeaderWithPort() { String testServer = "test.server"; request.addHeader(HOST, testServer + ":8080"); assertEquals(testServer, request.getServerName()); }
@Test public void getServerPortWithDefaultPort() { assertEquals(80, request.getServerPort()); }
@Test public void getServerPortWithCustomPort() { request.setServerPort(8080); assertEquals(8080, request.getServerPort()); }
@Test public void getServerNameWithCustomName() { request.setServerName("example.com"); assertEquals("example.com", request.getServerName()); }
@Test public void getRequestURLWithNegativePort() { request.setServerPort(-99); StringBuffer requestURL = request.getRequestURL(); assertEquals("http://localhost", requestURL.toString()); }
@Test public void getServerPortViaHostHeaderAsIpv6AddressWithoutPort() { String testServer = "[2001:db8:0:1]"; request.addHeader(HOST, testServer); assertEquals(80, request.getServerPort()); }
@Test public void getRequestURLWithNullRequestUri() { request.setRequestURI(null); StringBuffer requestURL = request.getRequestURL(); assertEquals("http://localhost", requestURL.toString()); }
@Test public void getRequestURLWithDefaults() { StringBuffer requestURL = request.getRequestURL(); assertEquals("http://localhost", requestURL.toString()); }
@Test(expected = IllegalArgumentException.class) public void setPreferredLocalesWithEmptyList() { request.setPreferredLocales(new ArrayList<Locale>()); }
@Test public void setPreferredLocales() { List<Locale> preferredLocales = Arrays.asList(Locale.ITALY, Locale.CHINA); request.setPreferredLocales(preferredLocales); assertEqualEnumerations(Collections.enumeration(preferredLocales), request.getLocales()); }
@Test public void noContent() throws IOException { assertEquals(-1, request.getContentLength()); assertNotNull(request.getInputStream()); assertEquals(-1, request.getInputStream().read()); }
@Test public void getServerNameViaHostHeaderAsIpv6AddressWithPort() { String ipv6Address = "[2001:db8:0:1]:8081"; request.addHeader(HOST, ipv6Address); assertEquals("2001:db8:0:1", request.getServerName()); }
@Test public void nullParameterName() { assertNull(request.getParameter(null)); assertNull(request.getParameterValues(null)); }
@Test public void getServerNameWithDefaultName() { assertEquals("localhost", request.getServerName()); }
@Test public void getServerPortViaHostHeaderWithoutPort() { String testServer = "test.server"; request.addHeader(HOST, testServer); assertEquals(80, request.getServerPort()); }