use of cn.taketoday.web.RequestContext in project today-infrastructure by TAKETODAY.
the class InternalResourceViewTests method includeOnAttribute.
@Test
public void includeOnAttribute() throws Exception {
given(request.getRequestDispatcher(url)).willReturn(new MockRequestDispatcher(url));
given(request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)).willReturn("somepath");
RequestContext context = new ServletRequestContext(null, request, response);
view.setUrl(url);
// Can now try multiple tests
view.render(model, context);
assertThat(response.getIncludedUrl()).isEqualTo(url);
model.forEach((key, value) -> verify(request).setAttribute(key, value));
}
use of cn.taketoday.web.RequestContext 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.RequestContext in project today-infrastructure by TAKETODAY.
the class XlsViewTests method testXls.
@Test
@SuppressWarnings("resource")
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, RequestContext request) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), requestContext);
Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of cn.taketoday.web.RequestContext in project today-infrastructure by TAKETODAY.
the class XlsViewTests method testXlsxStreamingView.
@Test
@SuppressWarnings("resource")
public void testXlsxStreamingView() throws Exception {
View excelView = new AbstractXlsxStreamingView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, RequestContext request) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), requestContext);
Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of cn.taketoday.web.RequestContext in project today-infrastructure by TAKETODAY.
the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
this.wac.refresh();
vr.setApplicationContext(this.wac);
vr.setContentType("myContentType");
vr.setRequestContextAttribute("rc");
View view = vr.resolveViewName("example1", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
assertThat(view.getContentType()).as("Correct textContentType").isEqualTo("myContentType");
view = vr.resolveViewName("example2", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
assertThat(view.getContentType()).as("Correct textContentType").isEqualTo("myContentType");
this.wac.registerSingleton(LocaleResolver.BEAN_NAME, new AcceptHeaderLocaleResolver());
Map<String, Object> model = new HashMap<>();
TestBean tb = new TestBean();
model.put("tb", tb);
view.render(model, this.requestContext);
assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
boolean condition = this.request.getAttribute("rc") instanceof RequestContext;
assertThat(condition).as("Correct rc attribute").isTrue();
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
assertThat(((RedirectView) view).getApplicationContext()).as("View not initialized as bean").isSameAs(this.wac);
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Aggregations