Include cookies in request snippets
Closes gh-336 See gh-302, gh-303, gh-304
This commit is contained in:
committed by
Andy Wilkinson
parent
0337160e80
commit
25bc6c37da
@@ -251,6 +251,17 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
|
||||
.header("a", "alpha").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithCookies() throws IOException {
|
||||
this.snippets.expectCurlRequest()
|
||||
.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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartPostWithNoSubmittedFileName() throws IOException {
|
||||
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
|
||||
|
||||
@@ -252,6 +252,17 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
|
||||
.header("a", "alpha").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithCookies() throws IOException {
|
||||
this.snippets.expectHttpieRequest().withContents(
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartPostWithNoSubmittedFileName() throws IOException {
|
||||
String expectedContent = String
|
||||
|
||||
@@ -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("`Connection`", "six").row("`Cookie`", "seven"));
|
||||
new RequestHeadersSnippet(
|
||||
Arrays.asList(headerWithName("X-Test").description("one"),
|
||||
headerWithName("Accept").description("two"),
|
||||
@@ -63,7 +63,8 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
headerWithName("Cache-Control").description("five"),
|
||||
headerWithName(
|
||||
"Connection")
|
||||
.description("six")))
|
||||
.description("six"),
|
||||
headerWithName("Cookie").description("seven")))
|
||||
.document(
|
||||
this.operationBuilder
|
||||
.request(
|
||||
@@ -78,6 +79,8 @@ public class RequestHeadersSnippetTests extends AbstractSnippetTests {
|
||||
"max-age=0")
|
||||
.header("Connection",
|
||||
"keep-alive")
|
||||
.header("Cookie",
|
||||
"cookie1=cookieVal1; cookie2=cookieVal2")
|
||||
.build());
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,21 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
|
||||
.request("http://localhost:8080/foo").header("Alpha", "a").build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestWithCookies() throws IOException {
|
||||
this.snippets.expectHttpRequest()
|
||||
.withContents(httpRequest(RequestMethod.GET, "/foo")
|
||||
.header(HttpHeaders.HOST, "localhost")
|
||||
.header(HttpHeaders.COOKIE, "name1=value1")
|
||||
.header(HttpHeaders.COOKIE, "name2=value2"));
|
||||
|
||||
new HttpRequestSnippet()
|
||||
.document(this.operationBuilder.request("http://localhost/foo")
|
||||
.cookie("name1", "value1")
|
||||
.cookie("name2", "value2")
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryString() throws IOException {
|
||||
this.snippets.expectHttpRequest()
|
||||
|
||||
@@ -19,11 +19,14 @@ package org.springframework.restdocs.test;
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
@@ -153,6 +156,8 @@ public class OperationBuilder extends OperationTestRule {
|
||||
|
||||
private List<OperationRequestPartBuilder> partBuilders = new ArrayList<>();
|
||||
|
||||
private Collection<Cookie> cookies = new ArrayList<>();
|
||||
|
||||
private OperationRequestBuilder(String uri) {
|
||||
this.requestUri = URI.create(uri);
|
||||
}
|
||||
@@ -163,7 +168,7 @@ public class OperationBuilder extends OperationTestRule {
|
||||
parts.add(builder.buildPart());
|
||||
}
|
||||
return new OperationRequestFactory().create(this.requestUri, this.method,
|
||||
this.content, this.headers, this.parameters, parts);
|
||||
this.content, this.headers, this.parameters, parts, this.cookies);
|
||||
}
|
||||
|
||||
public Operation build() {
|
||||
@@ -209,6 +214,11 @@ public class OperationBuilder extends OperationTestRule {
|
||||
return partBuilder;
|
||||
}
|
||||
|
||||
public OperationRequestBuilder cookie(String name, String value) {
|
||||
this.cookies.add(new Cookie(name, value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic builder API for creating an {@link OperationRequestPart}.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user