Search in sources :

Example 1 with MockHttpServletResponse

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

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

the class NashornScriptTemplateTests method renderTemplateWithUrl.

// SPR-13453
@Test
public void renderTemplateWithUrl() throws Exception {
    String url = "cn/taketoday/web/servlet/view/script/nashorn/template.html";
    MockHttpServletResponse response = render(url, null, ScriptTemplatingWithUrlConfiguration.class);
    assertThat(response.getContentAsString()).isEqualTo(("<html><head><title>Check url parameter</title></head><body><p>" + url + "</p></body></html>"));
}
Also used : MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockHttpServletResponse

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

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

the class RedirectViewTests method contextRelativeWithValidatedContextPath.

// SPR-16752
@Test
public void contextRelativeWithValidatedContextPath() throws Exception {
    String url = "/myUrl";
    this.request.setContextPath("//context");
    this.response = new MockHttpServletResponse();
    this.context = null;
    doTest(new HashMap<>(), url, true, "/context" + url);
    this.request.setContextPath("///context");
    this.response = new MockHttpServletResponse();
    this.context = null;
    doTest(new HashMap<>(), url, true, "/context" + url);
}
Also used : MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MockHttpServletResponse

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

the class WebUtilsTests method checkSameOrigin.

private boolean checkSameOrigin(String scheme, String serverName, int port, String originHeader) {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setScheme(scheme);
    servletRequest.setServerName(serverName);
    if (port != -1) {
        servletRequest.setServerPort(port);
    }
    servletRequest.addHeader(HttpHeaders.ORIGIN, originHeader);
    ServletRequestContext context = new ServletRequestContext(null, servletRequest, new MockHttpServletResponse());
    return WebUtils.isSameOrigin(context);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse)

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