Search in sources :

Example 16 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostMultiValuedHeader.

// SPR-11140
@Test
public void fromHttpRequestWithForwardedHostMultiValuedHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("a.example.org", result.getHost());
    assertEquals(-1, result.getPort());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 17 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostAndPort.

// SPR-11855
@Test
public void fromHttpRequestWithForwardedHostAndPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(8080);
    request.addHeader("X-Forwarded-Host", "foobarhost");
    request.addHeader("X-Forwarded-Port", "9090");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("foobarhost", result.getHost());
    assertEquals(9090, result.getPort());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 18 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestMultipleForwardedHeader.

@Test
public void fromHttpRequestMultipleForwardedHeader() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "host=84.198.58.199;proto=https");
    request.addHeader("Forwarded", "proto=ftp; host=1.2.3.4");
    request.setScheme("http");
    request.setServerName("example.com");
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https", result.getScheme());
    assertEquals("84.198.58.199", result.getHost());
    assertEquals("/rest/mobile/users/1", result.getPath());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 19 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHost.

@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());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 20 with ServletServerHttpRequest

use of org.springframework.http.server.ServletServerHttpRequest in project spring-framework by spring-projects.

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderQuoted.

@Test
public void fromHttpRequestForwardedHeaderQuoted() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "proto=\"https\"; host=\"84.198.58.199\"");
    request.setScheme("http");
    request.setServerName("example.com");
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertEquals("https", result.getScheme());
    assertEquals("84.198.58.199", result.getHost());
    assertEquals("/rest/mobile/users/1", result.getPath());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)64 Test (org.junit.Test)33 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)29 HttpRequest (org.springframework.http.HttpRequest)20 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Before (org.junit.Before)8 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)5 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)4 MediaType (org.springframework.http.MediaType)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1