Skip to content

Commit a9c1e04

Browse files
committed
Polish "Include cookies in request snippets"
See gh-336 Closes gh-302 Closes gh-303 Closes gh-304
1 parent 25bc6c3 commit a9c1e04

File tree

16 files changed

+107
-117
lines changed

16 files changed

+107
-117
lines changed

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

Lines changed: 1 addition & 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.

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

Lines changed: 9 additions & 9 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.
@@ -32,6 +32,7 @@
3232
import org.springframework.restdocs.operation.Parameters;
3333
import org.springframework.restdocs.snippet.Snippet;
3434
import org.springframework.restdocs.snippet.TemplatedSnippet;
35+
import org.springframework.util.CollectionUtils;
3536
import org.springframework.util.StringUtils;
3637

3738
/**
@@ -102,17 +103,16 @@ private String getOptions(Operation operation) {
102103
}
103104

104105
private void writeCookies(CliOperationRequest request, PrintWriter printer) {
105-
if (request.getCookies() != null && request.getCookies().size() > 0) {
106-
printer.print(" --cookie ");
106+
if (!CollectionUtils.isEmpty(request.getCookies())) {
107107
StringBuilder cookiesBuilder = new StringBuilder();
108-
109108
for (Cookie cookie : request.getCookies()) {
110-
cookiesBuilder.append(String.format("%s=%s;", cookie.getName(), cookie.getValue()));
109+
if (cookiesBuilder.length() > 0) {
110+
cookiesBuilder.append(";");
111+
}
112+
cookiesBuilder.append(
113+
String.format("%s=%s", cookie.getName(), cookie.getValue()));
111114
}
112-
113-
String cookiesHeader = cookiesBuilder.substring(0, cookiesBuilder.length() - 1); // remove trailing semicolon
114-
115-
printer.print(String.format("'%s'", cookiesHeader)); // add single quotes
115+
printer.print(String.format(" --cookie '%s'", cookiesBuilder.toString()));
116116
}
117117
}
118118

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

Lines changed: 6 additions & 2 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.
@@ -108,6 +108,7 @@ private String getRequestItems(CliOperationRequest request) {
108108
PrintWriter printer = new PrintWriter(requestItems);
109109
writeFormDataIfNecessary(request, printer);
110110
writeHeaders(request, printer);
111+
writeCookies(request, printer);
111112
writeParametersIfNecessary(request, printer);
112113
return requestItems.toString();
113114
}
@@ -162,9 +163,12 @@ private void writeHeaders(OperationRequest request, PrintWriter writer) {
162163
writer.print(String.format(" '%s:%s'", entry.getKey(), header));
163164
}
164165
}
166+
}
165167

168+
private void writeCookies(OperationRequest request, PrintWriter writer) {
166169
for (Cookie cookie : request.getCookies()) {
167-
writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(), cookie.getValue()));
170+
writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(),
171+
cookie.getValue()));
168172
}
169173
}
170174

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

Lines changed: 5 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.
@@ -89,9 +89,11 @@ public interface OperationRequest {
8989
URI getUri();
9090

9191
/**
92-
* Returns cookies sent with the request.
92+
* Returns {@link Cookie Cookies} sent with the request. If no cookies were sent an
93+
* empty collection is returned.
9394
*
94-
* @return the cookies
95+
* @return the cookies, never {@code null}
96+
* @since 1.2.0
9597
*/
9698
Collection<Cookie> getCookies();
9799

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

Lines changed: 5 additions & 4 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.
@@ -45,19 +45,20 @@ class StandardOperationRequest extends AbstractOperationMessage
4545

