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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setCookies() . 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 java-technology-stack 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 2
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testResolveLocaleContextWithInvalidLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "++ GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	try {
		resolver.resolveLocaleContext(request);
		fail("Should have thrown IllegalStateException");
	}
	catch (IllegalStateException ex) {
		assertTrue(ex.getMessage().contains("LanguageKoekje"));
		assertTrue(ex.getMessage().contains("++ GMT+1"));
	}
}
 
Example 3
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 4
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 5
Source File: CookieLocaleResolverTests.java    From java-technology-stack 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 6
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 7
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 8
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveLocaleWithCookieWithoutLocale() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();

	Locale loc = resolver.resolveLocale(request);
	assertEquals(request.getLocale(), loc);
}
 
Example 9
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveLocaleContext() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	// yup, koekje is the Dutch name for Cookie ;-)
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 10
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 11
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResolveLocaleWithCookieWithoutLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();

	Locale loc = resolver.resolveLocale(request);
	assertEquals(request.getLocale(), loc);
}
 
Example 12
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResolveLocaleContextWithCookieWithoutLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();

	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals(request.getLocale(), loc.getLocale());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 13
Source File: CookieLocaleResolverTests.java    From java-technology-stack 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 14
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void bindingCookieValue() throws ServletException, IOException {
	initServlet(BindingCookieValueController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
	request.setCookies(new Cookie("date", "2008-11-18"));
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("test-2008", response.getContentAsString());
}
 
Example 15
Source File: CookieLocaleResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResolveLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	Locale loc = resolver.resolveLocale(request);
	assertEquals("nl", loc.getLanguage());
}
 
Example 16
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResolveLocaleWithCookieWithoutLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.TAIWAN);
	Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, "");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();

	Locale loc = resolver.resolveLocale(request);
	assertEquals(request.getLocale(), loc);
}
 
Example 17
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void bindingCookieValue() throws Exception {
	initServletWithControllers(BindingCookieValueController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
	request.setCookies(new Cookie("date", "2008-11-18"));
	MockHttpServletResponse response = new MockHttpServletResponse();
	getServlet().service(request, response);
	assertEquals("test-2008", response.getContentAsString());
}
 
Example 18
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResolveLocale() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	Locale loc = resolver.resolveLocale(request);
	assertEquals("nl", loc.getLanguage());
}
 
Example 19
Source File: CookieLocaleResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResolveLocaleContext() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
 
Example 20
Source File: CookieLocaleResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveLocaleContextWithTimeZone() {
	MockHttpServletRequest request = new MockHttpServletRequest();
	Cookie cookie = new Cookie("LanguageKoekje", "nl GMT+1");
	request.setCookies(cookie);

	CookieLocaleResolver resolver = new CookieLocaleResolver();
	// yup, koekje is the Dutch name for Cookie ;-)
	resolver.setCookieName("LanguageKoekje");
	LocaleContext loc = resolver.resolveLocaleContext(request);
	assertEquals("nl", loc.getLocale().getLanguage());
	assertTrue(loc instanceof TimeZoneAwareLocaleContext);
	assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) loc).getTimeZone());
}