Search in sources :

Example 16 with MockHttpServletResponse

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

the class HiddenHttpMethodFilterTests method filterWithParameterForMethod.

private void filterWithParameterForMethod(String methodParam, String expectedMethod) throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    if (methodParam != null) {
        request.addParameter("_method", methodParam);
    }
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> assertThat(((HttpServletRequest) filterRequest).getMethod()).as("Invalid method").isEqualTo(expectedMethod);
    this.filter.doFilter(request, response, filterChain);
}
Also used : Test(org.junit.jupiter.api.Test) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) IOException(java.io.IOException) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletException(jakarta.servlet.ServletException) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

Example 17 with MockHttpServletResponse

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

the class ForwardedHeaderFilterTests method forwardedRequestWithServletForward.

// SPR-16983
@Test
public void forwardedRequestWithServletForward() throws Exception {
    this.request.setRequestURI("/foo");
    this.request.addHeader(X_FORWARDED_PROTO, "https");
    this.request.addHeader(X_FORWARDED_HOST, "www.mycompany.example");
    this.request.addHeader(X_FORWARDED_PORT, "443");
    this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
    HttpServletRequest wrappedRequest = (HttpServletRequest) this.filterChain.getRequest();
    this.request.setDispatcherType(DispatcherType.FORWARD);
    this.request.setRequestURI("/bar");
    this.filterChain.reset();
    this.filter.doFilter(wrappedRequest, new MockHttpServletResponse(), this.filterChain);
    HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
    assertThat(actual).isNotNull();
    assertThat(actual.getRequestURI()).isEqualTo("/bar");
    assertThat(actual.getRequestURL().toString()).isEqualTo("https://www.mycompany.example/bar");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with MockHttpServletResponse

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

the class ForwardedHeaderFilterTests method forwardedRequestInRemoveOnlyMode.

@Test
public void forwardedRequestInRemoveOnlyMode() throws Exception {
    this.request.setRequestURI("/mvc-showcase");
    this.request.addHeader(X_FORWARDED_PROTO, "https");
    this.request.addHeader(X_FORWARDED_HOST, "84.198.58.199");
    this.request.addHeader(X_FORWARDED_PORT, "443");
    this.request.addHeader(X_FORWARDED_SSL, "on");
    this.request.addHeader("foo", "bar");
    this.request.addHeader(X_FORWARDED_FOR, "203.0.113.195");
    this.filter.setRemoveOnly(true);
    this.filter.doFilter(this.request, new MockHttpServletResponse(), this.filterChain);
    HttpServletRequest actual = (HttpServletRequest) this.filterChain.getRequest();
    assertThat(actual).isNotNull();
    assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/mvc-showcase");
    assertThat(actual.getScheme()).isEqualTo("http");
    assertThat(actual.getServerName()).isEqualTo("localhost");
    assertThat(actual.getServerPort()).isEqualTo(80);
    assertThat(actual.isSecure()).isFalse();
    assertThat(actual.getRemoteAddr()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_ADDR);
    assertThat(actual.getRemoteHost()).isEqualTo(MockHttpServletRequest.DEFAULT_REMOTE_HOST);
    assertThat(actual.getHeader(X_FORWARDED_PROTO)).isNull();
    assertThat(actual.getHeader(X_FORWARDED_HOST)).isNull();
    assertThat(actual.getHeader(X_FORWARDED_PORT)).isNull();
    assertThat(actual.getHeader(X_FORWARDED_SSL)).isNull();
    assertThat(actual.getHeader(X_FORWARDED_FOR)).isNull();
    assertThat(actual.getHeader("foo")).isEqualTo("bar");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with MockHttpServletResponse

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

the class ReactiveTypeHandlerTests method resetRequest.

private void resetRequest() {
    this.servletRequest = new MockHttpServletRequest();
    this.servletResponse = new MockHttpServletResponse();
    this.webRequest = new ServletRequestContext(null, this.servletRequest, this.servletResponse);
    AsyncWebRequest webRequest = new StandardServletAsyncWebRequest(this.webRequest);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncRequest(webRequest);
    this.servletRequest.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) AsyncWebRequest(cn.taketoday.web.context.async.AsyncWebRequest) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

Example 20 with MockHttpServletResponse

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

the class ControllerTests method parameterizableViewController.

@Test
public void parameterizableViewController() throws Exception {
    String viewName = "viewName";
    ParameterizableViewController pvc = new ParameterizableViewController();
    pvc.setViewName(viewName);
    // We don't care about the params.
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.refresh();
    ServletRequestContext context = new ServletRequestContext(wac, new MockHttpServletRequest("GET", "foo.html"), new MockHttpServletResponse());
    ModelAndView mv = pvc.handleRequest(context);
    assertThat(mv.getModel().size() == 0).as("model has no data").isTrue();
    assertThat(mv.getViewName().equals(viewName)).as("model has correct viewname").isTrue();
    assertThat(pvc.getViewName().equals(viewName)).as("getViewName matches").isTrue();
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ModelAndView(cn.taketoday.web.view.ModelAndView) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)75 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)61 Test (org.junit.jupiter.api.Test)51 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)22 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)17 BeforeEach (org.junit.jupiter.api.BeforeEach)16 RequestContext (cn.taketoday.web.RequestContext)14 HashMap (java.util.HashMap)14 Locale (java.util.Locale)13 MockServletContext (cn.taketoday.web.mock.MockServletContext)12 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)12 Cookie (jakarta.servlet.http.Cookie)11 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)9 HttpMethod (cn.taketoday.http.HttpMethod)8 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)7 FilterChain (jakarta.servlet.FilterChain)7 ServletException (jakarta.servlet.ServletException)7 IOException (java.io.IOException)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 HttpHeaders (cn.taketoday.http.HttpHeaders)6