Skip to content

Commit ccb31ec

Browse files
committed
Merge pull request #336 from Tomasz Kopczynski
* gh-336: Polish "Include cookies in request snippets" Include cookies in request snippets
2 parents 0337160 + a9c1e04 commit ccb31ec

File tree

18 files changed

+325
-75
lines changed

18 files changed

+325
-75
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,16 @@
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;
2931
import org.springframework.restdocs.operation.OperationRequestPart;
3032
import org.springframework.restdocs.operation.Parameters;
3133
import org.springframework.restdocs.snippet.Snippet;
3234
import org.springframework.restdocs.snippet.TemplatedSnippet;
35+
import org.springframework.util.CollectionUtils;
3336
import org.springframework.util.StringUtils;
3437

3538
/**
@@ -92,12 +95,27 @@ private String getOptions(Operation operation) {
9295
writeUserOptionIfNecessary(request, printer);
9396
writeHttpMethodIfNecessary(request, printer);
9497
writeHeaders(request, printer);
98+
writeCookies(request, printer);
9599
writePartsIfNecessary(request, printer);
96100
writeContent(request, printer);
97101

98102
return command.toString();
99103
}
100104

105+
private void writeCookies(CliOperationRequest request, PrintWriter printer) {
106+
if (!CollectionUtils.isEmpty(request.getCookies())) {
107+
StringBuilder cookiesBuilder = new StringBuilder();
108+
for (Cookie cookie : request.getCookies()) {
109+
if (cookiesBuilder.length() > 0) {
110+
cookiesBuilder.append(";");
111+
}
112+
cookiesBuilder.append(
113+
String.format("%s=%s", cookie.getName(), cookie.getValue()));
114+
}
115+
printer.print(String.format(" --cookie '%s'", cookiesBuilder.toString()));
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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -106,6 +108,7 @@ private String getRequestItems(CliOperationRequest request) {
106108
PrintWriter printer = new PrintWriter(requestItems);
107109
writeFormDataIfNecessary(request, printer);
108110
writeHeaders(request, printer);
111+
writeCookies(request, printer);
109112
writeParametersIfNecessary(request, printer);
110113
return requestItems.toString();
111114
}
@@ -162,6 +165,13 @@ private void writeHeaders(OperationRequest request, PrintWriter writer) {
162165
}
163166
}
164167

168+
private void writeCookies(OperationRequest request, PrintWriter writer) {
169+
for (Cookie cookie : request.getCookies()) {
170+
writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(),
171+
cookie.getValue()));
172+
}
173+
}
174+
165175
private void writeParametersIfNecessary(CliOperationRequest request,
166176
PrintWriter writer) {
167177
if (StringUtils.hasText(request.getContentAsString())) {

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,13 @@ public interface OperationRequest {
8688
*/
8789
URI getUri();
8890

91+
/**
92+
* Returns {@link Cookie Cookies} sent with the request. If no cookies were sent an
93+
* empty collection is returned.
94+
*
95+
* @return the cookies, never {@code null}
96+
* @since 1.2.0
97+
*/
98+
Collection<Cookie> getCookies();
99+
89100
}

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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,25 +41,30 @@ 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
44-
* will have the given {@code headers}, {@code parameters}, and {@code parts}.
48+
* will have the given {@code headers}, {@code parameters}, {@code parts}, and
49+
* {@code cookies}.
4550
*
4651
* @param uri the uri
4752
* @param method the method
4853
* @param content the content
4954
* @param headers the headers
5055
* @param parameters the parameters
5156
* @param parts the parts
57+
* @param cookies the cookies
5258
*/
5359
StandardOperationRequest(URI uri, HttpMethod method, byte[] content,
5460
HttpHeaders headers, Parameters parameters,
55-
Collection<OperationRequestPart> parts) {
61+
Collection<OperationRequestPart> parts, Collection<Cookie> cookies) {
5662
super(content, headers);
5763
this.uri = uri;
5864
this.method = method;
5965
this.parameters = parameters;
6066
this.parts = parts;
67+
this.cookies = cookies;
6168
}
6269

6370
@Override
@@ -80,4 +87,9 @@ public URI getUri() {
8087
return this.uri;
8188
}
8289

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

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -251,16 +251,27 @@ 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").cookie("name2", "value2").build());
262+
}
263+
254264
@Test
255265
public void multipartPostWithNoSubmittedFileName() throws IOException {
256266
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
257267
+ "'Content-Type: multipart/form-data' -F "
258268
+ "'metadata={\"description\": \"foo\"}'";
259269
this.snippets.expectCurlRequest()
260270
.withContents(codeBlock("bash").content(expectedContent));
261-
new CurlRequestSnippet().document(this.operationBuilder
262-
.request("http://localhost/upload").method("POST")
263-
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
271+
new CurlRequestSnippet().document(
272+
this.operationBuilder.request("http://localhost/upload").method("POST")
273+
.header(HttpHeaders.CONTENT_TYPE,
274+
MediaType.MULTIPART_FORM_DATA_VALUE)
264275
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
265276
}
266277

@@ -275,9 +286,9 @@ public void multipartPostWithContentType() throws IOException {
275286
this.operationBuilder.request("http://localhost/upload").method("POST")
276287
.header(HttpHeaders.CONTENT_TYPE,
277288
MediaType.MULTIPART_FORM_DATA_VALUE)
278-
.part("image", new byte[0])
279-
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
280-
.submittedFileName("documents/images/example.png").build());
289+
.part("image", new byte[0])
290+
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
291+
.submittedFileName("documents/images/example.png").build());
281292
}
282293

283294
@Test
@@ -291,8 +302,8 @@ public void multipartPost() throws IOException {
291302
this.operationBuilder.request("http://localhost/upload").method("POST")
292303
.header(HttpHeaders.CONTENT_TYPE,
293304
MediaType.MULTIPART_FORM_DATA_VALUE)
294-
.part("image", new byte[0])
295-
.submittedFileName("documents/images/example.png").build());
305+
.part("image", new byte[0])
306+
.submittedFileName("documents/images/example.png").build());
296307
}
297308

298309
@Test
@@ -307,9 +318,9 @@ public void multipartPostWithParameters() throws IOException {
307318
this.operationBuilder.request("http://localhost/upload").method("POST")
308319
.header(HttpHeaders.CONTENT_TYPE,
309320
MediaType.MULTIPART_FORM_DATA_VALUE)
310-
.part("image", new byte[0])
311-
.submittedFileName("documents/images/example.png").and()
312-
.param("a", "apple", "avocado").param("b", "banana").build());
321+
.part("image", new byte[0])
322+
.submittedFileName("documents/images/example.png").and()
323+
.param("a", "apple", "avocado").param("b", "banana").build());
313324
}
314325

315326
@Test
@@ -321,7 +332,7 @@ public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException
321332
.header(HttpHeaders.AUTHORIZATION,
322333
"Basic " + Base64Utils
323334
.encodeToString("user:secret".getBytes()))
324-
.build());
335+
.build());
325336
}
326337

327338
@Test

0 commit comments

Comments
 (0)