Search in sources :

Example 26 with MockHttpServletRequest

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

the class ResourceHttpRequestHandlerTests method resourceNotFound.

@Test
public void resourceNotFound() throws Exception {
    for (HttpMethod method : HttpMethod.values()) {
        this.request = new MockHttpServletRequest("GET", "");
        this.response = new MockHttpServletResponse();
        resourceNotFound(method);
    }
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) HttpMethod(cn.taketoday.http.HttpMethod) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 27 with MockHttpServletRequest

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

the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.

// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
    MockServletContext servletContext = new MockServletContext() {

        @Override
        public String getMimeType(String filePath) {
            return "foo/bar";
        }
    };
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setLocations(paths);
    handler.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "");
    request.setRequestURI("foo.css");
    handler.handleRequest(new ServletRequestContext(null, request, response));
    assertThat(this.response.getContentType()).isEqualTo("foo/bar");
    assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Resource(cn.taketoday.core.io.Resource) UrlBasedResource(cn.taketoday.core.io.UrlBasedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 28 with MockHttpServletRequest

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

the class ResourceHttpRequestHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    List<Resource> paths = new ArrayList<>(2);
    paths.add(new ClassPathResource("test/", getClass()));
    paths.add(new ClassPathResource("testalternatepath/", getClass()));
    paths.add(new ClassPathResource("META-INF/resources/webjars/"));
    TestServletContext servletContext = new TestServletContext();
    this.handler = new ResourceHttpRequestHandler();
    this.handler.setLocations(paths);
    this.handler.setCacheSeconds(3600);
    this.handler.afterPropertiesSet();
    this.request = new MockHttpServletRequest(servletContext, "GET", "");
    this.response = new MockHttpServletResponse();
    requestContext = new ServletRequestContext(null, request, response);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) Resource(cn.taketoday.core.io.Resource) UrlBasedResource(cn.taketoday.core.io.UrlBasedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ArrayList(java.util.ArrayList) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with MockHttpServletRequest

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

the class ResourceHttpRequestHandlerTests method resolvePathWithTraversal.

@Test
@DisabledOnOs(OS.WINDOWS)
public void resolvePathWithTraversal() throws Exception {
    for (HttpMethod method : HttpMethod.values()) {
        this.request = new MockHttpServletRequest("GET", "");
        this.response = new MockHttpServletResponse();
        requestContext = new ServletRequestContext(null, request, response);
        testResolvePathWithTraversal(method);
    }
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) HttpMethod(cn.taketoday.http.HttpMethod) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Example 30 with MockHttpServletRequest

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

the class ResourceUrlProviderJavaConfigTests method setup.

@BeforeEach
@SuppressWarnings("resource")
public void setup() throws Exception {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(WebConfig.class);
    context.refresh();
    this.request = new MockHttpServletRequest("GET", "/");
    this.request.setContextPath("/myapp");
    this.response = new MockHttpServletResponse();
    this.filterChain = new MockFilterChain(this.servlet);
}
Also used : AnnotationConfigServletWebApplicationContext(cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

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