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

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#addUserRole() . 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: DispatcherServletTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 2
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void anotherLocaleRequest() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
	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);
	assertTrue(request.getAttribute("test3") != null);
	assertTrue(request.getAttribute("test3x") != null);
	assertTrue(request.getAttribute("test3y") != null);
	assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
}
 
Example 3
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 4
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void anotherLocaleRequest() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
	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);
	assertTrue(request.getAttribute("test3") != null);
	assertTrue(request.getAttribute("test3x") != null);
	assertTrue(request.getAttribute("test3y") != null);
	assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
}
 
Example 8
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void anotherLocaleRequest() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
	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);
	assertTrue(request.getAttribute("test3") != null);
	assertTrue(request.getAttribute("test3x") != null);
	assertTrue(request.getAttribute("test3y") != null);
	assertEquals("Wed, 01 Apr 2015 00:00:01 GMT", response.getHeader("Last-Modified"));
}
 
Example 9
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleMappingExceptionResolverWithSpecificHandler1() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
	request.addPreferredLocale(Locale.CANADA);
	request.addUserRole("role1");
	request.addParameter("access", "yes");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("forwarded to failed", "failed2.jsp", response.getForwardedUrl());
	assertTrue("Exception exposed", request.getAttribute("exception") instanceof IllegalAccessException);
}
 
Example 11
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void simpleMappingExceptionResolverWithSpecificHandler2() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
	request.addPreferredLocale(Locale.CANADA);
	request.addUserRole("role1");
	request.addParameter("servlet", "yes");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("forwarded to failed", "failed3.jsp", response.getForwardedUrl());
	assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException);
}
 
Example 13
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void parameterizableViewController() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
	request.addUserRole("role1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
}
 
Example 14
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 15
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 16
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 17
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void simpleMappingExceptionResolverWithSpecificHandler2() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
	request.addPreferredLocale(Locale.CANADA);
	request.addUserRole("role1");
	request.addParameter("servlet", "yes");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertEquals(200, response.getStatus());
	assertEquals("forwarded to failed", "failed3.jsp", response.getForwardedUrl());
	assertTrue("Exception exposed", request.getAttribute("exception") instanceof ServletException);
}
 
Example 18
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void parameterizableViewController() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
	request.addUserRole("role1");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertTrue("forwarded to form", "myform.jsp".equals(response.getForwardedUrl()));
}
 
Example 19
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 20
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handlerInterceptorSuppressesView() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/view.do");
	request.addUserRole("role1");
	request.addParameter("noView", "true");
	MockHttpServletResponse response = new MockHttpServletResponse();
	complexDispatcherServlet.service(request, response);
	assertTrue("Not forwarded", response.getForwardedUrl() == null);
}