Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#addPreferredLocale()
The following examples show how to use
org.springframework.mock.web.test.MockHttpServletRequest#addPreferredLocale() .
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 |
@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 2
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void themeChangeInterceptor2() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("theme", "mytheme"); request.addParameter("theme2", "theme"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); } catch (ServletException ex) { fail("Should not have thrown ServletException: " + ex.getMessage()); } }
Example 3
Source File: DispatcherServletTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void modelAndViewDefiningException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("fail", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); assertEquals(200, response.getStatus()); assertTrue("forwarded to failed", "failed1.jsp".equals(response.getForwardedUrl())); } catch (ServletException ex) { fail("Should not have thrown ServletException: " + ex.getMessage()); } }
Example 4
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void modelAndViewDefiningException() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("fail", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); try { complexDispatcherServlet.service(request, response); assertEquals(200, response.getStatus()); assertTrue("forwarded to failed", "failed1.jsp".equals(response.getForwardedUrl())); } catch (ServletException ex) { fail("Should not have thrown ServletException: " + ex.getMessage()); } }
Example 5
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void handlerInterceptorAbort() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addParameter("abort", "true"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); assertTrue(request.getAttribute("test1") != null); assertTrue(request.getAttribute("test1x") != null); assertTrue(request.getAttribute("test1y") == null); assertTrue(request.getAttribute("test2") == null); assertTrue(request.getAttribute("test2x") == null); assertTrue(request.getAttribute("test2y") == null); }
Example 6
Source File: CookieLocaleResolverTests.java From spring-analysis-note with MIT License | 6 votes |
@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 7
Source File: GroovyMarkupViewTests.java From java-technology-stack with MIT License | 5 votes |
private MockHttpServletResponse renderViewWithModel(String viewUrl, Map<String, Object> model, Locale locale) throws Exception { GroovyMarkupView view = createViewWithUrl(viewUrl); MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(locale); LocaleContextHolder.setLocale(locale); view.renderMergedTemplateModel(model, request, response); return response; }
Example 8
Source File: CookieLocaleResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@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 9
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void existingMultipartRequestButWrapped() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); MockHttpServletResponse response = new MockHttpServletResponse(); ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext() .getBean("multipartResolver"); MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request); complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response); multipartResolver.cleanupMultipart(multipartRequest); assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)); assertNotNull(request.getAttribute("cleanedUp")); }
Example 10
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void multipartResolutionFailed() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.setAttribute("fail", Boolean.TRUE); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue("forwarded to failed", "failed0.jsp".equals(response.getForwardedUrl())); assertEquals(200, response.getStatus()); assertTrue("correct exception", request.getAttribute( SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE) instanceof MaxUploadSizeExceededException); }
Example 11
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void localeRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); MockHttpServletResponse response = new MockHttpServletResponse(); simpleDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified")); }
Example 12
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void localeChangeInterceptor2() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.GERMAN); request.addUserRole("role2"); request.addParameter("locale", "en"); request.addParameter("locale2", "en_CA"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); }
Example 13
Source File: CookieLocaleResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testResolveLocaleWithoutCookieAndDefaultLocale() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.TAIWAN); CookieLocaleResolver resolver = new CookieLocaleResolver(); resolver.setDefaultLocale(Locale.GERMAN); Locale loc = resolver.resolveLocale(request); assertEquals(Locale.GERMAN, loc); }
Example 14
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void simpleMappingExceptionResolverWithAllHandlers1() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); request.addParameter("access", "yes"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(500, response.getStatus()); assertEquals("forwarded to failed", "failed1.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException); }
Example 15
Source File: CookieLocaleResolverTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testResolveLocaleWithoutCookie() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.TAIWAN); CookieLocaleResolver resolver = new CookieLocaleResolver(); Locale loc = resolver.resolveLocale(request); assertEquals(request.getLocale(), loc); }
Example 16
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void localeRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.CANADA); MockHttpServletResponse response = new MockHttpServletResponse(); simpleDispatcherServlet.service(request, response); assertTrue("Not forwarded", response.getForwardedUrl() == null); assertEquals("Wed, 01 Apr 2015 00:00:00 GMT", response.getHeader("Last-Modified")); }
Example 17
Source File: DispatcherServletTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void localeChangeInterceptor1() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do"); request.addPreferredLocale(Locale.GERMAN); request.addUserRole("role2"); request.addParameter("locale", "en"); MockHttpServletResponse response = new MockHttpServletResponse(); complexDispatcherServlet.service(request, response); assertEquals(200, response.getStatus()); assertEquals("forwarded to failed", "failed0.jsp", response.getForwardedUrl()); assertTrue("Exception exposed", request.getAttribute("exception").getClass().equals(ServletException.class)); }
Example 18
Source File: DispatcherServletTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void existingMultipartRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def"); request.addPreferredLocale(Locale.CANADA); request.addUserRole("role1"); MockHttpServletResponse response = new MockHttpServletResponse(); ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext() .getBean("multipartResolver"); MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request); complexDispatcherServlet.service(multipartRequest, response); multipartResolver.cleanupMultipart(multipartRequest); assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)); assertNotNull(request.getAttribute("cleanedUp")); }
Example 19
Source File: CookieLocaleResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testResolveLocaleWithoutCookie() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.TAIWAN); CookieLocaleResolver resolver = new CookieLocaleResolver(); Locale loc = resolver.resolveLocale(request); assertEquals(request.getLocale(), loc); }
Example 20
Source File: SessionLocaleResolverTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testResolveLocaleWithoutSessionAndDefaultLocale() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.TAIWAN); SessionLocaleResolver resolver = new SessionLocaleResolver(); resolver.setDefaultLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, resolver.resolveLocale(request)); }