Skip to content

Commit da99436

Browse files
tkopczynskimeistermeier
authored andcommitted
1 parent 3fd3ac6 commit da99436

File tree

18 files changed

+271
-11
lines changed

18 files changed

+271
-11
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Map.Entry;
2626
import java.util.Set;
2727

28+
import javax.servlet.http.Cookie;
29+
2830
import org.springframework.http.HttpHeaders;
2931
import org.springframework.http.HttpMethod;
3032
import org.springframework.restdocs.operation.OperationRequest;
@@ -125,6 +127,11 @@ public URI getUri() {
125127
return this.delegate.getUri();
126128
}
127129

130+
@Override
131+
public Collection<Cookie> getCookies() {
132+
return this.delegate.getCookies();
133+
}
134+
128135
private interface HeaderFilter {
129136

130137
boolean allow(String name, List<String> value);

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Map.Entry;
2525

26+
import javax.servlet.http.Cookie;
27+
2628
import org.springframework.http.HttpMethod;
2729
import org.springframework.restdocs.operation.Operation;
2830
import org.springframework.restdocs.operation.OperationRequest;
@@ -92,12 +94,28 @@ private String getOptions(Operation operation) {
9294
writeUserOptionIfNecessary(request, printer);
9395
writeHttpMethodIfNecessary(request, printer);
9496
writeHeaders(request, printer);
97+
writeCookies(request, printer);
9598
writePartsIfNecessary(request, printer);
9699
writeContent(request, printer);
97100

98101
return command.toString();
99102
}
100103

104+
private void writeCookies(CliOperationRequest request, PrintWriter printer) {
105+
if (request.getCookies() != null && request.getCookies().size() > 0) {
106+
printer.print(" --cookie ");
107+
StringBuilder cookiesBuilder = new StringBuilder();
108+
109+
for (Cookie cookie : request.getCookies()) {
110+
cookiesBuilder.append(String.format("%s=%s;", cookie.getName(), cookie.getValue()));
111+
}
112+
113+
String cookiesHeader = cookiesBuilder.substring(0, cookiesBuilder.length() - 1); // remove trailing semicolon
114+
115+
printer.print(String.format("'%s'", cookiesHeader)); // add single quotes
116+
}
117+
}
118+
101119
private void writeIncludeHeadersInOutputOption(PrintWriter writer) {
102120
writer.print("-i");
103121
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.Map.Entry;
2525

26+
import javax.servlet.http.Cookie;
27+
2628
import org.springframework.http.HttpHeaders;
2729
import org.springframework.http.HttpMethod;
2830
import org.springframework.http.MediaType;
@@ -160,6 +162,10 @@ private void writeHeaders(OperationRequest request, PrintWriter writer) {
160162
writer.print(String.format(" '%s:%s'", entry.getKey(), header));
161163
}
162164
}
165+
166+
for (Cookie cookie : request.getCookies()) {
167+
writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(), cookie.getValue()));
168+
}
163169
}
164170

