@Test public void serializable() throws Exception { UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com") .path("/{foo}") .query("bar={baz}") .build(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(uriComponents); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); UriComponents readObject = (UriComponents) ois.readObject(); assertThat(uriComponents.toString(), equalTo(readObject.toString())); }
@Test public void fromHttpRequestWithForwardedHost() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Host", "anotherHost"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("http://anotherHost/mvc-showcase", result.toString()); }
@Test // SPR-14761 public void fromHttpRequestWithForwardedIPv6HostAndPort() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Host", "[1abc:2abc:3abc::5ABC:6abc]:8080"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("http://[1abc:2abc:3abc::5ABC:6abc]:8080/mvc-showcase", result.toString()); }
@Test // SPR-14761 public void fromHttpRequestWithForwardedIPv4Host() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(-1); request.setRequestURI("/mvc-showcase"); request.addHeader("Forwarded", "host=192.168.0.1"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("http://192.168.0.1/mvc-showcase", result.toString()); }
@Test // SPR-12813 public void fromHttpRequestWithForwardedPortMultiValueHeader() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(9090); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Host", "a.example.org"); request.addHeader("X-Forwarded-Port", "80,52022"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("http://a.example.org/mvc-showcase", result.toString()); }
@Test // SPR-12771 public void fromHttpRequestWithForwardedProtoAndDefaultPort() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(80); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Proto", "https"); request.addHeader("X-Forwarded-Host", "84.198.58.199"); request.addHeader("X-Forwarded-Port", "443"); HttpRequest httpRequest = new ServletServerHttpRequest(request); UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build(); assertEquals("https://84.198.58.199/mvc-showcase", result.toString()); }
@Test // SPR-13257 public void parsesEmptyUri() { UriComponents components = UriComponentsBuilder.fromUriString("").build(); assertThat(components.toString(), equalTo("")); }
@Test public void parsesEmptyFragment() { UriComponents components = UriComponentsBuilder.fromUriString("/example#").build(); assertThat(components.getFragment(), is(nullValue())); assertThat(components.toString(), equalTo("/example")); }