Spring Framework MockHttpServletResponse - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework MockHttpServletResponse class.

This code example shows how to use the following methods:getStatus
    @Test 
    public void requestThatIsMatchedByDefaultInterceptorIsAllowed() throws Exception { 
        MockHttpServletRequest request = new MockHttpServletRequest(); 
        request.setServletPath("/somefile.html"); 
        request.setSession(createAuthenticatedSession("ROLE_0", "ROLE_1", "ROLE_2")); 
        MockHttpServletResponse response = new MockHttpServletResponse(); 
        fcp.doFilter(request, response, new MockFilterChain()); 
        assertEquals(200, response.getStatus()); 
    } 
 
    @Test 
    public void securedUrlAccessIsRejectedWithoutRequiredRole() throws Exception { 
        MockHttpServletRequest request = new MockHttpServletRequest(); 
        request.setServletPath("/secure/somefile.html"); 
        request.setSession(createAuthenticatedSession("ROLE_0")); 
        MockHttpServletResponse response = new MockHttpServletResponse(); 
        fcp.doFilter(request, response, new MockFilterChain()); 
        assertEquals(403, response.getStatus()); 
    } 
 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
 
	@Test(expected = BadCredentialsException.class)
	public void testFailedAuthentication() throws Exception {
		filter.setAuthenticationManager(authenticationManager);
		filter.afterPropertiesSet();
		filter.attemptAuthentication(new MockHttpServletRequest(),
				new MockHttpServletResponse());
	} 
 
	@Test
	public void testAuthentication() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.addParameter("client_id", "foo");
		filter.setAuthenticationManager(authenticationManager);
		filter.afterPropertiesSet();
		Authentication authentication = new UsernamePasswordAuthenticationToken(
				"foo", "", 
				AuthorityUtils.commaSeparatedStringToAuthorityList("CLIENT")); 
		Mockito.when( 
				authenticationManager.authenticate(Mockito
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
 */ 
public class WhitelabelErrorEndpointTests { 
	 
	private WhitelabelErrorEndpoint endpoint = new WhitelabelErrorEndpoint(); 
	private MockHttpServletRequest request = new MockHttpServletRequest(); 
	private MockHttpServletResponse response = new MockHttpServletResponse(); 
 
	@Test 
	public void testErrorPage() throws Exception { 
		request.setContextPath("/foo"); 
		request.setAttribute("error", new InvalidClientException("FOO")); 
		ModelAndView result = endpoint.handleError(request); 
		result.getView().render(result.getModel(), request , response); 
		String content = response.getContentAsString(); 
		assertTrue("Wrong content: " + content, content.contains("OAuth Error")); 
		assertTrue("Wrong content: " + content, content.contains("invalid_client")); 
	} 
 
} 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getStatus
    @Test 
    public void securedFilterChainCannotBeBypassedByAddingPathParameters() throws Exception { 
        MockHttpServletRequest request = new MockHttpServletRequest(); 
        request.setPathInfo("/secured;x=y/admin.html"); 
        request.setSession(createAuthenticatedSession("ROLE_USER")); 
        MockHttpServletResponse response = new MockHttpServletResponse(); 
        fcp.doFilter(request, response, new MockFilterChain()); 
        assertEquals(403, response.getStatus()); 
    } 
 
    @Test 
    public void adminFilePatternCannotBeBypassedByAddingPathParameters() throws Exception { 
        MockHttpServletRequest request = new MockHttpServletRequest(); 
        request.setServletPath("/secured/admin.html;x=user.html"); 
        request.setSession(createAuthenticatedSession("ROLE_USER")); 
        MockHttpServletResponse response = new MockHttpServletResponse(); 
        fcp.doFilter(request, response, new MockFilterChain()); 
        assertEquals(403, response.getStatus()); 
 
        // Try with pathInfo 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getContentAsString, getHeader, getHeaders, getStatus
        httpResponse.withStatusCode(HttpStatusCode.OK_200.code()); 
        httpResponse.withBody("somebody"); 
        httpResponse.withHeaders(new Header("headerName1", "headerValue1"), new Header("headerName2", "headerValue2")); 
        httpResponse.withCookies(new Cookie("cookieName1", "cookieValue1"), new Cookie("cookieName2", "cookieValue2")); 
        // - an HttpServletResponse 
        MockHttpServletResponse httpServletResponse = new MockHttpServletResponse(); 
 
        // when 
        new MockServerToHttpServletResponseMapper().mapMockServerResponseToHttpServletResponse(httpResponse, httpServletResponse); 
 
        // then 
        assertEquals(HttpStatusCode.OK_200.code(), httpServletResponse.getStatus()); 
        assertEquals("somebody", httpServletResponse.getContentAsString()); 
        assertEquals("headerValue1", httpServletResponse.getHeader("headerName1")); 
        assertEquals("headerValue2", httpServletResponse.getHeader("headerName2")); 
        assertEquals(Arrays.asList( 
                "cookieName1=cookieValue1", 
                "cookieName2=cookieValue2" 
        ), httpServletResponse.getHeaders("Set-Cookie")); 
    } 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
        filter = new LogoutFilter("/success", new SecurityContextLogoutHandler());
    } 
 
    public void testRequiresLogoutUrlWorksWithPathParams() { 
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
 
        request.setRequestURI("/j_spring_security_logout;someparam=blah?otherparam=blah");
 
        assertTrue(filter.requiresLogout(request, response));
    } 
 
    public void testRequiresLogoutUrlWorksWithQueryParams() { 
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setContextPath("/context");
        MockHttpServletResponse response = new MockHttpServletResponse();
 
        request.setRequestURI("/context/j_spring_security_logout?param=blah");
 
        assertTrue(filter.requiresLogout(request, response));
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
 
	private OAuth2AccessDeniedHandler handler = new OAuth2AccessDeniedHandler();
 
	private MockHttpServletRequest request = new MockHttpServletRequest();
 
	private MockHttpServletResponse response = new MockHttpServletResponse();
 
	@Test
	public void testHandleWithJson() throws Exception {
		request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
		handler.handle(request, response, new AccessDeniedException("Bad"));
		assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
		assertEquals(MediaType.APPLICATION_JSON_VALUE, response.getContentType());
		assertEquals(null, response.getErrorMessage());
	} 
 
} 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
		filter.setRestTemplate(restTemplate);
		Set<String> scopes = new HashSet<String>();
		scopes.addAll(Arrays.asList("read", "write"));
		OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("client", false, scopes);
		this.authentication = new OAuth2Authentication(storedOAuth2Request, null);
		filter.successfulAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse(), null, authentication);
		Mockito.verify(restTemplate, Mockito.times(1)).getAccessToken();
	} 
 
	@Test
	public void testUnsuccessfulAuthentication() throws IOException, ServletException {
		try { 
			filter.unsuccessfulAuthentication(null, null, new AccessTokenRequiredException("testing", null));
			fail("AccessTokenRedirectException must be thrown"); 
		} 
		catch (AccessTokenRequiredException ex) {
 
		} 
	} 
} 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:getStatus
		// datasetId=null 
		// htmlView= null 
		// verbose = null		 
		MockHttpServletRequest request = new MockHttpServletRequest("GET", "/thredds/catalog/NCOF/POLCOMS/IRISH_SEA/catalog.html");
		request.setServletPath("/NCOF/POLCOMS/IRISH_SEA/catalog.html");
        MockHttpServletResponse response = new MockHttpServletResponse();
		 
        ModelAndView mv =anyHtmlController.handleHtmlRequest(request, response);
        assertNull( mv );        
        assertEquals(200, response.getStatus() ); 
	}	 
 
 
	public void subsetCommandTest() throws Exception{
		 
		// SUBSET COMMAND REQUEST  
		// setting up the request with default values: 
		// command =null 
		// datasetId=NCOF/POLCOMS/IRISH_SEA  
		// htmlView= null 
10
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    private XContentTypeOptionsHeaderWriter writer;
 
    @Before 
    public void setup() { 
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        writer = new XContentTypeOptionsHeaderWriter();
    } 
 
    @Test
    public void writeHeaders() { 
        writer.writeHeaders(request, response);
 
        assertThat(response.getHeaderNames().size()).isEqualTo(1);
        assertThat(response.getHeaderValues("X-Content-Type-Options")).isEqualTo(Arrays.asList("nosniff"));
    } 
} 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java