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

The following examples show how to use org.springframework.mock.web.MockHttpServletResponse#getHeaders() . 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: AuthenticationControllerTest.java    From auth0-java-mvc-common with MIT License 6 votes vote down vote up
@Test
public void shouldSetLaxCookiesAndNoLegacyCookieWhenCodeFlow() {
    MockHttpServletResponse response = new MockHttpServletResponse();

    AuthenticationController controller = AuthenticationController.newBuilder("domain", "clientId", "clientSecret")
            .withResponseType("code")
            .build();

    controller.buildAuthorizeUrl(new MockHttpServletRequest(), response, "https://redirect.uri/here")
            .withState("state")
            .build();

    List<String> headers = response.getHeaders("Set-Cookie");

    assertThat(headers.size(), is(1));
    assertThat(headers, everyItem(is("com.auth0.state=state; HttpOnly; Max-Age=600; SameSite=Lax")));
}
 
Example 2
Source File: AuthenticationControllerTest.java    From auth0-java-mvc-common with MIT License 6 votes vote down vote up
@Test
public void shouldSetSameSiteNoneCookiesAndLegacyCookieWhenIdTokenResponse() {
    MockHttpServletResponse response = new MockHttpServletResponse();

    AuthenticationController controller = AuthenticationController.newBuilder("domain", "clientId", "clientSecret")
            .withResponseType("id_token")
            .build();

    controller.buildAuthorizeUrl(new MockHttpServletRequest(), response, "https://redirect.uri/here")
            .withState("state")
            .withNonce("nonce")
            .build();

    List<String> headers = response.getHeaders("Set-Cookie");

    assertThat(headers.size(), is(4));
    assertThat(headers, hasItem("com.auth0.state=state; HttpOnly; Max-Age=600; SameSite=None; Secure"));
    assertThat(headers, hasItem("_com.auth0.state=state; HttpOnly; Max-Age=600"));
    assertThat(headers, hasItem("com.auth0.nonce=nonce; HttpOnly; Max-Age=600; SameSite=None; Secure"));
    assertThat(headers, hasItem("_com.auth0.nonce=nonce; HttpOnly; Max-Age=600"));
}
 
Example 3
Source File: AuthenticationControllerTest.java    From auth0-java-mvc-common with MIT License 6 votes vote down vote up
@Test
public void shouldSetSameSiteNoneCookiesAndNoLegacyCookieWhenIdTokenResponse() {
    MockHttpServletResponse response = new MockHttpServletResponse();

    AuthenticationController controller = AuthenticationController.newBuilder("domain", "clientId", "clientSecret")
            .withResponseType("id_token")
            .withLegacySameSiteCookie(false)
            .build();

    controller.buildAuthorizeUrl(new MockHttpServletRequest(), response, "https://redirect.uri/here")
            .withState("state")
            .withNonce("nonce")
            .build();

    List<String> headers = response.getHeaders("Set-Cookie");

    assertThat(headers.size(), is(2));
    assertThat(headers, hasItem("com.auth0.state=state; HttpOnly; Max-Age=600; SameSite=None; Secure"));
    assertThat(headers, hasItem("com.auth0.nonce=nonce; HttpOnly; Max-Age=600; SameSite=None; Secure"));
}
 
Example 4
Source File: MockMvcClientHttpRequestFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		List<String> values = response.getHeaders(name);
		for (String value : values) {
			headers.add(name, value);
		}
	}
	return headers;
}
 
Example 5
Source File: MockMvcClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		List<String> values = response.getHeaders(name);
		for (String value : values) {
			headers.add(name, value);
		}
	}
	return headers;
}
 
Example 6
Source File: MockHttpClient.java    From karate with MIT License 5 votes vote down vote up
private static void logResponseHeaders(StringBuilder sb, int id, MockHttpServletResponse response) {
    Set<String> keys = new TreeSet(response.getHeaderNames());
    for (String key : keys) {
        List<String> entries = response.getHeaders(key);
        sb.append(id).append(' ').append('<').append(' ')
                .append(key).append(": ").append(entries.size() == 1 ? entries.get(0) : entries).append('\n');
    }
}
 
Example 7
Source File: MockMvcClientHttpRequestFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		List<String> values = response.getHeaders(name);
		for (String value : values) {
			headers.add(name, value);
		}
	}
	return headers;
}
 
Example 8
Source File: ServletNettyChannelHandler.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) throws Exception {
	if (!fullHttpRequest.getDecoderResult().isSuccess()) {
		sendError(channelHandlerContext, BAD_REQUEST);
		return;
	}

	MockHttpServletRequest servletRequest = createHttpServletRequest(fullHttpRequest);
	MockHttpServletResponse servletResponse = new MockHttpServletResponse();

	this.servlet.service(servletRequest, servletResponse);

	HttpResponseStatus status = HttpResponseStatus.valueOf(servletResponse.getStatus());
	HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
	
	for (String name : servletResponse.getHeaderNames()) {
		for (String value : servletResponse.getHeaders(name)) {
			response.headers().add(name, value);
		}
	}

	// Write the initial line and the header.
	channelHandlerContext.write(response);
	InputStream contentStream = new ByteArrayInputStream(servletResponse.getContentAsByteArray());

	// Write the content and flush it.
	ChannelFuture writeFuture = channelHandlerContext.writeAndFlush(new ChunkedStream(contentStream));
	writeFuture.addListener(ChannelFutureListener.CLOSE);
}
 
Example 9
Source File: MockMvcClientHttpRequestFactory.java    From initializr with Apache License 2.0 5 votes vote down vote up
private HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		List<String> values = response.getHeaders(name);
		for (String value : values) {
			headers.add(name, value);
		}
	}
	return headers;
}
 
Example 10
Source File: DapTestCommon.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public HttpResponse execute(HttpRequestBase rq) throws IOException {
  URI uri = rq.getURI();
  DapController controller = getController(uri);
  StandaloneMockMvcBuilder mvcbuilder = MockMvcBuilders.standaloneSetup(controller);
  mvcbuilder.setValidator(new TestServlet.NullValidator());
  MockMvc mockMvc = mvcbuilder.build();
  MockHttpServletRequestBuilder mockrb = MockMvcRequestBuilders.get(uri);
  // We need to use only the path part
  mockrb.servletPath(uri.getPath());
  // Move any headers from rq to mockrb
  Header[] headers = rq.getAllHeaders();
  for (int i = 0; i < headers.length; i++) {
    Header h = headers[i];
    mockrb.header(h.getName(), h.getValue());
  }
  // Since the url has the query parameters,
  // they will automatically be parsed and added
  // to the rb parameters.

  // Finally set the resource dir
  mockrb.requestAttr("RESOURCEDIR", this.resourcepath);

  // Now invoke the servlet
  MvcResult result;
  try {
    result = mockMvc.perform(mockrb).andReturn();
  } catch (Exception e) {
    throw new IOException(e);
  }

  // Collect the output
  MockHttpServletResponse res = result.getResponse();
  byte[] byteresult = res.getContentAsByteArray();

  // Convert to HttpResponse
  HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, res.getStatus(), "");
  if (response == null)
    throw new IOException("HTTPMethod.executeMock: Response was null");
  Collection<String> keys = res.getHeaderNames();
  // Move headers to the response
  for (String key : keys) {
    List<String> values = res.getHeaders(key);
    for (String v : values) {
      response.addHeader(key, v);
    }
  }
  ByteArrayEntity entity = new ByteArrayEntity(byteresult);
  String sct = res.getContentType();
  entity.setContentType(sct);
  response.setEntity(entity);
  return response;
}