165171
private void writeParametersIfNecessary(CliOperationRequest request,

spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Map.Entry;
2626

27+
import javax.servlet.http.Cookie;
28+
2729
import org.springframework.http.HttpHeaders;
2830
import org.springframework.http.HttpMethod;
2931
import org.springframework.http.MediaType;
@@ -35,6 +37,7 @@
3537
import org.springframework.restdocs.snippet.TemplatedSnippet;
3638
import org.springframework.util.StringUtils;
3739

40+
3841
/**
3942
* A {@link Snippet} that documents an HTTP request.
4043
*
@@ -112,6 +115,11 @@ private List<Map<String, String>> getHeaders(OperationRequest request) {
112115

113116
}
114117
}
118+
119+
for (Cookie cookie : request.getCookies()) {
120+
headers.add(header(HttpHeaders.COOKIE, String.format("%s=%s", cookie.getName(), cookie.getValue())));
121+
}
122+
115123
if (requiresFormEncodingContentTypeHeader(request)) {
116124
headers.add(header(HttpHeaders.CONTENT_TYPE,
117125
MediaType.APPLICATION_FORM_URLENCODED_VALUE));

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.net.URI;
2020
import java.util.Collection;
2121

22+
import javax.servlet.http.Cookie;
23+
2224
import org.springframework.http.HttpHeaders;
2325
import org.springframework.http.HttpMethod;
2426

@@ -86,4 +88,11 @@ public interface OperationRequest {
8688
*/
8789
URI getUri();
8890

91+
/**
92+
* Returns cookies sent with the request.
93+
*
94+
* @return the cookies
95+
*/
96+
Collection<Cookie> getCookies();
97+
8998
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestFactory.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.net.URI;
2020
import java.util.Collection;
21+
import java.util.Collections;
22+
23+
import javax.servlet.http.Cookie;
2124

2225
import org.springframework.http.HttpHeaders;
2326
import org.springframework.http.HttpMethod;
@@ -40,13 +43,34 @@ public class OperationRequestFactory {
4043
* @param headers the request's headers
4144
* @param parameters the request's parameters
4245
* @param parts the request's parts
46+
* @param cookies the request's cookies
4347
* @return the {@code OperationRequest}
4448
*/
4549
public OperationRequest create(URI uri, HttpMethod method, byte[] content,
4650
HttpHeaders headers, Parameters parameters,
47-
Collection<OperationRequestPart> parts) {
51+
Collection<OperationRequestPart> parts,
52+
Collection<Cookie> cookies) {
4853
return new StandardOperationRequest(uri, method, content,
49-
augmentHeaders(headers, uri, content), parameters, parts);
54+
augmentHeaders(headers, uri, content), parameters, parts, cookies);
55+
}
56+
57+
/**
58+
* Creates a new {@link OperationRequest}. The given {@code headers} will be augmented
59+
* to ensure that they always include a {@code Content-Length} header if the request
60+
* has any content and a {@code Host} header.
61+
*
62+
* @param uri the request's uri
63+
* @param method the request method
64+
* @param content the content of the request
65+
* @param headers the request's headers
66+
* @param parameters the request's parameters
67+
* @param parts the request's parts
68+
* @return the {@code OperationRequest}
69+
*/
70+
public OperationRequest create(URI uri, HttpMethod method, byte[] content,
71+
HttpHeaders headers, Parameters parameters,
72+
Collection<OperationRequestPart> parts) {
73+
return create(uri, method, content, headers, parameters, parts, Collections.<Cookie>emptyList());
5074
}
5175

5276
/**
@@ -62,7 +86,7 @@ public OperationRequest create(URI uri, HttpMethod method, byte[] content,
6286
public OperationRequest createFrom(OperationRequest original, byte[] newContent) {
6387
return new StandardOperationRequest(original.getUri(), original.getMethod(),
6488
newContent, getUpdatedHeaders(original.getHeaders(), newContent),
65-
original.getParameters(), original.getParts());
89+
original.getParameters(), original.getParts(), original.getCookies());
6690
}
6791

6892
/**
@@ -78,7 +102,7 @@ public OperationRequest createFrom(OperationRequest original,
78102
HttpHeaders newHeaders) {
79103
return new StandardOperationRequest(original.getUri(), original.getMethod(),
80104
original.getContent(), newHeaders, original.getParameters(),
81-
original.getParts());
105+
original.getParts(), original.getCookies());
82106
}
83107

84108
/**
@@ -94,7 +118,7 @@ public OperationRequest createFrom(OperationRequest original,
94118
Parameters newParameters) {
95119
return new StandardOperationRequest(original.getUri(), original.getMethod(),
96120
original.getContent(), original.getHeaders(), newParameters,
97-
original.getParts());
121+
original.getParts(), original.getCookies());
98122
}
99123

100124
private HttpHeaders augmentHeaders(HttpHeaders originalHeaders, URI uri,

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222

23+
import javax.servlet.http.Cookie;
24+
2325
import org.springframework.http.HttpHeaders;
2426
import org.springframework.http.HttpMethod;
2527

@@ -39,6 +41,8 @@ class StandardOperationRequest extends AbstractOperationMessage
3941

4042
private URI uri;
4143

44+
private Collection<Cookie> cookies;
45+
4246
/**
4347
* Creates a new request with the given {@code uri} and {@code method}. The request
4448
* will have the given {@code headers}, {@code parameters}, and {@code parts}.
@@ -52,12 +56,14 @@ class StandardOperationRequest extends AbstractOperationMessage
5256
*/
5357
StandardOperationRequest(URI uri, HttpMethod method, byte[] content,
5458
HttpHeaders headers, Parameters parameters,
55-
Collection<OperationRequestPart> parts) {
59+
Collection<OperationRequestPart> parts,
60+
Collection<Cookie> cookies) {
5661
super(content, headers);
5762
this.uri = uri;
5863
this.method = method;
5964
this.parameters = parameters;
6065
this.parts = parts;
66+
this.cookies = cookies;
6167
}
6268

6369
@Override
@@ -80,4 +86,9 @@ public URI getUri() {
8086
return this.uri;
8187
}
8288

89+
@Override
90+
public Collection<Cookie> getCookies() {
91+
return this.cookies;
92+
}
93+
8394
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ public void requestWithHeaders() throws IOException {
251251
.header("a", "alpha").build());
252252
}
253253

