Search in sources :

Example 6 with MockHttpServletRequest

use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderWithHostPortAndServerPort.

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

Example 7 with MockHttpServletRequest

use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv6Host.

@Test
// SPR-14761
void fromHttpRequestWithForwardedIPv6Host() {
    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]");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.toString()).isEqualTo("http://[1abc:2abc:3abc::5ABC:6abc]/mvc-showcase");
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with MockHttpServletRequest

use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedProtoWithDefaultPort.

@Test
// SPR-16262
void fromHttpRequestWithForwardedProtoWithDefaultPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("example.org");
    request.setServerPort(10080);
    request.addHeader("X-Forwarded-Proto", "https");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.getScheme()).isEqualTo("https");
    assertThat(result.getHost()).isEqualTo("example.org");
    assertThat(result.getPort()).isEqualTo(-1);
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with MockHttpServletRequest

use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedIPv4Host.

@Test
// SPR-14761
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 : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with MockHttpServletRequest

use of cn.taketoday.web.mock.MockHttpServletRequest in project today-infrastructure by TAKETODAY.

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHost.

@Test
void fromHttpRequestWithForwardedHost() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("https");
    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();
    assertThat(result.toString()).isEqualTo("https://anotherHost/mvc-showcase");
}
Also used : ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) HttpRequest(cn.taketoday.http.HttpRequest) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)221 Test (org.junit.jupiter.api.Test)187 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)97 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)57 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)44 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 ServletServerHttpRequest (cn.taketoday.http.server.ServletServerHttpRequest)29 HttpRequest (cn.taketoday.http.HttpRequest)27 Cookie (jakarta.servlet.http.Cookie)20 RequestContext (cn.taketoday.web.RequestContext)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 ClassPathResource (cn.taketoday.core.io.ClassPathResource)15 Resource (cn.taketoday.core.io.Resource)15 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)15 Locale (java.util.Locale)15 MockServletContext (cn.taketoday.web.mock.MockServletContext)14 HttpMethod (cn.taketoday.http.HttpMethod)12 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)11 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)11 LocaleContext (cn.taketoday.core.i18n.LocaleContext)10