use of cn.taketoday.web.mock.MockServletContext 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);
}
use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.
the class InternalResourceViewTests method forward.
@Test
public void forward() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
request.setContextPath("/mycontext");
request.setServletPath("/myservlet");
request.setPathInfo(";mypathinfo");
request.setQueryString("?param1=value1");
RequestContext context = new ServletRequestContext(null, request, response);
view.setUrl(url);
view.setServletContext(new MockServletContext() {
@Override
public int getMinorVersion() {
return 4;
}
});
view.render(model, context);
assertThat(response.getForwardedUrl()).isEqualTo(url);
model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
use of cn.taketoday.web.mock.MockServletContext 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");
}
use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.
the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
StaticWebServletApplicationContext webAppContext = new StaticWebServletApplicationContext();
webAppContext.setServletContext(new MockServletContext());
webAppContext.refresh();
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
nestedResolver.setApplicationContext(webAppContext);
nestedResolver.setViewClass(InternalResourceView.class);
viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
viewResolver.afterPropertiesSet();
String viewName = "view";
Locale locale = Locale.ENGLISH;
View result = viewResolver.resolveViewName(viewName, locale);
assertThat(result).as("Invalid view").isNotNull();
}
use of cn.taketoday.web.mock.MockServletContext 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();
}
Aggregations