Merge branch '1.0.x'

This commit is contained in:
Andy Wilkinson
2016-02-15 15:50:08 +00:00
7 changed files with 152 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
@@ -87,6 +87,17 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo?param=value").build());
}
@Test
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectCurlRequest("request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i"));
new CurlRequestSnippet()
.document(new OperationBuilder("request-with-query-string-with-no-value",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?param").build());
}
@Test
public void postRequestWithQueryString() throws IOException {
this.snippet.expectCurlRequest("post-request-with-query-string")
@@ -98,6 +109,18 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.build());
}
@Test
public void postRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectCurlRequest("post-request-with-query-string-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param' -i -X POST"));
new CurlRequestSnippet().document(
new OperationBuilder("post-request-with-query-string-with-no-value",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?param").method("POST")
.build());
}
@Test
public void postRequestWithOneParameter() throws IOException {
this.snippet.expectCurlRequest("post-request-with-one-parameter")
@@ -109,6 +132,17 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.build());
}
@Test
public void postRequestWithOneParameterWithNoValue() throws IOException {
this.snippet.expectCurlRequest("post-request-with-one-parameter-with-no-value")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1='"));
new CurlRequestSnippet().document(
new OperationBuilder("post-request-with-one-parameter-with-no-value",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1").build());
}
@Test
public void postRequestWithMultipleParameters() throws IOException {
this.snippet.expectCurlRequest("post-request-with-multiple-parameters")

View File

@@ -70,6 +70,18 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.request("http://localhost/foo?bar=baz").build());
}
@Test
public void getRequestWithQueryStringWithNoValue() throws IOException {
this.snippet.expectHttpRequest("get-request-with-query-string-with-no-value")
.withContents(httpRequest(RequestMethod.GET, "/foo?bar")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(
new OperationBuilder("get-request-with-query-string-with-no-value",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?bar").build());
}
@Test
public void postRequestWithContent() throws IOException {
String content = "Hello, world";
@@ -112,6 +124,20 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.param("a", "alpha").build());
}
@Test
public void postRequestWithParameterWithNoValue() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter")
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("bar="));
new HttpRequestSnippet()
.document(new OperationBuilder("post-request-with-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("bar").build());
}
@Test
public void putRequestWithContent() throws IOException {
String content = "Hello, world";
@@ -186,6 +212,30 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
.part("image", "<< data >>".getBytes()).build());
}
@Test
public void multipartPostWithParameterWithNoValue() throws IOException {
String paramPart = createPart(
String.format("Content-Disposition: form-data; " + "name=a%n"), false);
String filePart = createPart(String
.format("Content-Disposition: form-data; " + "name=image%n%n<< data >>"));
String expectedContent = paramPart + filePart;
this.snippet
.expectHttpRequest(
"multipart-post-with-parameter-with-no-value")
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet().document(
new OperationBuilder("multipart-post-with-parameter-with-no-value",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.param("a").part("image", "<< data >>".getBytes())
.build());
}
@Test
public void multipartPostWithContentType() throws IOException {
String expectedContent = createPart(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
@@ -58,6 +58,18 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests {
.param("b", "bravo").build());
}
@Test
public void requestParameterWithNoValue() throws IOException {
this.snippet.expectRequestParameters("request-parameter-with-no-value")
.withContents(
tableWithHeader("Parameter", "Description").row("a", "one"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("request-parameter-with-no-value",
this.snippet.getOutputDirectory())
.request("http://localhost").param("a").build());
}
@Test
public void ignoredRequestParameter() throws IOException {
this.snippet.expectRequestParameters("ignored-request-parameter").withContents(

View File

@@ -19,6 +19,7 @@ package org.springframework.restdocs.test;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -159,8 +160,13 @@ public class OperationBuilder {
}
public OperationRequestBuilder param(String name, String... values) {
for (String value : values) {
this.parameters.add(name, value);
if (values.length > 0) {
for (String value : values) {
this.parameters.add(name, value);
}
}
else {
this.parameters.put(name, Collections.<String>emptyList());
}
return this;
}