Search in sources :

Example 6 with MockHttpServletResponse

use of cn.taketoday.web.mock.MockHttpServletResponse 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 7 with MockHttpServletResponse

use of cn.taketoday.web.mock.MockHttpServletResponse 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 8 with MockHttpServletResponse

use of cn.taketoday.web.mock.MockHttpServletResponse 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 9 with MockHttpServletResponse

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

the class ResourceHttpRequestHandlerTests method testInvalidPath.

private void testInvalidPath(String requestPath, ResourceHttpRequestHandler handler) throws Exception {
    request.setRequestURI(requestPath);
    this.response = new MockHttpServletResponse();
    requestContext = new ServletRequestContext(null, request, response);
    handler.handleRequest(requestContext);
    assertThat(this.response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
}
Also used : ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

Example 10 with MockHttpServletResponse

use of cn.taketoday.web.mock.MockHttpServletResponse 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)

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