Polish "Include cookies in the HTTP response snippet"

See gh-340
Closes gh-305
This commit is contained in:
Andy Wilkinson
2017-03-13 20:45:23 +00:00
parent 46190535f7
commit 1b5e75ff2c
5 changed files with 22 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 the original author or authors. * Copyright 2014-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -48,7 +48,8 @@ class MockMvcResponseConverter implements ResponseConverter<MockHttpServletRespo
} }
} }
if (response.getCookies() != null && !headers.containsKey(HttpHeaders.SET_COOKIE)) { if (response.getCookies() != null
&& !headers.containsKey(HttpHeaders.SET_COOKIE)) {
for (Cookie cookie : response.getCookies()) { for (Cookie cookie : response.getCookies()) {
headers.add(HttpHeaders.SET_COOKIE, generateSetCookieHeader(cookie)); headers.add(HttpHeaders.SET_COOKIE, generateSetCookieHeader(cookie));
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 the original author or authors. * Copyright 2014-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public class MockMvcResponseConverterTests {
private final MockMvcResponseConverter factory = new MockMvcResponseConverter(); private final MockMvcResponseConverter factory = new MockMvcResponseConverter();
@Test @Test
public void httpResponse() { public void basicResponse() {
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);
@@ -67,6 +67,7 @@ public class MockMvcResponseConverterTests {
assertThat(operationResponse.getHeaders().size(), is(1)); assertThat(operationResponse.getHeaders().size(), is(1));
assertTrue(operationResponse.getHeaders().containsKey(HttpHeaders.SET_COOKIE)); assertTrue(operationResponse.getHeaders().containsKey(HttpHeaders.SET_COOKIE));
assertThat(operationResponse.getHeaders().get(HttpHeaders.SET_COOKIE), equalTo(Collections.singletonList("name=value;domain=localhost;HttpOnly"))); assertThat(operationResponse.getHeaders().get(HttpHeaders.SET_COOKIE), equalTo(
Collections.singletonList("name=value;domain=localhost;HttpOnly")));
} }
} }

View File

@@ -371,17 +371,16 @@ public class MockMvcRestDocumentationIntegrationTests {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).build(); .apply(documentationConfiguration(this.restDocumentation)).build();
mockMvc.perform(get("/set-cookie")) mockMvc.perform(get("/set-cookie")).andExpect(status().isOk())
.andExpect(status().isOk())
.andDo(document("set-cookie", .andDo(document("set-cookie",
responseHeaders(headerWithName(HttpHeaders.SET_COOKIE).description("set-cookie")))); responseHeaders(headerWithName(HttpHeaders.SET_COOKIE)
.description("set-cookie"))));
assertThat( assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"),
new File("build/generated-snippets/set-cookie/http-response.adoc"),
is(snippet(asciidoctor()) is(snippet(asciidoctor())
.withContents( .withContents(httpResponse(asciidoctor(), HttpStatus.OK).header(
httpResponse(asciidoctor(), HttpStatus.OK) HttpHeaders.SET_COOKIE,
.header(HttpHeaders.SET_COOKIE, "name=value;domain=localhost;HttpOnly")))); "name=value;domain=localhost;HttpOnly"))));
} }
@Test @Test

View File

@@ -274,19 +274,17 @@ public class RestAssuredRestDocumentationIntegrationTests {
given().port(tomcat.getPort()) given().port(tomcat.getPort())
.filter(documentationConfiguration(this.restDocumentation)) .filter(documentationConfiguration(this.restDocumentation))
.filter(document("set-cookie", .filter(document("set-cookie",
preprocessResponse(removeHeaders(HttpHeaders.DATE, HttpHeaders.CONTENT_TYPE)))) preprocessResponse(removeHeaders(HttpHeaders.DATE,
HttpHeaders.CONTENT_TYPE))))
.get("/set-cookie").then().statusCode(200); .get("/set-cookie").then().statusCode(200);
assertExpectedSnippetFilesExist(new File("build/generated-snippets/set-cookie"), assertExpectedSnippetFilesExist(new File("build/generated-snippets/set-cookie"),
"http-request.adoc", "http-response.adoc", "curl-request.adoc"); "http-request.adoc", "http-response.adoc", "curl-request.adoc");
assertThat( assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"),
new File("build/generated-snippets/set-cookie/http-response.adoc"),
is(snippet(asciidoctor()) is(snippet(asciidoctor())
.withContents(httpResponse(asciidoctor(), HttpStatus.OK) .withContents(httpResponse(asciidoctor(), HttpStatus.OK).header(
.header(HttpHeaders.SET_COOKIE, "name=value;domain=localhost;HttpOnly") HttpHeaders.SET_COOKIE,
) "name=value;domain=localhost;HttpOnly"))));
)
);
} }
@Test @Test

View File

@@ -113,7 +113,8 @@ class TomcatServer extends ExternalResource {
private static final class CookiesServlet extends HttpServlet { private static final class CookiesServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Cookie cookie = new Cookie("name", "value"); Cookie cookie = new Cookie("name", "value");
cookie.setDomain("localhost"); cookie.setDomain("localhost");
cookie.setHttpOnly(true); cookie.setHttpOnly(true);