Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#setAttribute()

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.GERMAN);
	request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
	Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(Locale.GERMAN, loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 2
Source File: RequestContextListenerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThread() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 3
Source File: FreeMarkerMacroTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	ServletContext sc = new MockServletContext();
	wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);

	// final Template expectedTemplate = new Template();
	fc = new FreeMarkerConfigurer();
	fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
	fc.setServletContext(sc);
	fc.afterPropertiesSet();

	wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example 4
Source File: TilesViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockServletContext servletContext = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(servletContext);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	response = new MockHttpServletResponse();

	renderer = mock(Renderer.class);

	view = new TilesView();
	view.setServletContext(servletContext);
	view.setRenderer(renderer);
	view.setUrl(VIEW_PATH);
	view.afterPropertiesSet();
}
 
Example 5
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
	assertNull(request.getAttribute("command"));
}
 
Example 6
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRestore() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	TestBean command = new TestBean();
	request.setAttribute("command", command);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
 
Example 7
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testResolveLocaleContextWithInvalidLocaleOnErrorDispatch() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.GERMAN);
	request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
	Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(Locale.GERMAN, loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 8
Source File: RequestContextListenerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestContextListenerWithSameThreadAndAttributesGone() {
	RequestContextListener listener = new RequestContextListener();
	MockServletContext context = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(context);
	request.setAttribute("test", "value");

	assertNull(RequestContextHolder.getRequestAttributes());
	listener.requestInitialized(new ServletRequestEvent(context, request));
	assertNotNull(RequestContextHolder.getRequestAttributes());
	assertEquals("value",
			RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
	MockRunnable runnable = new MockRunnable();
	RequestContextHolder.getRequestAttributes().registerDestructionCallback(
			"test", runnable, RequestAttributes.SCOPE_REQUEST);

	request.clearAttributes();
	listener.requestDestroyed(new ServletRequestEvent(context, request));
	assertNull(RequestContextHolder.getRequestAttributes());
	assertTrue(runnable.wasExecuted());
}
 
Example 9
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
	MockHttpServletResponse response = new MockHttpServletResponse();

	request.setAttribute("test1", "value1");
	request.setAttribute("test2", "value2");
	WebApplicationContext wac = new StaticWebApplicationContext();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
	simpleDispatcherServlet.service(request, response);

	assertEquals("value1", request.getAttribute("test1"));
	assertEquals("value2", request.getAttribute("test2"));
	assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
	assertNull(request.getAttribute("command"));
}
 
Example 10
Source File: OptionsTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	TestBean bean = new TestBean();
	bean.setName("foo");
	bean.setCountry("UK");
	bean.setMyFloat(new Float("12.34"));
	request.setAttribute(COMMAND_NAME, bean);

	List floats = new ArrayList();
	floats.add(new Float("12.30"));
	floats.add(new Float("12.31"));
	floats.add(new Float("12.32"));
	floats.add(new Float("12.33"));
	floats.add(new Float("12.34"));
	floats.add(new Float("12.35"));

	request.setAttribute("floats", floats);
}
 
Example 11
Source File: BaseViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void dynamicModelOverridesPathVariables() throws Exception {
	WebApplicationContext wac = mock(WebApplicationContext.class);
	given(wac.getServletContext()).willReturn(new MockServletContext());

	TestView tv = new TestView(wac);
	tv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> pathVars = new HashMap<>();
	pathVars.put("one", "bar");
	pathVars.put("something", "else");
	request.setAttribute(View.PATH_VARIABLES, pathVars);

	Map<String, Object> model = new HashMap<>();
	model.put("one", new HashMap<>());
	model.put("two", new Object());

	tv.render(model, request, response);

	checkContainsAll(model, tv.model);
	assertEquals(3, tv.model.size());
	assertEquals("else", tv.model.get("something"));
	assertTrue(tv.initialized);
}
 
Example 12
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void keepExistingContentType() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<String, FreeMarkerConfig>();
	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);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();
	response.setContentType("myContentType");

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals("myContentType", response.getContentType());
}
 
Example 13
Source File: BaseViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void dynamicModelOverridesPathVariables() throws Exception {
	WebApplicationContext wac = mock(WebApplicationContext.class);
	given(wac.getServletContext()).willReturn(new MockServletContext());

	TestView tv = new TestView(wac);
	tv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> pathVars = new HashMap<>();
	pathVars.put("one", "bar");
	pathVars.put("something", "else");
	request.setAttribute(View.PATH_VARIABLES, pathVars);

	Map<String, Object> model = new HashMap<>();
	model.put("one", new HashMap<>());
	model.put("two", new Object());

	tv.render(model, request, response);

	checkContainsAll(model, tv.model);
	assertEquals(3, tv.model.size());
	assertEquals("else", tv.model.get("something"));
	assertTrue(tv.initialized);
}
 
Example 14
Source File: ResourceHandlerRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mapPathToLocation() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setMethod("GET");
	request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css");

	ResourceHttpRequestHandler handler = getHandler("/resources/**");
	handler.handleRequest(request, this.response);

	assertEquals("test stylesheet content", this.response.getContentAsString());
}
 
Example 15
Source File: UrlFilenameViewControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void withFlashAttributes() throws Exception {
	UrlFilenameViewController ctrl = new UrlFilenameViewController();
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/index");
	request.setAttribute(DispatcherServlet.INPUT_FLASH_MAP_ATTRIBUTE, new ModelMap("name", "value"));
	MockHttpServletResponse response = new MockHttpServletResponse();
	ModelAndView mv = ctrl.handleRequest(request, response);
	assertEquals("index", mv.getViewName());
	assertEquals(1, mv.getModel().size());
	assertEquals("value", mv.getModel().get("name"));
}
 
Example 16
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResolveLocaleContextWithInvalidTimeZoneOnErrorDispatch() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, new ServletException());
	Cookie cookie = new Cookie("LanguageKoekje", "nl X-MT");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+2"));
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 17
Source File: BaseViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void dynamicModelOverridesPathVariables() throws Exception {
	WebApplicationContext wac = mock(WebApplicationContext.class);
	given(wac.getServletContext()).willReturn(new MockServletContext());

	TestView tv = new TestView(wac);
	tv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	MockHttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> pathVars = new HashMap<String, Object>();
	pathVars.put("one", "bar");
	pathVars.put("something", "else");
	request.setAttribute(View.PATH_VARIABLES, pathVars);

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("one", new HashMap<Object, Object>());
	model.put("two", new Object());

	tv.render(model, request, response);

	checkContainsAll(model, tv.model);
	assertEquals(3, tv.model.size());
	assertEquals("else", tv.model.get("something"));
	assertTrue(tv.initialized);
}
 
Example 18
Source File: RedirectViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void attributeStatusCodeHttp11() throws Exception {
	RedirectView rv = new RedirectView();
	rv.setUrl("http://url.somewhere.com");
	rv.setHttp10Compatible(false);
	MockHttpServletRequest request = createRequest();
	request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.CREATED);
	MockHttpServletResponse response = new MockHttpServletResponse();
	rv.render(new HashMap<String, Object>(), request, response);
	assertEquals(201, response.getStatus());
	assertEquals("http://url.somewhere.com", response.getHeader("Location"));
}
 
Example 19
Source File: FreeMarkerViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void keepExistingContentType() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();
	response.setContentType("myContentType");

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals("myContentType", response.getContentType());
}
 
Example 20
Source File: AbstractFormTagTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	request.setAttribute(COMMAND_NAME, createTestBean());
}