Search in sources :

Example 91 with ServletServerHttpRequest

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

the class UriComponentsBuilderTests method fromHttpRequestMultipleForwardedHeaderComma.

@Test
void fromHttpRequestMultipleForwardedHeaderComma() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "host=84.198.58.199 ;proto=https, 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();
    assertThat(result.getScheme()).isEqualTo("https");
    assertThat(result.getHost()).isEqualTo("84.198.58.199");
    assertThat(result.getPath()).isEqualTo("/rest/mobile/users/1");
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 92 with ServletServerHttpRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv4Host.

// SPR-14761
@Test
void fromHttpRequestWithForwardedIPv4Host() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("https");
    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();
    assertThat(result.toString()).isEqualTo("https://192.168.0.1/mvc-showcase");
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 93 with ServletServerHttpRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv6HostAndPort.

// SPR-14761
@Test
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();
    assertThat(result.toString()).isEqualTo("http://[1abc:2abc:3abc::5ABC:6abc]:8080/mvc-showcase");
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 94 with ServletServerHttpRequest

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

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderComma.

// gh-25737
@Test
void fromHttpRequestForwardedHeaderComma() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "for=192.0.2.0,for=192.0.2.1;proto=https;host=192.0.2.3:9090");
    request.setScheme("http");
    request.setServerPort(8080);
    request.setServerName("example.com");
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.getScheme()).isEqualTo("https");
    assertThat(result.getHost()).isEqualTo("192.0.2.3");
    assertThat(result.getPath()).isEqualTo("/rest/mobile/users/1");
    assertThat(result.getPort()).isEqualTo(9090);
    assertThat(result.toUriString()).isEqualTo("https://192.0.2.3:9090/rest/mobile/users/1");
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 95 with ServletServerHttpRequest

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

the class WebUtilsTests method checkSameOrigin.

private boolean checkSameOrigin(String scheme, String serverName, int port, String originHeader) {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    ServerHttpRequest request = new ServletServerHttpRequest(servletRequest);
    servletRequest.setScheme(scheme);
    servletRequest.setServerName(serverName);
    if (port != -1) {
        servletRequest.setServerPort(port);
    }
    servletRequest.addHeader(HttpHeaders.ORIGIN, originHeader);
    return WebUtils.isSameOrigin(request);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest)

Aggregations

ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)111 Test (org.junit.jupiter.api.Test)39 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)39 HttpRequest (org.springframework.http.HttpRequest)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ServerHttpRequest (org.springframework.http.server.ServerHttpRequest)13 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)10 IOException (java.io.IOException)8 ServerHttpResponse (org.springframework.http.server.ServerHttpResponse)8 BeforeEach (org.junit.jupiter.api.BeforeEach)7 HttpHeaders (org.springframework.http.HttpHeaders)7 MediaType (org.springframework.http.MediaType)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 HttpInputMessage (org.springframework.http.HttpInputMessage)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 URI (java.net.URI)4 ResponseEntity (org.springframework.http.ResponseEntity)4