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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#getAttribute() . 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetLocaleContextToNull() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setLocaleContext(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.TAIWAN, locale);
	TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
	assertNull(timeZone);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 2
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetLocaleToNullWithDefault() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.CANADA_FRENCH);
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.CANADA_FRENCH, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 3
Source File: RequestMappingInfoHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void uriTemplateVariables() {
	PatternsRequestCondition patterns = new PatternsRequestCondition("/{path1}/{path2}");
	RequestMappingInfo key = new RequestMappingInfo(patterns, null, null, null, null, null, null);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
	String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
	this.handlerMapping.handleMatch(key, lookupPath, request);

	@SuppressWarnings("unchecked")
	Map<String, String> uriVariables =
		(Map<String, String>) request.getAttribute(
				HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);

	assertNotNull(uriVariables);
	assertEquals("1", uriVariables.get("path1"));
	assertEquals("2", uriVariables.get("path2"));
}
 
Example 4
Source File: RequestScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception {
	String name = "requestScopedProxy";
	ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
	// assertTrue(AopUtils.isJdkDynamicProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 5
Source File: RequestScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetFromScope() throws Exception {
	String name = "requestScopedObject";
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isCglibProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 6
Source File: RequestScopedProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception {
	String name = "requestScopedProxy";
	ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isJdkDynamicProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 7
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleToNullWithDefault() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.CANADA_FRENCH);
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.CANADA_FRENCH, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 8
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetLocaleToNull() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.TAIWAN, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 9
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleToNull() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.TAIWAN, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 10
Source File: RequestScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception {
	String name = "requestScopedProxy";
	ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
	// assertTrue(AopUtils.isJdkDynamicProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 11
Source File: RequestScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetFromScope() throws Exception {
	String name = "requestScopedObject";
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isCglibProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 12
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleContextToNullWithDefault() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.CANADA_FRENCH);
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
	resolver.setLocaleContext(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.CANADA_FRENCH, locale);
	TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(TimeZone.getTimeZone("GMT+1"), timeZone);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 13
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleToNullWithDefault() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.CANADA_FRENCH);
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.CANADA_FRENCH, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 14
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleContextToNull() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setLocaleContext(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.TAIWAN, locale);
	TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
	assertNull(timeZone);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 15
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSetLocaleToNull() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setLocale(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.TAIWAN, locale);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 16
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetLocaleContextToNullWithDefault() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
	request.setCookies(cookie);
	MockHttpServletResponse response = new MockHttpServletResponse();

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setDefaultLocale(Locale.CANADA_FRENCH);
	resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
	resolver.setLocaleContext(request, response, null);
	Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(Locale.CANADA_FRENCH, locale);
	TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
	assertEquals(TimeZone.getTimeZone("GMT+1"), timeZone);

	Cookie[] cookies = response.getCookies();
	assertEquals(1, cookies.length);
	Cookie localeCookie = cookies[0];
	assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
	assertEquals("", localeCookie.getValue());
}
 
Example 17
Source File: RequestMappingInfoHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void handleMatchUriTemplateVariables() {
	RequestMappingInfo key = RequestMappingInfo.paths("/{path1}/{path2}").build();
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/1/2");
	String lookupPath = new UrlPathHelper().getLookupPathForRequest(request);
	this.handlerMapping.handleMatch(key, lookupPath, request);

	String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
	Map<String, String> uriVariables = (Map<String, String>) request.getAttribute(name);

	assertNotNull(uriVariables);
	assertEquals("1", uriVariables.get("path1"));
	assertEquals("2", uriVariables.get("path2"));
}
 
Example 18
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void setRequestScopedAttribute() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
	Object value = request.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 19
Source File: ServletRequestAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void setRequestScopedAttribute() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
	Object value = request.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 20
Source File: ServletRequestAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void setRequestScopedAttribute() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_REQUEST);
	Object value = request.getAttribute(KEY);
	assertSame(VALUE, value);
}