Java Code Examples for org.springframework.mock.web.MockHttpServletResponse#getHeader()

The following examples show how to use org.springframework.mock.web.MockHttpServletResponse#getHeader() . 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: HeaderResultMatchers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Assert the primary value of the named response header parsed into a date
 * using the preferred date format described in RFC 7231.
 * <p>The {@link ResultMatcher} returned by this method throws an
 * {@link AssertionError} if the response does not contain the specified
 * header, or if the supplied {@code value} does not match the primary value.
 * @since 4.2
 * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
 */
public ResultMatcher dateValue(final String name, final long value) {
	return result -> {
		MockHttpServletResponse response = result.getResponse();
		String headerValue = response.getHeader(name);
		assertNotNull("Response does not contain header '" + name + "'", headerValue);

		HttpHeaders headers = new HttpHeaders();
		headers.setDate("expected", value);
		headers.set("actual", headerValue);

		assertEquals("Response header '" + name + "'='" + headerValue + "' " +
						"does not match expected value '" + headers.getFirst("expected") + "'",
				headers.getFirstDate("expected"), headers.getFirstDate("actual"));
	};
}
 
Example 2
Source File: HeaderResultMatchers.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Assert the primary value of the named response header parsed into a date
 * using the preferred date format described in RFC 7231.
 * <p>The {@link ResultMatcher} returned by this method throws an
 * {@link AssertionError} if the response does not contain the specified
 * header, or if the supplied {@code value} does not match the primary value.
 * @since 4.2
 * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
 */
public ResultMatcher dateValue(final String name, final long value) {
	return result -> {
		MockHttpServletResponse response = result.getResponse();
		String headerValue = response.getHeader(name);
		assertNotNull("Response does not contain header '" + name + "'", headerValue);

		HttpHeaders headers = new HttpHeaders();
		headers.setDate("expected", value);
		headers.set("actual", headerValue);

		assertEquals("Response header '" + name + "'='" + headerValue + "' " +
						"does not match expected value '" + headers.getFirst("expected") + "'",
				headers.getFirstDate("expected"), headers.getFirstDate("actual"));
	};
}
 
Example 3
Source File: HeaderResultMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Assert the primary value of the named response header as a {@code long}.
 * <p>The {@link ResultMatcher} returned by this method throws an
 * {@link AssertionError} if the response does not contain the specified
 * header, or if the supplied {@code value} does not match the primary value.
 */
public ResultMatcher longValue(final String name, final long value) {
	return result -> {
		MockHttpServletResponse response = result.getResponse();
		assertTrue("Response does not contain header '" + name + "'", response.containsHeader(name));
		String headerValue = response.getHeader(name);
		if (headerValue != null) {
			assertEquals("Response header '" + name + "'", value, Long.parseLong(headerValue));
		}
	};
}
 
Example 4
Source File: HeaderResultMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert the primary value of the named response header as a {@code long}.
 * <p>The {@link ResultMatcher} returned by this method throws an
 * {@link AssertionError} if the response does not contain the specified
 * header, or if the supplied {@code value} does not match the primary value.
 */
public ResultMatcher longValue(final String name, final long value) {
	return result -> {
		MockHttpServletResponse response = result.getResponse();
		assertTrue("Response does not contain header '" + name + "'", response.containsHeader(name));
		String headerValue = response.getHeader(name);
		if (headerValue != null) {
			assertEquals("Response header '" + name + "'", value, Long.parseLong(headerValue));
		}
	};
}
 
Example 5
Source File: CsrfPreventionFilterTest.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonModifyingRequestTokenGeneration() throws IOException, ServletException {
  MockHttpServletResponse response = performNonModifyingRequest(nonModifyingRequestUrl, new MockHttpSession());

  String cookieToken = (String) response.getHeader(CSRF_SET_COOKIE_HEADER_NAME);
  String headerToken = (String) response.getHeader(CSRF_HEADER_NAME);

  Assert.assertNotNull(cookieToken);
  Assert.assertNotNull(headerToken);

  assertThat(cookieToken).matches(CSRF_COOKIE_NAME + "=[A-Z0-9]{32}; Path=/camunda; SameSite=Lax");

  Assert.assertEquals("No HTTP Header Token!",false, headerToken.isEmpty());
  assertThat(cookieToken).contains(headerToken);
}
 
