Search in sources :

Example 1 with MockHttpServletRequest

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

the class ContentNegotiatingViewResolverTests method createViewResolver.

@BeforeEach
public void createViewResolver() {
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    viewResolver = new ContentNegotiatingViewResolver();
    viewResolver.setApplicationContext(wac);
    request = new MockHttpServletRequest("GET", "/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.requestContext = new MockServletRequestContext(wac, request, response);
    RequestContextHolder.set(requestContext);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MockHttpServletRequest

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

the class InternalResourceViewTests method forward.

@Test
public void forward() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
    request.setContextPath("/mycontext");
    request.setServletPath("/myservlet");
    request.setPathInfo(";mypathinfo");
    request.setQueryString("?param1=value1");
    RequestContext context = new ServletRequestContext(null, request, response);
    view.setUrl(url);
    view.setServletContext(new MockServletContext() {

        @Override
        public int getMinorVersion() {
            return 4;
        }
    });
    view.render(model, context);
    assertThat(response.getForwardedUrl()).isEqualTo(url);
    model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockHttpServletRequest

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

the class ScriptTemplateViewTests method resourceLoaderPath.

// SPR-14210
@Test
public void resourceLoaderPath() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
    given(engine.invokeFunction(any(), any(), any(), any())).willReturn("foo");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    this.view.setUrl("cn/taketoday/web/servlet/view/script/empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script/");
    this.view.setUrl("empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script");
    this.view.setUrl("empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestForwardedHeaderComma.

@Test
// gh-25737
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 : 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 5 with MockHttpServletRequest

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

the class UriComponentsBuilderTests method fromHttpRequestWithForwardedInvalidIPv6Address.

@Test
// gh-26748
void fromHttpRequestWithForwardedInvalidIPv6Address() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("http");
    request.setServerName("localhost");
    request.setServerPort(-1);
    request.setRequestURI("/mvc-showcase");
    request.addHeader("X-Forwarded-Host", "2a02:918:175:ab60:45ee:c12c:dac1:808b");
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    assertThatIllegalArgumentException().isThrownBy(() -> UriComponentsBuilder.fromHttpRequest(httpRequest).build());
}
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