Search in sources :

Example 16 with MockHttpServletRequest

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

the class WebUtilsTests method testWithXForwardedHeaders.

private void testWithXForwardedHeaders(String serverName, int port, String forwardedProto, String forwardedHost, int forwardedPort, String originHeader) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName(serverName);
    if (port != -1) {
        request.setServerPort(port);
    }
    if (forwardedProto != null) {
        request.addHeader("X-Forwarded-Proto", forwardedProto);
    }
    if (forwardedHost != null) {
        request.addHeader("X-Forwarded-Host", forwardedHost);
    }
    if (forwardedPort != -1) {
        request.addHeader("X-Forwarded-Port", String.valueOf(forwardedPort));
    }
    request.addHeader(HttpHeaders.ORIGIN, originHeader);
// HttpServletRequest requestToUse = adaptFromForwardedHeaders(request);
// ServerHttpRequest httpRequest = new ServletServerHttpRequest(requestToUse);
// 
// assertThat(WebUtils.isSameOrigin(httpRequest)).isTrue();
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest)

Example 17 with MockHttpServletRequest

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

the class BaseViewTests method renderWithStaticAttributesNoCollision.

/**
 * Test attribute passing, NOT CSV parsing.
 */
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    tv.render(model, requestContext);
    checkContainsAll(model, tv.model);
    checkContainsAll(p, tv.model);
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.mock.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 18 with MockHttpServletRequest

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

the class BaseViewTests method dynamicModelOverridesStaticAttributesIfCollision.

@Test
public void dynamicModelOverridesStaticAttributesIfCollision() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("one", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    tv.render(model, requestContext);
    // Check it contains all
    checkContainsAll(model, tv.model);
    assertThat(tv.model.size()).isEqualTo(3);
    assertThat(tv.model.get("something")).isEqualTo("else");
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.mock.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 19 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort.

@Test
void fromHttpRequestForwardedHeaderWithoutHostPortAndWithServerPort() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Forwarded", "proto=https; host=84.198.58.199");
    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(-1);
    assertThat(result.toUriString()).isEqualTo("https://84.198.58.199/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 20 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedProtoMultiValueHeader.

@Test
// SPR-12816
void fromHttpRequestWithForwardedProtoMultiValueHeader() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(8080);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "a.example.org");
    request.addHeader("X-Forwarded-Port", "443");
    request.addHeader("X-Forwarded-Proto", "https,https");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
    assertThat(result.toString()).isEqualTo("https://a.example.org/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