Example 6
Source File: CsrfPreventionFilterTest.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsecutiveNonModifyingRequestTokens() throws IOException, ServletException {
  MockHttpSession session = new MockHttpSession();

  // first non-modifying request
  MockHttpServletResponse firstResponse = performNonModifyingRequest(nonModifyingRequestUrl, session);
  // second non-modifying request
  MockHttpServletResponse secondResponse = performNonModifyingRequest(nonModifyingRequestUrl, session);

  String headerToken1 = (String) firstResponse.getHeader(CSRF_HEADER_NAME);
  String headerToken2 = (String) secondResponse.getHeader(CSRF_HEADER_NAME);

  Assert.assertNotNull(headerToken1);
  Assert.assertNull(headerToken2);
}
 
Example 7
Source File: CsrfPreventionFilterTest.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifyingRequestTokenValidation() throws IOException, ServletException {
  MockHttpSession session = new MockHttpSession();

  // first a non-modifying request to obtain a token
  MockHttpServletResponse nonModifyingResponse = performNonModifyingRequest(nonModifyingRequestUrl, session);

  if (!isModifyingFetchRequest) {
    String token = (String) nonModifyingResponse.getHeader(CSRF_HEADER_NAME);
    HttpServletResponse modifyingResponse = performModifyingRequest(token, session);
    Assert.assertEquals(Response.Status.OK.getStatusCode(), modifyingResponse.getStatus());
  }
}
 
Example 8
Source File: CsrfPreventionFilterTest.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonModifyingRequestTokenGenerationWithRootContextPath() throws IOException, ServletException {
  // given
  MockHttpSession session = new MockHttpSession();
  MockHttpServletRequest nonModifyingRequest = new MockHttpServletRequest();
  nonModifyingRequest.setMethod("GET");
  nonModifyingRequest.setSession(session);

  // set root context path in request
  nonModifyingRequest.setRequestURI("/"  + nonModifyingRequestUrl);
  nonModifyingRequest.setContextPath("");

  // when
  MockHttpServletResponse response = new MockHttpServletResponse();
  applyFilter(nonModifyingRequest, response);

  // then
  String cookieToken = (String) response.getHeader(CSRF_SET_COOKIE_HEADER_NAME);
  String headerToken = (String) response.getHeader(CSRF_HEADER_NAME);

  Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());

  Assert.assertNotNull(cookieToken);
  Assert.assertNotNull(headerToken);

  assertThat(cookieToken).matches(CSRF_COOKIE_NAME + "=[A-Z0-9]{32}; Path=/; SameSite=Lax");

  Assert.assertEquals("No HTTP Header Token!",false, headerToken.isEmpty());
  assertThat(cookieToken).contains(headerToken);
}
 
Example 9
Source File: AbstractRepositoryServletTestCase.java    From archiva with Apache License 2.0 4 votes vote down vote up
protected WebResponse getWebResponse( WebRequest webRequest ) //, boolean followRedirect )
    throws Exception
{

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI( webRequest.getUrl().getPath() );
    request.addHeader( "User-Agent", "Apache Archiva unit test" );

    request.setMethod( webRequest.getHttpMethod().name() );

    if ( webRequest.getHttpMethod() == HttpMethod.PUT )
    {
        PutMethodWebRequest putRequest = PutMethodWebRequest.class.cast( webRequest );
        request.setContentType( putRequest.contentType );
        request.setContent( IOUtils.toByteArray( putRequest.inputStream ) );
    }

    if ( webRequest instanceof MkColMethodWebRequest )
    {
        request.setMethod( "MKCOL" );
    }

    final MockHttpServletResponse response = execute( request );

    if ( response.getStatus() == HttpServletResponse.SC_MOVED_PERMANENTLY
        || response.getStatus() == HttpServletResponse.SC_MOVED_TEMPORARILY )
    {
        String location = response.getHeader( "Location" );
        log.debug( "follow redirect to {}", location );
        return getWebResponse( new GetMethodWebRequest( location ) );
    }

    return new WebResponse( null, null, 1 )
    {
        @Override
        public String getContentAsString()
        {
            try
            {
                return response.getContentAsString();
            }
            catch ( UnsupportedEncodingException e )
            {
                throw new RuntimeException( e.getMessage(), e );
            }
        }

        @Override
        public int getStatusCode()
        {
            return response.getStatus();
        }

        @Override
        public String getResponseHeaderValue( String headerName )
        {
            return response.getHeader( headerName );
        }
    };
}