4646
/**
4747
* Creates a new request with the given {@code uri} and {@code method}. The request
48-
* 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}.
4950
*
5051
* @param uri the uri
5152
* @param method the method
5253
* @param content the content
5354
* @param headers the headers
5455
* @param parameters the parameters
5556
* @param parts the parts
57+
* @param cookies the cookies
5658
*/
5759
StandardOperationRequest(URI uri, HttpMethod method, byte[] content,
5860
HttpHeaders headers, Parameters parameters,
59-
Collection<OperationRequestPart> parts,
60-
Collection<Cookie> cookies) {
61+
Collection<OperationRequestPart> parts, Collection<Cookie> cookies) {
6162
super(content, headers);
6263
this.uri = uri;
6364
this.method = method;

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

Lines changed: 17 additions & 17 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.
@@ -254,12 +254,11 @@ public void requestWithHeaders() throws IOException {
254254
@Test
255255
public void requestWithCookies() throws IOException {
256256
this.snippets.expectCurlRequest()
257-
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i" +
258-
" --cookie 'name1=value1;name2=value2'"));
257+
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
258+
+ " --cookie 'name1=value1;name2=value2'"));
259259
new CurlRequestSnippet()
260260
.document(this.operationBuilder.request("http://localhost/foo")
261-
.cookie("name1", "value1")
262-
.cookie("name2", "value2").build());
261+
.cookie("name1", "value1").cookie("name2", "value2").build());
263262
}
264263

265264
@Test
@@ -269,9 +268,10 @@ public void multipartPostWithNoSubmittedFileName() throws IOException {
269268
+ "'metadata={\"description\": \"foo\"}'";
270269
this.snippets.expectCurlRequest()
271270
.withContents(codeBlock("bash").content(expectedContent));
272-
new CurlRequestSnippet().document(this.operationBuilder
273-
.request("http://localhost/upload").method("POST")
274-
.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)
275275
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
276276
}
277277

@@ -286,9 +286,9 @@ public void multipartPostWithContentType() throws IOException {
286286
this.operationBuilder.request("http://localhost/upload").method("POST")
287287
.header(HttpHeaders.CONTENT_TYPE,
288288
MediaType.MULTIPART_FORM_DATA_VALUE)
289-
.part("image", new byte[0])
290-
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
291-
.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());
292292
}
293293

294294
@Test
@@ -302,8 +302,8 @@ public void multipartPost() throws IOException {
302302
this.operationBuilder.request("http://localhost/upload").method("POST")
303303
.header(HttpHeaders.CONTENT_TYPE,
304304
MediaType.MULTIPART_FORM_DATA_VALUE)
305-
.part("image", new byte[0])
306-
.submittedFileName("documents/images/example.png").build());
305+
.part("image", new byte[0])
306+
.submittedFileName("documents/images/example.png").build());
307307
}
308308

309309
@Test
@@ -318,9 +318,9 @@ public void multipartPostWithParameters() throws IOException {
318318
this.operationBuilder.request("http://localhost/upload").method("POST")
319319
.header(HttpHeaders.CONTENT_TYPE,
320320
MediaType.MULTIPART_FORM_DATA_VALUE)
321-
.part("image", new byte[0])
322-
.submittedFileName("documents/images/example.png").and()
323-
.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());
324324
}
325325

326326
@Test
@@ -332,7 +332,7 @@ public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException
332332
.header(HttpHeaders.AUTHORIZATION,
333333
"Basic " + Base64Utils
334334
.encodeToString("user:secret".getBytes()))
335-
.build());
335+
.build());
336336
}
337337

338338
@Test

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

Lines changed: 17 additions & 17 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.
@@ -255,12 +255,11 @@ public void requestWithHeaders() throws IOException {
255255
@Test
256256
public void requestWithCookies() throws IOException {
257257
this.snippets.expectHttpieRequest().withContents(
258-
codeBlock("bash").content("$ http GET 'http://localhost/foo'" +
259-
" 'Cookie:name1=value1' 'Cookie:name2=value2'"));
258+
codeBlock("bash").content("$ http GET 'http://localhost/foo'"
259+
+ " 'Cookie:name1=value1' 'Cookie:name2=value2'"));
260260
new HttpieRequestSnippet()
261261
.document(this.operationBuilder.request("http://localhost/foo")
262-
.cookie("name1", "value1")
263-
.cookie("name2", "value2").build());
262+
.cookie("name1", "value1").cookie("name2", "value2").build());
264263
}
265264

