Search in sources :

Example 1 with MockHttpServletResponse

use of cn.taketoday.web.testfixture.servlet.MockHttpServletResponse in project today-framework by TAKETODAY.

the class ServerHttpRequestTests method createRequest.

private ServerHttpRequest createRequest(String uriString, String contextPath) throws Exception {
    URI uri = URI.create(uriString);
    MockHttpServletRequest request = new TestHttpServletRequest(uri);
    request.setContextPath(contextPath);
    AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
    return new ServletServerHttpRequest(request, asyncContext, "", DefaultDataBufferFactory.sharedInstance, 1024);
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) MockAsyncContext(cn.taketoday.web.testfixture.servlet.MockAsyncContext) MockAsyncContext(cn.taketoday.web.testfixture.servlet.MockAsyncContext) AsyncContext(jakarta.servlet.AsyncContext) URI(java.net.URI) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)

Example 2 with MockHttpServletResponse

use of cn.taketoday.web.testfixture.servlet.MockHttpServletResponse in project today-framework by TAKETODAY.

the class DefaultServletHandlerConfigurerTests method setup.

@BeforeEach
public void setup() {
    response = new MockHttpServletResponse();
    servletContext = new DispatchingMockServletContext();
    configurer = new DefaultServletHandlerConfigurer(servletContext);
}
Also used : MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with MockHttpServletResponse

use of cn.taketoday.web.testfixture.servlet.MockHttpServletResponse in project today-framework by TAKETODAY.

the class CorsFilterTests method nonCorsRequest.

@Test
public void nonCorsRequest() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/test.html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
        assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS)).isNull();
    };
    filter.doFilter(request, response, filterChain);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CorsFilter(cn.taketoday.web.servlet.filter.CorsFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) IOException(java.io.IOException) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) HttpHeaders(cn.taketoday.http.HttpHeaders) Assertions.fail(org.assertj.core.api.Assertions.fail) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletUtils(cn.taketoday.web.servlet.ServletUtils) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpServletResponse

use of cn.taketoday.web.testfixture.servlet.MockHttpServletResponse in project today-framework by TAKETODAY.

the class CorsFilterTests method validActualRequest.

@Test
public void validActualRequest() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(), "/test.html");
    request.addHeader(HttpHeaders.ORIGIN, "https://domain2.com");
    request.addHeader("header2", "foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = (filterRequest, filterResponse) -> {
        final RequestContext context = ServletUtils.getRequestContext(request, response);
        assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("https://domain2.com");
        final String header = response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS);
        assertThat(header).isEqualTo("header3, header4");
    };
    filter.doFilter(request, response, filterChain);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CorsFilter(cn.taketoday.web.servlet.filter.CorsFilter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) IOException(java.io.IOException) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) ServletException(jakarta.servlet.ServletException) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) HttpHeaders(cn.taketoday.http.HttpHeaders) Assertions.fail(org.assertj.core.api.Assertions.fail) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletUtils(cn.taketoday.web.servlet.ServletUtils) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MockHttpServletResponse

use of cn.taketoday.web.testfixture.servlet.MockHttpServletResponse in project today-framework by TAKETODAY.

the class CookieLocaleResolverTests method testSetAndResolveLocale.

@Test
public void testSetAndResolveLocale() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocale(requestContext, new Locale("nl", ""));
    Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    assertThat(cookie).isNotNull();
    assertThat(cookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
    assertThat(cookie.getDomain()).isNull();
    assertThat(cookie.getPath()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_PATH);
    assertThat(cookie.getSecure()).isFalse();
    request = new MockHttpServletRequest();
    request.setCookies(cookie);
    resolver = new CookieLocaleResolver();
    Locale loc = resolver.resolveLocale(requestContext);
    assertThat(loc.getLanguage()).isEqualTo("nl");
}
Also used : Locale(java.util.Locale) Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)104 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)77 Test (org.junit.jupiter.api.Test)66 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)49 BeforeEach (org.junit.jupiter.api.BeforeEach)29 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)18 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)17 RequestContext (cn.taketoday.web.RequestContext)14 HashMap (java.util.HashMap)14 BindingContext (cn.taketoday.web.BindingContext)13 Locale (java.util.Locale)13 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)12 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)12 RequestParam (cn.taketoday.web.annotation.RequestParam)11 Cookie (jakarta.servlet.http.Cookie)11 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)10 ConfigurableWebBindingInitializer (cn.taketoday.web.bind.support.ConfigurableWebBindingInitializer)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)9 HttpMethod (cn.taketoday.http.HttpMethod)8 Method (java.lang.reflect.Method)8