diff --git a/spring-restdocs-core/build.gradle b/spring-restdocs-core/build.gradle index 4a841707..60752f0b 100644 --- a/spring-restdocs-core/build.gradle +++ b/spring-restdocs-core/build.gradle @@ -27,7 +27,6 @@ task jmustacheRepackJar(type: Jar) { repackJar -> dependencies { compile 'com.fasterxml.jackson.core:jackson-databind' compile 'org.springframework:spring-web' - compile 'javax.servlet:javax.servlet-api' compile files(jmustacheRepackJar) jarjar 'com.googlecode.jarjar:jarjar:1.3' jmustache 'com.samskivert:jmustache@jar' 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 10ae0486..36bcb591 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 @@ -25,13 +25,12 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.util.Base64Utils; /** @@ -128,7 +127,7 @@ final class CliOperationRequest implements OperationRequest { } @Override - public Collection getCookies() { + public Collection getCookies() { return this.delegate.getCookies(); } 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 886a4c07..1fb6140a 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 @@ -23,13 +23,12 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpMethod; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.restdocs.snippet.Snippet; import org.springframework.restdocs.snippet.TemplatedSnippet; import org.springframework.util.CollectionUtils; @@ -105,7 +104,7 @@ public class CurlRequestSnippet extends TemplatedSnippet { private void writeCookies(CliOperationRequest request, PrintWriter printer) { if (!CollectionUtils.isEmpty(request.getCookies())) { StringBuilder cookiesBuilder = new StringBuilder(); - for (Cookie cookie : request.getCookies()) { + for (RequestCookie cookie : request.getCookies()) { if (cookiesBuilder.length() > 0) { cookiesBuilder.append(";"); } 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 38a8970b..410495fe 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 @@ -23,8 +23,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; @@ -32,6 +30,7 @@ import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.restdocs.snippet.Snippet; import org.springframework.restdocs.snippet.TemplatedSnippet; import org.springframework.util.StringUtils; @@ -166,7 +165,7 @@ public class HttpieRequestSnippet extends TemplatedSnippet { } private void writeCookies(OperationRequest request, PrintWriter writer) { - for (Cookie cookie : request.getCookies()) { + for (RequestCookie cookie : request.getCookies()) { writer.print(String.format(" 'Cookie:%s=%s'", cookie.getName(), cookie.getValue())); } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java index 8262571a..f72337af 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java @@ -24,8 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; @@ -33,11 +31,11 @@ import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.restdocs.snippet.Snippet; import org.springframework.restdocs.snippet.TemplatedSnippet; import org.springframework.util.StringUtils; - /** * A {@link Snippet} that documents an HTTP request. * @@ -116,8 +114,9 @@ public class HttpRequestSnippet extends TemplatedSnippet { } } - for (Cookie cookie : request.getCookies()) { - headers.add(header(HttpHeaders.COOKIE, String.format("%s=%s", cookie.getName(), cookie.getValue()))); + for (RequestCookie cookie : request.getCookies()) { + headers.add(header(HttpHeaders.COOKIE, + String.format("%s=%s", cookie.getName(), cookie.getValue()))); } if (requiresFormEncodingContentTypeHeader(request)) { 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 93972dba..f768663d 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 @@ -19,8 +19,6 @@ package org.springframework.restdocs.operation; import java.net.URI; import java.util.Collection; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -89,12 +87,12 @@ public interface OperationRequest { URI getUri(); /** - * Returns {@link Cookie Cookies} sent with the request. If no cookies were sent an - * empty collection is returned. + * Returns the {@link RequestCookie cookies} sent with the request. If no cookies were + * sent an empty collection is returned. * * @return the cookies, never {@code null} * @since 1.2.0 */ - Collection getCookies(); + Collection getCookies(); } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestFactory.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestFactory.java index 3f75e289..da1c07cf 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestFactory.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationRequestFactory.java @@ -20,8 +20,6 @@ import java.net.URI; import java.util.Collection; import java.util.Collections; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -48,8 +46,7 @@ public class OperationRequestFactory { */ public OperationRequest create(URI uri, HttpMethod method, byte[] content, HttpHeaders headers, Parameters parameters, - Collection parts, - Collection cookies) { + Collection parts, Collection cookies) { return new StandardOperationRequest(uri, method, content, augmentHeaders(headers, uri, content), parameters, parts, cookies); } @@ -70,7 +67,8 @@ public class OperationRequestFactory { public OperationRequest create(URI uri, HttpMethod method, byte[] content, HttpHeaders headers, Parameters parameters, Collection parts) { - return create(uri, method, content, headers, parameters, parts, Collections.emptyList()); + return create(uri, method, content, headers, parameters, parts, + Collections.emptyList()); } /** diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/RequestCookie.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/RequestCookie.java new file mode 100644 index 00000000..595a19f2 --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/RequestCookie.java @@ -0,0 +1,60 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.restdocs.operation; + +/** + * A representation of a Cookie received in a request. + * + * @author Andy Wilkinson + * @since 1.2.0 + */ +public final class RequestCookie { + + private final String name; + + private final String value; + + /** + * Creates a new {@code RequestCookie} with the given {@code name} and {@code value}. + * + * @param name the name of the cookie + * @param value the value of the cookie + */ + public RequestCookie(String name, String value) { + this.name = name; + this.value = value; + } + + /** + * Returns the name of the cookie. + * + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * Returns the value of the cookie. + * + * @return the value + */ + public String getValue() { + return this.value; + } + +} 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 8495c6c9..5fb57508 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 @@ -20,8 +20,6 @@ import java.net.URI; import java.util.Collection; import java.util.Collections; -import javax.servlet.http.Cookie; - import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -41,7 +39,7 @@ class StandardOperationRequest extends AbstractOperationMessage private URI uri; - private Collection cookies; + private Collection cookies; /** * Creates a new request with the given {@code uri} and {@code method}. The request @@ -58,7 +56,7 @@ class StandardOperationRequest extends AbstractOperationMessage */ 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; @@ -88,7 +86,7 @@ class StandardOperationRequest extends AbstractOperationMessage } @Override - public Collection getCookies() { + public Collection getCookies() { return this.cookies; } 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 6d210b5f..032b0845 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 @@ -25,8 +25,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.http.Cookie; - import org.junit.runners.model.Statement; import org.springframework.http.HttpHeaders; @@ -43,6 +41,7 @@ import org.springframework.restdocs.operation.OperationRequestPartFactory; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.OperationResponseFactory; import org.springframework.restdocs.operation.Parameters; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.restdocs.operation.StandardOperation; import org.springframework.restdocs.snippet.RestDocumentationContextPlaceholderResolverFactory; import org.springframework.restdocs.snippet.StandardWriterResolver; @@ -156,7 +155,7 @@ public class OperationBuilder extends OperationTestRule { private List partBuilders = new ArrayList<>(); - private Collection cookies = new ArrayList<>(); + private Collection cookies = new ArrayList<>(); private OperationRequestBuilder(String uri) { this.requestUri = URI.create(uri); @@ -215,7 +214,7 @@ public class OperationBuilder extends OperationTestRule { } public OperationRequestBuilder cookie(String name, String value) { - this.cookies.add(new Cookie(name, value)); + this.cookies.add(new RequestCookie(name, value)); return this; } diff --git a/spring-restdocs-mockmvc/build.gradle b/spring-restdocs-mockmvc/build.gradle index d8a0529c..5baca9c2 100644 --- a/spring-restdocs-mockmvc/build.gradle +++ b/spring-restdocs-mockmvc/build.gradle @@ -1,6 +1,7 @@ description = 'Spring REST Docs MockMvc' dependencies { + compile 'javax.servlet:javax.servlet-api' compile 'org.springframework:spring-test' compile project(':spring-restdocs-core') optional 'junit:junit' 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 2aeb6119..af66e401 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 @@ -21,14 +21,12 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.net.URI; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map.Entry; import javax.servlet.ServletException; -import javax.servlet.http.Cookie; import javax.servlet.http.Part; import org.springframework.http.HttpHeaders; @@ -43,6 +41,7 @@ import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.OperationRequestPartFactory; import org.springframework.restdocs.operation.Parameters; import org.springframework.restdocs.operation.RequestConverter; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.util.FileCopyUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; @@ -71,7 +70,7 @@ class MockMvcRequestConverter implements RequestConverter parts = extractParts(mockRequest); - Collection cookies = extractCookies(mockRequest); + Collection cookies = extractCookies(mockRequest); String queryString = mockRequest.getQueryString(); if (!StringUtils.hasText(queryString) && "GET".equals(mockRequest.getMethod())) { @@ -90,12 +89,16 @@ class MockMvcRequestConverter implements RequestConverter extractCookies(MockHttpServletRequest mockRequest) { - if (mockRequest.getCookies() != null) { - return Arrays.asList(mockRequest.getCookies()); + private Collection extractCookies(MockHttpServletRequest mockRequest) { + if (mockRequest.getCookies() == null || mockRequest.getCookies().length == 0) { + return Collections.emptyList(); } - - return Collections.emptyList(); + List cookies = new ArrayList<>(); + for (javax.servlet.http.Cookie servletCookie : mockRequest.getCookies()) { + cookies.add( + new RequestCookie(servletCookie.getName(), servletCookie.getValue())); + } + return cookies; } private List extractParts(MockHttpServletRequest servletRequest) 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 d2a18373..8b6c67eb 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 @@ -21,7 +21,6 @@ import java.net.URI; import java.util.Arrays; import java.util.Iterator; -import javax.servlet.http.Cookie; import javax.servlet.http.Part; import org.junit.Test; @@ -33,6 +32,7 @@ import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockServletContext; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -92,20 +92,22 @@ public class MockMvcRequestConverterTests { @Test public void requestWithCookies() throws Exception { - OperationRequest request = createOperationRequest(MockMvcRequestBuilders - .get("/foo").cookie(new Cookie("cookieName1", "cookieVal1"), - new Cookie("cookieName2", "cookieVal2"))); + OperationRequest request = createOperationRequest( + MockMvcRequestBuilders.get("/foo") + .cookie(new javax.servlet.http.Cookie("cookieName1", + "cookieVal1"), + new javax.servlet.http.Cookie("cookieName2", "cookieVal2"))); assertThat(request.getUri(), is(URI.create("http://localhost/foo"))); assertThat(request.getMethod(), is(HttpMethod.GET)); assertThat(request.getCookies().size(), is(equalTo(2))); - Iterator cookieIterator = request.getCookies().iterator(); + Iterator cookieIterator = request.getCookies().iterator(); - Cookie cookie1 = cookieIterator.next(); + RequestCookie cookie1 = cookieIterator.next(); assertThat(cookie1.getName(), is(equalTo("cookieName1"))); assertThat(cookie1.getValue(), is(equalTo("cookieVal1"))); - Cookie cookie2 = cookieIterator.next(); + RequestCookie cookie2 = cookieIterator.next(); assertThat(cookie2.getName(), is(equalTo("cookieName2"))); assertThat(cookie2.getValue(), is(equalTo("cookieVal2"))); } 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 6b64c6a3..852db3a2 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 @@ -39,6 +39,7 @@ import org.springframework.restdocs.operation.OperationRequestPart; import org.springframework.restdocs.operation.OperationRequestPartFactory; import org.springframework.restdocs.operation.Parameters; import org.springframework.restdocs.operation.RequestConverter; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils; @@ -60,12 +61,11 @@ class RestAssuredRequestConverter extractCookies(requestSpec)); } - private Collection extractCookies( + private Collection extractCookies( FilterableRequestSpecification requestSpec) { - Collection cookies = new ArrayList<>(); + Collection cookies = new ArrayList<>(); for (Cookie cookie : requestSpec.getCookies()) { - cookies.add( - new javax.servlet.http.Cookie(cookie.getName(), cookie.getValue())); + cookies.add(new RequestCookie(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 4617cb30..906b8647 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 @@ -25,8 +25,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; -import javax.servlet.http.Cookie; - import com.jayway.restassured.RestAssured; import com.jayway.restassured.specification.FilterableRequestSpecification; import com.jayway.restassured.specification.RequestSpecification; @@ -44,6 +42,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.restdocs.operation.OperationRequest; import org.springframework.restdocs.operation.OperationRequestPart; +import org.springframework.restdocs.operation.RequestCookie; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -154,13 +153,13 @@ public class RestAssuredRequestConverterTests { .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getCookies().size(), is(equalTo(2))); - Iterator cookieIterator = request.getCookies().iterator(); - Cookie cookie1 = cookieIterator.next(); + Iterator cookieIterator = request.getCookies().iterator(); + RequestCookie cookie1 = cookieIterator.next(); assertThat(cookie1.getName(), is(equalTo("cookie1"))); assertThat(cookie1.getValue(), is(equalTo("cookieVal1"))); - Cookie cookie2 = cookieIterator.next(); + RequestCookie cookie2 = cookieIterator.next(); assertThat(cookie2.getName(), is(equalTo("cookie2"))); assertThat(cookie2.getValue(), is(equalTo("cookieVal2"))); }