266265
@Test
@@ -270,9 +269,10 @@ public void multipartPostWithNoSubmittedFileName() throws IOException {
270269
+ " 'metadata'@<(echo '{\"description\": \"foo\"}')");
271270
this.snippets.expectHttpieRequest()
272271
.withContents(codeBlock("bash").content(expectedContent));
273-
new HttpieRequestSnippet().document(this.operationBuilder
274-
.request("http://localhost/upload").method("POST")
275-
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
272+
new HttpieRequestSnippet().document(
273+
this.operationBuilder.request("http://localhost/upload").method("POST")
274+
.header(HttpHeaders.CONTENT_TYPE,
275+
MediaType.MULTIPART_FORM_DATA_VALUE)
276276
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
277277
}
278278

@@ -288,9 +288,9 @@ public void multipartPostWithContentType() throws IOException {
288288
this.operationBuilder.request("http://localhost/upload").method("POST")
289289
.header(HttpHeaders.CONTENT_TYPE,
290290
MediaType.MULTIPART_FORM_DATA_VALUE)
291-
.part("image", new byte[0])
292-
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
293-
.submittedFileName("documents/images/example.png").build());
291+
.part("image", new byte[0])
292+
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
293+
.submittedFileName("documents/images/example.png").build());
294294
}
295295

296296
@Test
@@ -304,8 +304,8 @@ public void multipartPost() throws IOException {
304304
this.operationBuilder.request("http://localhost/upload").method("POST")
305305
.header(HttpHeaders.CONTENT_TYPE,
306306
MediaType.MULTIPART_FORM_DATA_VALUE)
307-
.part("image", new byte[0])
308-
.submittedFileName("documents/images/example.png").build());
307+
.part("image", new byte[0])
308+
.submittedFileName("documents/images/example.png").build());
309309
}
310310

311311
@Test
@@ -320,9 +320,9 @@ public void multipartPostWithParameters() throws IOException {
320320
this.operationBuilder.request("http://localhost/upload").method("POST")
321321
.header(HttpHeaders.CONTENT_TYPE,
322322
MediaType.MULTIPART_FORM_DATA_VALUE)
323-
.part("image", new byte[0])
324-
.submittedFileName("documents/images/example.png").and()
325-
.param("a", "apple", "avocado").param("b", "banana").build());
323+
.part("image", new byte[0])
324+
.submittedFileName("documents/images/example.png").and()
325+
.param("a", "apple", "avocado").param("b", "banana").build());
326326
}
327327

328328
@Test
@@ -334,7 +334,7 @@ public void basicAuthCredentialsAreSuppliedUsingAuthOption() throws IOException
334334
.header(HttpHeaders.AUTHORIZATION,
335335
"Basic " + Base64Utils
336336
.encodeToString("user:secret".getBytes()))
337-
.build());
337+
.build());
338338
}
339339

340340
@Test

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

Lines changed: 7 additions & 14 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.
@@ -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").row("`Cookie`", "seven"));
57+
.row("`Connection`", "six"));
5858
new RequestHeadersSnippet(
5959
Arrays.asList(headerWithName("X-Test").description("one"),
6060
headerWithName("Accept").description("two"),
@@ -63,8 +63,7 @@ public void requestWithHeaders() throws IOException {
6363
headerWithName("Cache-Control").description("five"),
6464
headerWithName(
6565
"Connection")
66-
.description("six"),
67-
headerWithName("Cookie").description("seven")))
66+
.description("six")))
6867
.document(
6968
this.operationBuilder
7069
.request(
@@ -73,15 +72,9 @@ public void requestWithHeaders() throws IOException {
7372
.header("Accept", "*/*")
7473
.header("Accept-Encoding",
7574
"gzip, deflate")
76-
.header("Accept-Language",
77-
"en-US,en;q=0.5")
78-
.header("Cache-Control",
79-
"max-age=0")
80-
.header("Connection",
81-
"keep-alive")
82-
.header("Cookie",
83-
"cookie1=cookieVal1; cookie2=cookieVal2")
84-
.build());
75+
.header("Accept-Language", "en-US,en;q=0.5")
76+
.header("Cache-Control", "max-age=0")
77+
.header("Connection", "keep-alive").build());
8578
}
8679

8780
@Test
@@ -150,7 +143,7 @@ public void requestHeadersWithCustomDescriptorAttributes() throws IOException {
150143
.header("X-Test", "test")
151144
.header("Accept-Encoding",
152145
"gzip, deflate")
153-
.header("Accept", "*/*").build());
146+
.header("Accept", "*/*").build());
154147
}
155148

156149
@Test

0 commit comments

Comments
 (0)