Example usage for org.springframework.mock.web MockHttpServletRequest addHeader

List of usage examples for org.springframework.mock.web MockHttpServletRequest addHeader

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest addHeader.

Prototype

public void addHeader(String name, Object value) 

Source Link

Document

Add an HTTP header entry for the given name.

Usage

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void testWrongPasswordReturnsForbiddenIfIgnoreFailureIsFalse() throws Exception {
    String token = "rod:WRONG_PASSWORD";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
    request.setServletPath("/some_file.html");
    request.setSession(new MockHttpSession());
    assertThat(filter.isIgnoreFailure()).isFalse();
    final MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);

    // Test - the filter chain will not be invoked, as we get a 401 forbidden response
    verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    assertThat(response.getStatus()).isEqualTo(401);
}

From source file:org.springframework.security.web.authentication.www.BasicAuthenticationFilterTests.java

@Test
public void skippedOnErrorDispatch() throws Exception {

    String token = "bad:credentials";
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
    request.setServletPath("/some_file.html");
    request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error");
    MockHttpServletResponse response = new MockHttpServletResponse();

    FilterChain chain = mock(FilterChain.class);

    filter.doFilter(request, response, chain);

    assertThat(response.getStatus()).isEqualTo(200);
}

From source file:org.unitedinternet.cosmo.dav.impl.StandardDavRequestTest.java

/**
 * Tests bad depth./* w  ww  . j  ava 2s .  co m*/
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testBadDepth() throws Exception {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest();
    httpRequest.addHeader("Depth", "bad value");
    StandardDavRequest request = new StandardDavRequest(httpRequest, testHelper.getResourceLocatorFactory(),
            testHelper.getEntityFactory());

    try {
        int depth = request.getDepth();
        Assert.fail("got bad depth " + depth);
    } catch (IllegalArgumentException e) {
        // expected
    }
}