Search in sources :

Example 11 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostWithDefaultPort.

@Test
// SPR-11872
void fromHttpRequestWithForwardedHostWithDefaultPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(10080);
    request.addHeader("X-Forwarded-Host", "example.org");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    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 12 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedHostWithForwardedScheme.

@Test
void fromHttpRequestWithForwardedHostWithForwardedScheme() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(10080);
    request.addHeader("X-Forwarded-Host", "example.org");
    request.addHeader("X-Forwarded-Proto", "https");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.getHost()).isEqualTo("example.org");
    assertThat(result.getScheme()).isEqualTo("https");
    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 13 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderQuoted.

@Test
void fromHttpRequestForwardedHeaderQuoted() {
    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();
    assertThat(result.getScheme()).isEqualTo("https");
    assertThat(result.getHost()).isEqualTo("84.198.58.199");
    assertThat(result.getPath()).isEqualTo("/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 14 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestResetsPort443.

// gh-17368, gh-27097
@ParameterizedTest
@ValueSource(strings = { "https", "wss" })
void fromHttpRequestResetsPort443(String protocol) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("X-Forwarded-Proto", protocol);
    request.addHeader("X-Forwarded-Host", "84.198.58.199");
    request.addHeader("X-Forwarded-Port", 443);
    request.setScheme("http");
    request.setServerName("example.com");
    request.setServerPort(80);
    request.setRequestURI("/rest/mobile/users/1");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.getScheme()).isEqualTo(protocol);
    assertThat(result.getHost()).isEqualTo("84.198.58.199");
    assertThat(result.getPort()).isEqualTo(-1);
    assertThat(result.getPath()).isEqualTo("/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) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with MockHttpServletRequest

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

the class WebUtilsTests method checkSameOrigin.

private boolean checkSameOrigin(String scheme, String serverName, int port, String originHeader) {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setScheme(scheme);
    servletRequest.setServerName(serverName);
    if (port != -1) {
        servletRequest.setServerPort(port);
    }
    servletRequest.addHeader(HttpHeaders.ORIGIN, originHeader);
    ServletRequestContext context = new ServletRequestContext(null, servletRequest, new MockHttpServletResponse());
    return WebUtils.isSameOrigin(context);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

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