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

The following examples show how to use org.springframework.mock.web.MockHttpServletResponse#addHeader() . 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: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 8 votes vote down vote up
@Test
void testNoHeaderRecording() throws IOException, ServletException {
    when(coreConfiguration.isCaptureHeaders()).thenReturn(false);
    filterChain = new MockFilterChain(new TestServlet());
    final MockHttpServletRequest get = new MockHttpServletRequest("GET", "/foo");
    get.addHeader("Elastic-Apm-Traceparent", "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01");
    get.setCookies(new Cookie("foo", "bar"));
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockResponse.addHeader("foo", "bar");
    mockResponse.addHeader("bar", "baz");
    filterChain.doFilter(get, mockResponse);
    assertThat(reporter.getTransactions()).hasSize(1);
    assertThat(reporter.getFirstTransaction().getContext().getResponse().getHeaders().isEmpty()).isTrue();
    assertThat(reporter.getFirstTransaction().getContext().getRequest().getHeaders().isEmpty()).isTrue();
    assertThat(reporter.getFirstTransaction().getContext().getRequest().getCookies().isEmpty()).isTrue();
    assertThat(reporter.getFirstTransaction().getTraceContext().getTraceId().toString()).isEqualTo("0af7651916cd43dd8448eb211c80319c");
    assertThat(reporter.getFirstTransaction().getTraceContext().getParentId().toString()).isEqualTo("b9c7c989f97918e1");
}
 
Example 2
Source File: ApmFilterTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Test
void testAllHeaderRecording() throws IOException, ServletException {
    when(coreConfiguration.isCaptureHeaders()).thenReturn(true);
    filterChain = new MockFilterChain(new TestServlet());
    final MockHttpServletRequest get = new MockHttpServletRequest("GET", "/foo");
    get.addHeader("foo", "bar");
    get.setCookies(new Cookie("foo", "bar"));
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    mockResponse.addHeader("foo", "bar");
    mockResponse.addHeader("bar", "baz");
    filterChain.doFilter(get, mockResponse);
    assertThat(reporter.getTransactions()).hasSize(1);
    final Request request = reporter.getFirstTransaction().getContext().getRequest();
    assertThat(request.getHeaders().isEmpty()).isFalse();
    assertThat(request.getHeaders().get("foo")).isEqualTo("bar");
    assertThat(request.getCookies().get("foo")).isEqualTo("bar");
    final Response response = reporter.getFirstTransaction().getContext().getResponse();
    assertThat(response.getHeaders().get("foo")).isEqualTo("bar");
    assertThat(response.getHeaders().get("bar")).isEqualTo("baz");
}
 
Example 3
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void existingRequiredWildcardResponseHeader() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200);
    response.addHeader("x-", "w");
    assertNoViolations(test(aggregator,
            header,
            get("/resheader/reqwild"),
            response));
}
 
Example 4
Source File: XpathResultMatchersTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
Example 5
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void wildcardResponseHeader() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200, "\"hula\"");
    response.addHeader("x-bla", "1");
    response.addHeader("x-hula", "2");
    response.addHeader("req", "3");
    assertNoViolations(test(aggregator,
            header,
            get("/resheader"),
            response));
}
 
Example 6
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void allowedRepeatResponseHeader() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200, "\"hula\"");
    response.addHeader("rep", "1");
    response.addHeader("rep", "2");
    response.addHeader("req", "xxx");
    assertNoViolations(test(aggregator,
            header,
            get("/resheader"),
            response));
}
 
Example 7
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void caseInsensitiveNames() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200);
    response.addHeader("x-INT", "6");
    assertNoViolations(test(aggregator,
            header,
            get("/header/xint").header("x-INT", "5"),
            response));
}
 
Example 8
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void undefinedResponseHeader() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200, "\"hula\"");
    response.addHeader("a", "b");
    assertOneResponseViolationThat(test(aggregator,
            header,
            get("/data"),
            response),
            equalTo("Header 'a' on action(GET /data) response(200) is not defined")
    );
}
 
Example 9
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void notIgnoreXresponseHeadersIfGiven() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200);
    response.addHeader("x-int", "blu");
    response.addHeader("x-ig", "nix");
    assertOneResponseViolationThat(test(aggregator,
            header.ignoringXheaders(),
            get("/header/xint"),
            response),
            equalTo("Header 'x-int' on action(GET /header/xint) response(200) - Value 'blu' is not a valid integer"));
}
 
Example 10
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test(expected = AssertionError.class)
public void prefixWithPayloadNotLongEnough() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(new String("test".getBytes("ISO-8859-1")));
	StubMvcResult result =  new StubMvcResult(null, null, null, null, null, null, response);

	new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result);
}
 
Example 11
Source File: XpathResultMatchersTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
Example 12
Source File: HeaderTest.java    From raml-tester with Apache License 2.0 5 votes vote down vote up
@Test
public void ignoreXheaders() throws Exception {
    final MockHttpServletResponse response = jsonResponse(200, "\"hula\"");
    response.addHeader("x-hula", "hop");
    assertNoViolations(test(aggregator,
            header.ignoringXheaders(),
            get("/data").header("x-bla", "blu"),
            response));
}
 
Example 13
Source File: ContentResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 14
Source File: JsonPathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult createPrefixedStubMvcResult(String jsonPrefix) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 15
Source File: XpathResultMatchersTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 16
Source File: XpathResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 17
Source File: ContentResultMatchersTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 18
Source File: ContentResultMatchersTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 19
Source File: JsonPathResultMatchersTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private StubMvcResult createPrefixedStubMvcResult(String jsonPrefix) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
Example 20
Source File: XpathResultMatchersTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
	return new StubMvcResult(null, null, null, null, null, null, response);
}