254+
@Test
255+
public void requestWithCookies() throws IOException {
256+
this.snippets.expectCurlRequest()
257+
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i" +
258+
" --cookie 'name1=value1;name2=value2'"));
259+
new CurlRequestSnippet()
260+
.document(this.operationBuilder.request("http://localhost/foo")
261+
.cookie("name1", "value1")
262+
.cookie("name2", "value2").build());
263+
}
264+
254265
@Test
255266
public void multipartPostWithNoSubmittedFileName() throws IOException {
256267
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "

spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ public void requestWithHeaders() throws IOException {
252252
.header("a", "alpha").build());
253253
}
254254

255+
@Test
256+
public void requestWithCookies() throws IOException {
257+
this.snippets.expectHttpieRequest().withContents(
258+
codeBlock("bash").content("$ http GET 'http://localhost/foo'" +
259+
" 'Cookie:name1=value1' 'Cookie:name2=value2'"));
260+
new HttpieRequestSnippet()
261+
.document(this.operationBuilder.request("http://localhost/foo")
262+
.cookie("name1", "value1")
263+
.cookie("name2", "value2").build());
264+
}
265+
255266
@Test
256267
public void multipartPostWithNoSubmittedFileName() throws IOException {
257268
String expectedContent = String

spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void requestWithHeaders() throws IOException {
5454
.row("`X-Test`", "one").row("`Accept`", "two")
5555
.row("`Accept-Encoding`", "three")
5656
.row("`Accept-Language`", "four").row("`Cache-Control`", "five")
57-
.row("`Connection`", "six"));
57+
.row("`Connection`", "six").row("`Cookie`", "seven"));
5858
new RequestHeadersSnippet(
5959
Arrays.asList(headerWithName("X-Test").description("one"),
6060
headerWithName("Accept").description("two"),
@@ -63,7 +63,8 @@ public void requestWithHeaders() throws IOException {
6363
headerWithName("Cache-Control").description("five"),
6464
headerWithName(
6565
"Connection")
66-
.description("six")))
66+
.description("six"),
67+
headerWithName("Cookie").description("seven")))
6768
.document(
6869
this.operationBuilder
6970
.request(
@@ -78,6 +79,8 @@ public void requestWithHeaders() throws IOException {
7879
"max-age=0")
7980
.header("Connection",
8081
"keep-alive")
82+
.header("Cookie",
83+
"cookie1=cookieVal1; cookie2=cookieVal2")
8184
.build());
8285
}
8386

spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ public void getRequestWithPort() throws IOException {
8080
.request("http://localhost:8080/foo").header("Alpha", "a").build());
8181
}
8282

83+
@Test
84+
public void getRequestWithCookies() throws IOException {
85+
this.snippets.expectHttpRequest()
86+
.withContents(httpRequest(RequestMethod.GET, "/foo")
87+
.header(HttpHeaders.HOST, "localhost")
88+
.header(HttpHeaders.COOKIE, "name1=value1")
89+
.header(HttpHeaders.COOKIE, "name2=value2"));
90+
91+
new HttpRequestSnippet()
92+
.document(this.operationBuilder.request("http://localhost/foo")
93+
.cookie("name1", "value1")
94+
.cookie("name2", "value2")
95+
.build());
96+
}
97+
8398
@Test
8499
public void getRequestWithQueryString() throws IOException {
85100
this.snippets.expectHttpRequest()

spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import java.io.File;
2020
import java.net.URI;
2121
import java.util.ArrayList;
22+
import java.util.Collection;
2223
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627

28+
import javax.servlet.http.Cookie;
29+
2730
import org.junit.runners.model.Statement;
2831

2932
import org.springframework.http.HttpHeaders;
@@ -153,6 +156,8 @@ public final class OperationRequestBuilder {
153156

154157
private List<OperationRequestPartBuilder> partBuilders = new ArrayList<>();
155158

159+
private Collection<Cookie> cookies = new ArrayList<>();
160+
156161
private OperationRequestBuilder(String uri) {
157162
this.requestUri = URI.create(uri);
158163
}
@@ -163,7 +168,7 @@ private OperationRequest buildRequest() {
163168
parts.add(builder.buildPart());
164169
}
165170
return new OperationRequestFactory().create(this.requestUri, this.method,
166-
this.content, this.headers, this.parameters, parts);
171+
this.content, this.headers, this.parameters, parts, this.cookies);
167172
}
168173

169174
public Operation build() {
@@ -209,6 +214,11 @@ public OperationRequestPartBuilder part(String name, byte[] content) {
209214
return partBuilder;
210215
}
211216

217+
public OperationRequestBuilder cookie(String name, String value) {
218+
this.cookies.add(new Cookie(name, value));
219+
return this;
220+
}
221+
212222
/**
213223
* Basic builder API for creating an {@link OperationRequestPart}.
214224
*/

0 commit comments

Comments
 (0)