From a9c1e04081971d06eacef8109a47688e72753420 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 2 Feb 2017 14:42:43 +0000 Subject: [PATCH] Polish "Include cookies in request snippets" See gh-336 Closes gh-302 Closes gh-303 Closes gh-304 --- .../restdocs/cli/CliOperationRequest.java | 2 +- .../restdocs/cli/CurlRequestSnippet.java | 18 ++++---- .../restdocs/cli/HttpieRequestSnippet.java | 8 +++- .../restdocs/operation/OperationRequest.java | 8 ++-- .../operation/StandardOperationRequest.java | 9 ++-- .../restdocs/cli/CurlRequestSnippetTests.java | 34 +++++++-------- .../cli/HttpieRequestSnippetTests.java | 34 +++++++-------- .../headers/RequestHeadersSnippetTests.java | 21 ++++------ .../http/HttpRequestSnippetTests.java | 41 +++++++++---------- .../restdocs/test/OperationBuilder.java | 2 +- .../mockmvc/MockMvcRequestConverter.java | 2 +- .../mockmvc/MockMvcRequestConverterTests.java | 5 +-- ...kMvcRestDocumentationIntegrationTests.java | 13 +++--- .../RestAssuredRequestConverter.java | 10 ++--- .../RestAssuredRequestConverterTests.java | 5 +-- ...uredRestDocumentationIntegrationTests.java | 12 +++--- 16 files changed, 107 insertions(+), 117 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java index 22a6b2a4..10ae0486 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CliOperationRequest.java @@ -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"); * you may not use this file except in compliance with the License. diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java index 50809790..886a4c07 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/CurlRequestSnippet.java @@ -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"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.Parameters; import org.springframework.restdocs.snippet.Snippet; import org.springframework.restdocs.snippet.TemplatedSnippet; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -102,17 +103,16 @@ public class CurlRequestSnippet extends TemplatedSnippet { } private void writeCookies(CliOperationRequest request, PrintWriter printer) { - if (request.getCookies() != null && request.getCookies().size() > 0) { - printer.print(" --cookie "); + if (!CollectionUtils.isEmpty(request.getCookies())) { StringBuilder cookiesBuilder = new StringBuilder(); - for (Cookie cookie : request.getCookies()) { - cookiesBuilder.append(String.format("%s=%s;", cookie.getName(), cookie.getValue())); + if (cookiesBuilder.length() > 0) { + cookiesBuilder.append(";"); + } + cookiesBuilder.append( + String.format("%s=%s", cookie.getName(), cookie.getValue())); } - - String cookiesHeader = cookiesBuilder.substring(0, cookiesBuilder.length() - 1); // remove trailing semicolon - - printer.print(String.format("'%s'", cookiesHeader)); // add single quotes + printer.print(String.format(" --cookie '%s'", cookiesBuilder.toString())); } } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java index 6fa07f05..38a8970b 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/cli/HttpieRequestSnippet.java @@ -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"); * you may not use this file except in compliance with the License. @@ -108,6 +108,7 @@ public class HttpieRequestSnippet extends TemplatedSnippet { PrintWriter printer = new PrintWriter(requestItems); writeFormDataIfNecessary(request, printer); writeHeaders(request, printer); + writeCookies(request, printer); writeParametersIfNecessary(request, printer); return requestItems.toString(); } @@ -162,9 +163,12 @@ public class HttpieRequestSnippet extends TemplatedSnippet { writer.print(String.format(" '%s:%s'", entry.getKey(), header)); } } + } + private void writeCookies(OperationRequest request, PrintWriter writer) { for (Cookie cookie : request.getCookies()) { - writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(), cookie.getValue())); + writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(), + cookie.getValue())); } } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java index 50d0e7bd..93972dba 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -89,9 +89,11 @@ public interface OperationRequest { URI getUri(); /** - * Returns cookies sent with the request. + * Returns {@link Cookie Cookies} sent with the request. If no cookies were sent an + * empty collection is returned. * - * @return the cookies + * @return the cookies, never {@code null} + * @since 1.2.0 */ Collection getCookies(); diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java index 594534ce..8495c6c9 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,8 @@ class StandardOperationRequest extends AbstractOperationMessage /** * Creates a new request with the given {@code uri} and {@code method}. The request - * will have the given {@code headers}, {@code parameters}, and {@code parts}. + * will have the given {@code headers}, {@code parameters}, {@code parts}, and + * {@code cookies}. * * @param uri the uri * @param method the method @@ -53,11 +54,11 @@ class StandardOperationRequest extends AbstractOperationMessage * @param headers the headers * @param parameters the parameters * @param parts the parts + * @param cookies the cookies */ StandardOperationRequest(URI uri, HttpMethod method, byte[] content, HttpHeaders headers, Parameters parameters, - Collection parts, - Collection cookies) { + Collection parts, Collection cookies) { super(content, headers); this.uri = uri; this.method = method; diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java index b5a23a5a..33d6aa88 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/CurlRequestSnippetTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -254,12 +254,11 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { @Test public void requestWithCookies() throws IOException { this.snippets.expectCurlRequest() - .withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i" + - " --cookie 'name1=value1;name2=value2'")); + .withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i" + + " --cookie 'name1=value1;name2=value2'")); new CurlRequestSnippet() .document(this.operationBuilder.request("http://localhost/foo") - .cookie("name1", "value1") - .cookie("name2", "value2").build()); + .cookie("name1", "value1").cookie("name2", "value2").build()); } @Test @@ -269,9 +268,10 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { + "'metadata={\"description\": \"foo\"}'"; this.snippets.expectCurlRequest() .withContents(codeBlock("bash").content(expectedContent)); - new CurlRequestSnippet().document(this.operationBuilder - .request("http://localhost/upload").method("POST") - .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) + new CurlRequestSnippet().document( + this.operationBuilder.request("http://localhost/upload").method("POST") + .header(HttpHeaders.CONTENT_TYPE, + MediaType.MULTIPART_FORM_DATA_VALUE) .part("metadata", "{\"description\": \"foo\"}".getBytes()).build()); } @@ -286,9 +286,9 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) - .submittedFileName("documents/images/example.png").build()); + .part("image", new byte[0]) + .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) + .submittedFileName("documents/images/example.png").build()); } @Test @@ -302,8 +302,8 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .submittedFileName("documents/images/example.png").build()); + .part("image", new byte[0]) + .submittedFileName("documents/images/example.png").build()); } @Test @@ -318,9 +318,9 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .submittedFileName("documents/images/example.png").and() - .param("a", "apple", "avocado").param("b", "banana").build()); + .part("image", new byte[0]) + .submittedFileName("documents/images/example.png").and() + .param("a", "apple", "avocado").param("b", "banana").build()); } @Test @@ -332,7 +332,7 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests { .header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils .encodeToString("user:secret".getBytes())) - .build()); + .build()); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java index 8cf564d5..acb12cb9 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/cli/HttpieRequestSnippetTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -255,12 +255,11 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { @Test public void requestWithCookies() throws IOException { this.snippets.expectHttpieRequest().withContents( - codeBlock("bash").content("$ http GET 'http://localhost/foo'" + - " 'Cookie:name1=value1' 'Cookie:name2=value2'")); + codeBlock("bash").content("$ http GET 'http://localhost/foo'" + + " 'Cookie:name1=value1' 'Cookie:name2=value2'")); new HttpieRequestSnippet() .document(this.operationBuilder.request("http://localhost/foo") - .cookie("name1", "value1") - .cookie("name2", "value2").build()); + .cookie("name1", "value1").cookie("name2", "value2").build()); } @Test @@ -270,9 +269,10 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { + " 'metadata'@<(echo '{\"description\": \"foo\"}')"); this.snippets.expectHttpieRequest() .withContents(codeBlock("bash").content(expectedContent)); - new HttpieRequestSnippet().document(this.operationBuilder - .request("http://localhost/upload").method("POST") - .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) + new HttpieRequestSnippet().document( + this.operationBuilder.request("http://localhost/upload").method("POST") + .header(HttpHeaders.CONTENT_TYPE, + MediaType.MULTIPART_FORM_DATA_VALUE) .part("metadata", "{\"description\": \"foo\"}".getBytes()).build()); } @@ -288,9 +288,9 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) - .submittedFileName("documents/images/example.png").build()); + .part("image", new byte[0]) + .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) + .submittedFileName("documents/images/example.png").build()); } @Test @@ -304,8 +304,8 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .submittedFileName("documents/images/example.png").build()); + .part("image", new byte[0]) + .submittedFileName("documents/images/example.png").build()); } @Test @@ -320,9 +320,9 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", new byte[0]) - .submittedFileName("documents/images/example.png").and() - .param("a", "apple", "avocado").param("b", "banana").build()); + .part("image", new byte[0]) + .submittedFileName("documents/images/example.png").and() + .param("a", "apple", "avocado").param("b", "banana").build()); } @Test @@ -334,7 +334,7 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests { .header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils .encodeToString("user:secret".getBytes())) - .build()); + .build()); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java index d5c73bcb..1f28ea11 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/headers/RequestHeadersSnippetTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests { .row("`X-Test`", "one").row("`Accept`", "two") .row("`Accept-Encoding`", "three") .row("`Accept-Language`", "four").row("`Cache-Control`", "five") - .row("`Connection`", "six").row("`Cookie`", "seven")); + .row("`Connection`", "six")); new RequestHeadersSnippet( Arrays.asList(headerWithName("X-Test").description("one"), headerWithName("Accept").description("two"), @@ -63,8 +63,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests { headerWithName("Cache-Control").description("five"), headerWithName( "Connection") - .description("six"), - headerWithName("Cookie").description("seven"))) + .description("six"))) .document( this.operationBuilder .request( @@ -73,15 +72,9 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests { .header("Accept", "*/*") .header("Accept-Encoding", "gzip, deflate") - .header("Accept-Language", - "en-US,en;q=0.5") - .header("Cache-Control", - "max-age=0") - .header("Connection", - "keep-alive") - .header("Cookie", - "cookie1=cookieVal1; cookie2=cookieVal2") - .build()); + .header("Accept-Language", "en-US,en;q=0.5") + .header("Cache-Control", "max-age=0") + .header("Connection", "keep-alive").build()); } @Test @@ -150,7 +143,7 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests { .header("X-Test", "test") .header("Accept-Encoding", "gzip, deflate") - .header("Accept", "*/*").build()); + .header("Accept", "*/*").build()); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java index 2e64467e..4312770e 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -90,9 +90,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { new HttpRequestSnippet() .document(this.operationBuilder.request("http://localhost/foo") - .cookie("name1", "value1") - .cookie("name2", "value2") - .build()); + .cookie("name1", "value1").cookie("name2", "value2").build()); } @Test @@ -142,8 +140,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.POST, "/foo") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet().document(this.operationBuilder .request("http://localhost/foo").method("POST").content(content).build()); @@ -154,8 +152,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.POST, "/foo?a=alpha") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet() .document(this.operationBuilder.request("http://localhost/foo") @@ -168,8 +166,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet() .document(this.operationBuilder.request("http://localhost/foo?b=bravo") @@ -182,8 +180,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet().document(this.operationBuilder .request("http://localhost/foo?b=bravo").method("POST") @@ -196,8 +194,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.POST, "/foo?b=bravo&a=alpha") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet().document(this.operationBuilder .request("http://localhost/foo?b=bravo&a=alpha").method("POST") @@ -251,8 +249,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { String content = "Hello, world"; this.snippets.expectHttpRequest() .withContents(httpRequest(RequestMethod.PUT, "/foo") - .header(HttpHeaders.HOST, "localhost").content(content).header( - HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); + .header(HttpHeaders.HOST, "localhost").content(content) + .header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length)); new HttpRequestSnippet().document(this.operationBuilder .request("http://localhost/foo").method("PUT").content(content).build()); @@ -284,7 +282,7 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", "<< data >>".getBytes()).build()); + .part("image", "<< data >>".getBytes()).build()); } @Test @@ -310,8 +308,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .param("a", "apple", "avocado").param("b", "banana") - .part("image", "<< data >>".getBytes()).build()); + .param("a", "apple", "avocado").param("b", "banana") + .part("image", "<< data >>".getBytes()).build()); } @Test @@ -347,9 +345,8 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests { this.operationBuilder.request("http://localhost/upload").method("POST") .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE) - .part("image", "<< data >>".getBytes()) - .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE) - .build()); + .part("image", "<< data >>".getBytes()) + .header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE).build()); } @Test diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java index 4bfa58af..6d210b5f 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/test/OperationBuilder.java @@ -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"); * you may not use this file except in compliance with the License. diff --git a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java index 2d86da0a..2aeb6119 100644 --- a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverter.java @@ -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"); * you may not use this file except in compliance with the License. diff --git a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverterTests.java b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverterTests.java index 501752a5..d2a18373 100644 --- a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverterTests.java +++ b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRequestConverterTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -93,8 +93,7 @@ public class MockMvcRequestConverterTests { @Test public void requestWithCookies() throws Exception { OperationRequest request = createOperationRequest(MockMvcRequestBuilders - .get("/foo") - .cookie(new Cookie("cookieName1", "cookieVal1"), + .get("/foo").cookie(new Cookie("cookieName1", "cookieVal1"), new Cookie("cookieName2", "cookieVal2"))); assertThat(request.getUri(), is(URI.create("http://localhost/foo"))); assertThat(request.getMethod(), is(HttpMethod.GET)); diff --git a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java index a7e56314..77b2136a 100644 --- a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java +++ b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java @@ -166,10 +166,9 @@ public class MockMvcRestDocumentationIntegrationTests { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); - mockMvc.perform(get("/") - .accept(MediaType.APPLICATION_JSON) - .cookie(new Cookie("cookieName", "cookieVal"))) - .andExpect(status().isOk()).andDo(document("curl-snippet-with-cookies")); + mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON) + .cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk()) + .andDo(document("curl-snippet-with-cookies")); assertThat( new File( "build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"), @@ -231,10 +230,8 @@ public class MockMvcRestDocumentationIntegrationTests { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(documentationConfiguration(this.restDocumentation)).build(); - mockMvc.perform(get("/") - .accept(MediaType.APPLICATION_JSON) - .cookie(new Cookie("cookieName", "cookieVal"))) - .andExpect(status().isOk()) + mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON) + .cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk()) .andDo(document("httpie-snippet-with-cookies")); assertThat( new File( diff --git a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredRequestConverter.java b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredRequestConverter.java index eff63a4c..6b64c6a3 100644 --- a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredRequestConverter.java +++ b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredRequestConverter.java @@ -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"); * you may not use this file except in compliance with the License. @@ -60,13 +60,13 @@ class RestAssuredRequestConverter extractCookies(requestSpec)); } - private Collection extractCookies(FilterableRequestSpecification requestSpec) { + private Collection extractCookies( + FilterableRequestSpecification requestSpec) { Collection cookies = new ArrayList<>(); - for (Cookie cookie : requestSpec.getCookies()) { - cookies.add(new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue())); + cookies.add( + new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue())); } - return cookies; } diff --git a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java index 4db14321..4617cb30 100644 --- a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java +++ b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -148,8 +148,7 @@ public class RestAssuredRequestConverterTests { @Test public void cookies() { RequestSpecification requestSpec = RestAssured.given().port(this.port) - .cookie("cookie1", "cookieVal1") - .cookie("cookie2", "cookieVal2"); + .cookie("cookie1", "cookieVal1").cookie("cookie2", "cookieVal2"); requestSpec.get("/"); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); diff --git a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRestDocumentationIntegrationTests.java b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRestDocumentationIntegrationTests.java index 087003d9..07d62682 100644 --- a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRestDocumentationIntegrationTests.java +++ b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRestDocumentationIntegrationTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -124,17 +124,15 @@ public class RestAssuredRestDocumentationIntegrationTests { String contentType = "text/plain; charset=UTF-8"; given().port(this.port).filter(documentationConfiguration(this.restDocumentation)) .filter(document("curl-snippet-with-cookies")).accept("application/json") - .contentType(contentType) - .cookie("cookieName", "cookieVal").get("/") - .then() - .statusCode(200); + .contentType(contentType).cookie("cookieName", "cookieVal").get("/") + .then().statusCode(200); assertThat( new File( "build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"), is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash") .content("$ curl 'http://localhost:" + this.port + "/' -i " - + "-H 'Accept: application/json' " - + "-H 'Content-Type: " + contentType + "' " + + "-H 'Accept: application/json' " + "-H 'Content-Type: " + + contentType + "' " + "--cookie 'cookieName=cookieVal'")))); }