use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework by TAKETODAY.
the class FreeMarkerViewTests method keepExistingContentType.
@Test
public void keepExistingContentType() throws Exception {
FreeMarkerView fv = new FreeMarkerView();
WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
MockServletContext sc = new MockServletContext();
Map<String, FreeMarkerConfig> configs = new HashMap<>();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
configs.put("configurer", configurer);
given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
given(wac.getServletContext()).willReturn(sc);
given(wac.getBean(LocaleResolver.BEAN_NAME)).willReturn(new AcceptHeaderLocaleResolver());
given(wac.getBean(LocaleResolver.BEAN_NAME, LocaleResolver.class)).willReturn(new AcceptHeaderLocaleResolver());
fv.setUrl("templateName");
fv.setApplicationContext(wac);
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.US);
HttpServletResponse response = new MockHttpServletResponse();
response.setContentType("myContentType");
Map<String, Object> model = new HashMap<>();
model.put("myattr", "myvalue");
ServletRequestContext context = new ServletRequestContext(wac, request, response);
fv.render(model, context);
assertThat(response.getContentType()).isEqualTo("myContentType");
}
use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-framework 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");
}
use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-infrastructure by TAKETODAY.
the class WebMvcAutoConfiguration method localeResolver.
@Component
@ConditionalOnMissingBean(name = LocaleResolver.BEAN_NAME)
public LocaleResolver localeResolver() {
if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.webProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.webProperties.getLocale());
return localeResolver;
}
use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-infrastructure by TAKETODAY.
the class ViewResolverTests method internalResourceViewResolverWithAttributes.
@Test
public void internalResourceViewResolverWithAttributes() throws Exception {
this.wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
Properties props = new Properties();
props.setProperty("key1", "value1");
vr.setAttributes(props);
Map<String, Object> map = new HashMap<>();
map.put("key2", 2);
vr.setAttributesMap(map);
vr.setApplicationContext(this.wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example1");
Map<String, Object> attributes = ((InternalResourceView) view).getStaticAttributes();
assertThat(attributes.get("key1")).isEqualTo("value1");
assertThat(attributes.get("key2")).isEqualTo(2);
view = vr.resolveViewName("example2", Locale.getDefault());
assertThat(view).isInstanceOf(JstlView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("example2");
attributes = ((InternalResourceView) view).getStaticAttributes();
assertThat(attributes.get("key1")).isEqualTo("value1");
assertThat(attributes.get("key2")).isEqualTo(2);
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();
assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
assertThat(this.request.getAttribute("key1")).isEqualTo("value1");
assertThat(this.request.getAttribute("key2")).isEqualTo(2);
}
use of cn.taketoday.web.i18n.AcceptHeaderLocaleResolver in project today-infrastructure by TAKETODAY.
the class ViewResolverTests method internalResourceViewResolverWithContextBeans.
@Test
public void internalResourceViewResolverWithContextBeans() throws Exception {
this.wac.registerSingleton("myBean", TestBean.class);
this.wac.registerSingleton("myBean2", TestBean.class);
this.wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
Properties props = new Properties();
props.setProperty("key1", "value1");
vr.setAttributes(props);
Map<String, Object> map = new HashMap<>();
map.put("key2", 2);
vr.setAttributesMap(map);
vr.setExposeContextBeansAsAttributes(true);
vr.setApplicationContext(this.wac);
this.wac.registerSingleton(LocaleResolver.BEAN_NAME, new AcceptHeaderLocaleResolver());
View view = vr.resolveViewName("example1", Locale.getDefault());
view.render(new HashMap<String, Object>(), new ServletRequestContext(null, request, response));
}
Aggregations