Merge branch '1.2.x'
This commit is contained in:
@@ -36,12 +36,7 @@ import org.springframework.restdocs.operation.RequestCookie;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.hasEntry;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@@ -58,8 +53,8 @@ public class MockMvcRequestConverterTests {
|
||||
public void httpRequest() throws Exception {
|
||||
OperationRequest request = createOperationRequest(
|
||||
MockMvcRequestBuilders.get("/foo"));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -68,26 +63,27 @@ public class MockMvcRequestConverterTests {
|
||||
.buildRequest(new MockServletContext());
|
||||
mockRequest.setServerPort(8080);
|
||||
OperationRequest request = this.factory.convert(mockRequest);
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost:8080/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithContextPath() throws Exception {
|
||||
OperationRequest request = createOperationRequest(
|
||||
MockMvcRequestBuilders.get("/foo/bar").contextPath("/foo"));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo/bar")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo/bar"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestWithHeaders() throws Exception {
|
||||
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
|
||||
.get("/foo").header("a", "alpha", "apple").header("b", "bravo"));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getHeaders(), hasEntry("a", Arrays.asList("alpha", "apple")));
|
||||
assertThat(request.getHeaders(), hasEntry("b", Arrays.asList("bravo")));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
assertThat(request.getHeaders()).containsEntry("a",
|
||||
Arrays.asList("alpha", "apple"));
|
||||
assertThat(request.getHeaders()).containsEntry("b", Arrays.asList("bravo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,19 +92,19 @@ public class MockMvcRequestConverterTests {
|
||||
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)));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
assertThat(request.getCookies().size()).isEqualTo(2);
|
||||
|
||||
Iterator<RequestCookie> cookieIterator = request.getCookies().iterator();
|
||||
|
||||
RequestCookie cookie1 = cookieIterator.next();
|
||||
assertThat(cookie1.getName(), is(equalTo("cookieName1")));
|
||||
assertThat(cookie1.getValue(), is(equalTo("cookieVal1")));
|
||||
assertThat(cookie1.getName()).isEqualTo("cookieName1");
|
||||
assertThat(cookie1.getValue()).isEqualTo("cookieVal1");
|
||||
|
||||
RequestCookie cookie2 = cookieIterator.next();
|
||||
assertThat(cookie2.getName(), is(equalTo("cookieName2")));
|
||||
assertThat(cookie2.getValue(), is(equalTo("cookieVal2")));
|
||||
assertThat(cookie2.getName()).isEqualTo("cookieName2");
|
||||
assertThat(cookie2.getValue()).isEqualTo("cookieVal2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,8 +114,8 @@ public class MockMvcRequestConverterTests {
|
||||
mockRequest.setScheme("https");
|
||||
mockRequest.setServerPort(443);
|
||||
OperationRequest request = this.factory.convert(mockRequest);
|
||||
assertThat(request.getUri(), is(URI.create("https://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,45 +125,45 @@ public class MockMvcRequestConverterTests {
|
||||
mockRequest.setScheme("https");
|
||||
mockRequest.setServerPort(8443);
|
||||
OperationRequest request = this.factory.convert(mockRequest);
|
||||
assertThat(request.getUri(), is(URI.create("https://localhost:8443/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("https://localhost:8443/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestWithParametersProducesUriWithQueryString() throws Exception {
|
||||
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
|
||||
.get("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
|
||||
assertThat(request.getUri(),
|
||||
is(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo")));
|
||||
assertThat(request.getParameters().size(), is(2));
|
||||
assertThat(request.getParameters(),
|
||||
hasEntry("a", Arrays.asList("alpha", "apple")));
|
||||
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri())
|
||||
.isEqualTo(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"));
|
||||
assertThat(request.getParameters().size()).isEqualTo(2);
|
||||
assertThat(request.getParameters()).containsEntry("a",
|
||||
Arrays.asList("alpha", "apple"));
|
||||
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestWithQueryStringPopulatesParameters() throws Exception {
|
||||
OperationRequest request = createOperationRequest(
|
||||
MockMvcRequestBuilders.get("/foo?a=alpha&b=bravo"));
|
||||
assertThat(request.getUri(),
|
||||
is(URI.create("http://localhost/foo?a=alpha&b=bravo")));
|
||||
assertThat(request.getParameters().size(), is(2));
|
||||
assertThat(request.getParameters(), hasEntry("a", Arrays.asList("alpha")));
|
||||
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("bravo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.GET));
|
||||
assertThat(request.getUri())
|
||||
.isEqualTo(URI.create("http://localhost/foo?a=alpha&b=bravo"));
|
||||
assertThat(request.getParameters().size()).isEqualTo(2);
|
||||
assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha"));
|
||||
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("bravo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postRequestWithParameters() throws Exception {
|
||||
OperationRequest request = createOperationRequest(MockMvcRequestBuilders
|
||||
.post("/foo").param("a", "alpha", "apple").param("b", "br&vo"));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.POST));
|
||||
assertThat(request.getParameters().size(), is(2));
|
||||
assertThat(request.getParameters(),
|
||||
hasEntry("a", Arrays.asList("alpha", "apple")));
|
||||
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
assertThat(request.getParameters().size()).isEqualTo(2);
|
||||
assertThat(request.getParameters()).containsEntry("a",
|
||||
Arrays.asList("alpha", "apple"));
|
||||
assertThat(request.getParameters()).containsEntry("b", Arrays.asList("br&vo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -175,15 +171,15 @@ public class MockMvcRequestConverterTests {
|
||||
OperationRequest request = createOperationRequest(
|
||||
MockMvcRequestBuilders.multipart("/foo")
|
||||
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.POST));
|
||||
assertThat(request.getParts().size(), is(1));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
assertThat(request.getParts().size()).isEqualTo(1);
|
||||
OperationRequestPart part = request.getParts().iterator().next();
|
||||
assertThat(part.getName(), is(equalTo("file")));
|
||||
assertThat(part.getSubmittedFileName(), is(nullValue()));
|
||||
assertThat(part.getHeaders().size(), is(1));
|
||||
assertThat(part.getHeaders().getContentLength(), is(4L));
|
||||
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(part.getName()).isEqualTo("file");
|
||||
assertThat(part.getSubmittedFileName()).isNull();
|
||||
assertThat(part.getHeaders().size()).isEqualTo(1);
|
||||
assertThat(part.getHeaders().getContentLength()).isEqualTo(4L);
|
||||
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -191,14 +187,14 @@ public class MockMvcRequestConverterTests {
|
||||
OperationRequest request = createOperationRequest(
|
||||
MockMvcRequestBuilders.multipart("/foo").file(new MockMultipartFile(
|
||||
"file", "original", "image/png", new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
|
||||
assertThat(request.getMethod(), is(HttpMethod.POST));
|
||||
assertThat(request.getParts().size(), is(1));
|
||||
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
|
||||
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
|
||||
assertThat(request.getParts().size()).isEqualTo(1);
|
||||
OperationRequestPart part = request.getParts().iterator().next();
|
||||
assertThat(part.getName(), is(equalTo("file")));
|
||||
assertThat(part.getSubmittedFileName(), is(equalTo("original")));
|
||||
assertThat(part.getHeaders().getContentType(), is(MediaType.IMAGE_PNG));
|
||||
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(part.getName()).isEqualTo("file");
|
||||
assertThat(part.getSubmittedFileName()).isEqualTo("original");
|
||||
assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
|
||||
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -215,14 +211,14 @@ public class MockMvcRequestConverterTests {
|
||||
given(mockPart.getSubmittedFileName()).willReturn("submitted.txt");
|
||||
mockRequest.addPart(mockPart);
|
||||
OperationRequest request = this.factory.convert(mockRequest);
|
||||
assertThat(request.getParts().size(), is(1));
|
||||
assertThat(request.getParts().size()).isEqualTo(1);
|
||||
OperationRequestPart part = request.getParts().iterator().next();
|
||||
assertThat(part.getName(), is(equalTo("part-name")));
|
||||
assertThat(part.getSubmittedFileName(), is(equalTo("submitted.txt")));
|
||||
assertThat(part.getHeaders().getContentType(), is(nullValue()));
|
||||
assertThat(part.getHeaders().get("a"), contains("alpha"));
|
||||
assertThat(part.getHeaders().get("b"), contains("bravo", "banana"));
|
||||
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(part.getName()).isEqualTo("part-name");
|
||||
assertThat(part.getSubmittedFileName()).isEqualTo("submitted.txt");
|
||||
assertThat(part.getHeaders().getContentType()).isNull();
|
||||
assertThat(part.getHeaders().get("a")).containsExactly("alpha");
|
||||
assertThat(part.getHeaders().get("b")).containsExactly("bravo", "banana");
|
||||
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -240,14 +236,14 @@ public class MockMvcRequestConverterTests {
|
||||
given(mockPart.getContentType()).willReturn("image/png");
|
||||
mockRequest.addPart(mockPart);
|
||||
OperationRequest request = this.factory.convert(mockRequest);
|
||||
assertThat(request.getParts().size(), is(1));
|
||||
assertThat(request.getParts().size()).isEqualTo(1);
|
||||
OperationRequestPart part = request.getParts().iterator().next();
|
||||
assertThat(part.getName(), is(equalTo("part-name")));
|
||||
assertThat(part.getSubmittedFileName(), is(equalTo("submitted.png")));
|
||||
assertThat(part.getHeaders().getContentType(), is(MediaType.IMAGE_PNG));
|
||||
assertThat(part.getHeaders().get("a"), contains("alpha"));
|
||||
assertThat(part.getHeaders().get("b"), contains("bravo", "banana"));
|
||||
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
|
||||
assertThat(part.getName()).isEqualTo("part-name");
|
||||
assertThat(part.getSubmittedFileName()).isEqualTo("submitted.png");
|
||||
assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
|
||||
assertThat(part.getHeaders().get("a")).containsExactly("alpha");
|
||||
assertThat(part.getHeaders().get("b")).containsExactly("bravo", "banana");
|
||||
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
|
||||
}
|
||||
|
||||
private OperationRequest createOperationRequest(MockHttpServletRequestBuilder builder)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2018 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.
|
||||
@@ -28,10 +28,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.restdocs.operation.OperationResponse;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockMvcResponseConverter}.
|
||||
@@ -46,29 +43,22 @@ public class MockMvcResponseConverterTests {
|
||||
public void basicResponse() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
OperationResponse operationResponse = this.factory.convert(response);
|
||||
|
||||
assertThat(operationResponse.getStatus(), is(HttpStatus.OK));
|
||||
assertThat(operationResponse.getStatus()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithCookie() {
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
Cookie cookie = new Cookie("name", "value");
|
||||
cookie.setDomain("localhost");
|
||||
cookie.setHttpOnly(true);
|
||||
|
||||
response.addCookie(cookie);
|
||||
|
||||
OperationResponse operationResponse = this.factory.convert(response);
|
||||
|
||||
assertThat(operationResponse.getHeaders().size(), is(1));
|
||||
assertTrue(operationResponse.getHeaders().containsKey(HttpHeaders.SET_COOKIE));
|
||||
assertThat(operationResponse.getHeaders().get(HttpHeaders.SET_COOKIE), equalTo(
|
||||
Collections.singletonList("name=value; Domain=localhost; HttpOnly")));
|
||||
assertThat(operationResponse.getHeaders()).hasSize(1);
|
||||
assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE,
|
||||
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2018 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.
|
||||
@@ -28,10 +28,7 @@ import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockMvcRestDocumentationConfigurer}.
|
||||
@@ -51,7 +48,6 @@ public class MockMvcRestDocumentationConfigurerTests {
|
||||
RequestPostProcessor postProcessor = new MockMvcRestDocumentationConfigurer(
|
||||
this.restDocumentation).beforeMockMvcCreated(null, null);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
|
||||
assertUriConfiguration("http", "localhost", 8080);
|
||||
}
|
||||
|
||||
@@ -61,7 +57,6 @@ public class MockMvcRestDocumentationConfigurerTests {
|
||||
this.restDocumentation).uris().withScheme("https")
|
||||
.beforeMockMvcCreated(null, null);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
|
||||
assertUriConfiguration("https", "localhost", 8080);
|
||||
}
|
||||
|
||||
@@ -71,7 +66,6 @@ public class MockMvcRestDocumentationConfigurerTests {
|
||||
this.restDocumentation).uris().withHost("api.example.com")
|
||||
.beforeMockMvcCreated(null, null);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
|
||||
assertUriConfiguration("http", "api.example.com", 8080);
|
||||
}
|
||||
|
||||
@@ -81,7 +75,6 @@ public class MockMvcRestDocumentationConfigurerTests {
|
||||
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
|
||||
null);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
|
||||
assertUriConfiguration("http", "localhost", 8081);
|
||||
}
|
||||
|
||||
@@ -91,20 +84,20 @@ public class MockMvcRestDocumentationConfigurerTests {
|
||||
this.restDocumentation).uris().withPort(8081).beforeMockMvcCreated(null,
|
||||
null);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
assertThat(this.request.getHeader("Content-Length"), is(nullValue()));
|
||||
assertThat(this.request.getHeader("Content-Length")).isNull();
|
||||
}
|
||||
|
||||
private void assertUriConfiguration(String scheme, String host, int port) {
|
||||
assertEquals(scheme, this.request.getScheme());
|
||||
assertEquals(host, this.request.getServerName());
|
||||
assertEquals(port, this.request.getServerPort());
|
||||
assertThat(scheme).isEqualTo(this.request.getScheme());
|
||||
assertThat(host).isEqualTo(this.request.getServerName());
|
||||
assertThat(port).isEqualTo(this.request.getServerPort());
|
||||
RequestContextHolder
|
||||
.setRequestAttributes(new ServletRequestAttributes(this.request));
|
||||
try {
|
||||
URI uri = BasicLinkBuilder.linkToCurrentMapping().toUri();
|
||||
assertEquals(scheme, uri.getScheme());
|
||||
assertEquals(host, uri.getHost());
|
||||
assertEquals(port, uri.getPort());
|
||||
assertThat(scheme).isEqualTo(uri.getScheme());
|
||||
assertThat(host).isEqualTo(uri.getHost());
|
||||
assertThat(port).isEqualTo(uri.getPort());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
|
||||
@@ -17,8 +17,12 @@
|
||||
package org.springframework.restdocs.mockmvc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -28,6 +32,7 @@ import java.util.regex.Pattern;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -43,13 +48,14 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationIntegrationTests.TestConfiguration;
|
||||
import org.springframework.restdocs.test.SnippetMatchers.HttpRequestMatcher;
|
||||
import org.springframework.restdocs.test.SnippetConditions.HttpRequestCondition;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
@@ -57,11 +63,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.restdocs.cli.CliDocumentation.curlRequest;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
|
||||
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
|
||||
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
|
||||
@@ -85,14 +88,11 @@ import static org.springframework.restdocs.request.RequestDocumentation.partWith
|
||||
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
|
||||
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
|
||||
import static org.springframework.restdocs.request.RequestDocumentation.requestParts;
|
||||
import static org.springframework.restdocs.snippet.Attributes.attributes;
|
||||
import static org.springframework.restdocs.snippet.Attributes.key;
|
||||
import static org.springframework.restdocs.templates.TemplateFormats.asciidoctor;
|
||||
import static org.springframework.restdocs.templates.TemplateFormats.markdown;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.httpRequest;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.snippet;
|
||||
import static org.springframework.restdocs.test.SnippetConditions.codeBlock;
|
||||
import static org.springframework.restdocs.test.SnippetConditions.httpRequest;
|
||||
import static org.springframework.restdocs.test.SnippetConditions.httpResponse;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@@ -132,7 +132,6 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
|
||||
.snippets().withEncoding("UTF-8"))
|
||||
.build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("basic"));
|
||||
assertExpectedSnippetFilesExist(new File("build/generated-snippets/basic"),
|
||||
@@ -145,7 +144,6 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.apply(new MockMvcRestDocumentationConfigurer(this.restDocumentation)
|
||||
.snippets().withEncoding("UTF-8").withTemplateFormat(markdown()))
|
||||
.build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("basic-markdown"));
|
||||
assertExpectedSnippetFilesExist(
|
||||
@@ -157,33 +155,29 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void curlSnippetWithContent() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
|
||||
.andExpect(status().isOk()).andDo(document("curl-snippet-with-content"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String
|
||||
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(String
|
||||
.format("$ curl 'http://localhost:8080/' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'content'")))));
|
||||
+ " -d 'content'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithCookies() throws Exception {
|
||||
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"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(codeBlock(asciidoctor(), "bash").content(String
|
||||
"build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(String
|
||||
.format("$ curl 'http://localhost:8080/' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " --cookie 'cookieName=cookieVal'")))));
|
||||
+ " --cookie 'cookieName=cookieVal'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -194,12 +188,12 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
|
||||
.andDo(document("curl-snippet-with-query-string"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
codeBlock(asciidoctor(), "bash").content(String.format("$ curl "
|
||||
+ "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'a=alpha'")))));
|
||||
"build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash")
|
||||
.withContent(String.format("$ curl "
|
||||
+ "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'a=alpha'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -210,44 +204,41 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.content("some content")).andExpect(status().isOk())
|
||||
.andDo(document("curl-snippet-with-content-and-parameters"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
codeBlock(asciidoctor(), "bash").content(String.format(
|
||||
"$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
|
||||
"build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(String
|
||||
.format("$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
|
||||
+ " -H 'Accept: application/json' \\%n"
|
||||
+ " -d 'some content'")))));
|
||||
+ " -d 'some content'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpieSnippetWithContent() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("httpie-snippet-with-content"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ echo 'content' | "
|
||||
+ "http POST 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json'")))));
|
||||
"build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash")
|
||||
.withContent(String.format("$ echo 'content' | "
|
||||
+ "http POST 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpieSnippetWithCookies() throws Exception {
|
||||
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("httpie-snippet-with-cookies"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ http GET 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json' \\%n"
|
||||
+ " 'Cookie:cookieName=cookieVal'")))));
|
||||
"build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(
|
||||
String.format("$ http GET 'http://localhost:8080/' \\%n"
|
||||
+ " 'Accept:application/json' \\%n"
|
||||
+ " 'Cookie:cookieName=cookieVal'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -258,11 +249,11 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
|
||||
.andDo(document("httpie-snippet-with-query-string"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
codeBlock(asciidoctor(), "bash").content(String.format("$ http "
|
||||
+ "--form POST 'http://localhost:8080/?foo=bar' \\%n"
|
||||
+ " 'Accept:application/json' \\%n 'a=alpha'")))));
|
||||
"build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash")
|
||||
.withContent(String.format("$ http "
|
||||
+ "--form POST 'http://localhost:8080/?foo=bar' \\%n"
|
||||
+ " 'Accept:application/json' \\%n 'a=alpha'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -273,18 +264,17 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
|
||||
.andDo(document("httpie-snippet-post-with-content-and-parameters"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(codeBlock(asciidoctor(), "bash")
|
||||
.content(String.format("$ echo " + "'some content' | http POST "
|
||||
+ "'http://localhost:8080/?a=alpha' \\%n"
|
||||
+ " 'Accept:application/json'")))));
|
||||
"build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(
|
||||
String.format("$ echo " + "'some content' | http POST "
|
||||
+ "'http://localhost:8080/?a=alpha' \\%n"
|
||||
+ " 'Accept:application/json'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void linksSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("links",
|
||||
links(linkWithRel("rel").description("The description"))));
|
||||
@@ -298,11 +288,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void pathParametersSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("{foo}", "/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("links", pathParameters(
|
||||
parameterWithName("foo").description("The description"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
|
||||
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
|
||||
"path-parameters.adoc");
|
||||
@@ -312,11 +300,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void requestParametersSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("links", requestParameters(
|
||||
parameterWithName("foo").description("The description"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
|
||||
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
|
||||
"request-parameters.adoc");
|
||||
@@ -326,12 +312,10 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void requestFieldsSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/").param("foo", "bar").content("{\"a\":\"alpha\"}")
|
||||
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
|
||||
.andDo(document("links", requestFields(
|
||||
fieldWithPath("a").description("The description"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
|
||||
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
|
||||
"request-fields.adoc");
|
||||
@@ -341,11 +325,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void requestPartsSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(multipart("/upload").file("foo", "bar".getBytes()))
|
||||
.andExpect(status().isOk()).andDo(document("request-parts", requestParts(
|
||||
partWithName("foo").description("The description"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(
|
||||
new File("build/generated-snippets/request-parts"), "http-request.adoc",
|
||||
"http-response.adoc", "curl-request.adoc", "request-parts.adoc");
|
||||
@@ -355,14 +337,12 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void responseFieldsSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("links",
|
||||
responseFields(fieldWithPath("a").description("The description"),
|
||||
subsectionWithPath("links")
|
||||
.description("Links to other resources"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
|
||||
"http-request.adoc", "http-response.adoc", "curl-request.adoc",
|
||||
"response-fields.adoc");
|
||||
@@ -372,24 +352,20 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void responseWithSetCookie() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/set-cookie")).andExpect(status().isOk())
|
||||
.andDo(document("set-cookie",
|
||||
responseHeaders(headerWithName(HttpHeaders.SET_COOKIE)
|
||||
.description("set-cookie"))));
|
||||
|
||||
assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(httpResponse(asciidoctor(), HttpStatus.OK).header(
|
||||
HttpHeaders.SET_COOKIE,
|
||||
"name=value; Domain=localhost; HttpOnly"))));
|
||||
assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
|
||||
.has(content(httpResponse(asciidoctor(), HttpStatus.OK).header(
|
||||
HttpHeaders.SET_COOKIE,
|
||||
"name=value; Domain=localhost; HttpOnly")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parameterizedOutputDirectory() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("{method-name}"));
|
||||
assertExpectedSnippetFilesExist(
|
||||
@@ -402,13 +378,11 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation))
|
||||
.alwaysDo(document("{method-name}-{step}")).build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
assertExpectedSnippetFilesExist(
|
||||
new File("build/generated-snippets/multi-step-1/"), "http-request.adoc",
|
||||
"http-response.adoc", "curl-request.adoc");
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
assertExpectedSnippetFilesExist(
|
||||
@@ -428,11 +402,9 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation))
|
||||
.alwaysDo(documentation).build();
|
||||
|
||||
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(documentation.document(
|
||||
responseHeaders(headerWithName("a").description("one"))));
|
||||
|
||||
assertExpectedSnippetFilesExist(
|
||||
new File(
|
||||
"build/generated-snippets/always-do-with-additional-snippets-1/"),
|
||||
@@ -444,9 +416,7 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
public void preprocessedRequest() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
|
||||
MvcResult result = mockMvc
|
||||
.perform(get("/").header("a", "alpha").header("b", "bravo")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
@@ -458,18 +428,17 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
HttpHeaders.CONTENT_LENGTH),
|
||||
replacePattern(pattern, "\"<<beta>>\""))))
|
||||
.andReturn();
|
||||
|
||||
HttpRequestMatcher originalRequest = httpRequest(asciidoctor(), RequestMethod.GET,
|
||||
"/");
|
||||
HttpRequestCondition originalRequest = httpRequest(asciidoctor(),
|
||||
RequestMethod.GET, "/");
|
||||
for (String headerName : iterable(result.getRequest().getHeaderNames())) {
|
||||
originalRequest.header(headerName, result.getRequest().getHeader(headerName));
|
||||
}
|
||||
assertThat(
|
||||
new File("build/generated-snippets/original-request/http-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(originalRequest
|
||||
.header("Host", "localhost:8080").header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}"))));
|
||||
HttpRequestMatcher preprocessedRequest = httpRequest(asciidoctor(),
|
||||
new File("build/generated-snippets/original-request/http-request.adoc"))
|
||||
.has(content(originalRequest.header("Host", "localhost:8080")
|
||||
.header("Content-Length", "13")
|
||||
.content("{\"a\":\"alpha\"}")));
|
||||
HttpRequestCondition preprocessedRequest = httpRequest(asciidoctor(),
|
||||
RequestMethod.GET, "/");
|
||||
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
|
||||
HttpHeaders.CONTENT_LENGTH);
|
||||
@@ -481,9 +450,8 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
}
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/preprocessed-request/http-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(preprocessedRequest.content(prettyPrinted))));
|
||||
"build/generated-snippets/preprocessed-request/http-request.adoc"))
|
||||
.has(content(preprocessedRequest.content(prettyPrinted)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -503,7 +471,7 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
|
||||
.andDo(document("default-preprocessed-request")).andReturn();
|
||||
|
||||
HttpRequestMatcher preprocessedRequest = httpRequest(asciidoctor(),
|
||||
HttpRequestCondition preprocessedRequest = httpRequest(asciidoctor(),
|
||||
RequestMethod.GET, "/");
|
||||
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
|
||||
HttpHeaders.CONTENT_LENGTH);
|
||||
@@ -515,9 +483,8 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
}
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/default-preprocessed-request/http-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(preprocessedRequest.content(prettyPrinted))));
|
||||
"build/generated-snippets/default-preprocessed-request/http-request.adoc"))
|
||||
.has(content(preprocessedRequest.content(prettyPrinted)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -536,23 +503,22 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
String original = "{\"a\":\"alpha\",\"links\":[{\"rel\":\"rel\","
|
||||
+ "\"href\":\"href\"}]}";
|
||||
assertThat(
|
||||
new File("build/generated-snippets/original-response/http-response.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
httpResponse(asciidoctor(), HttpStatus.OK).header("a", "alpha")
|
||||
new File("build/generated-snippets/original-response/http-response.adoc"))
|
||||
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
.header("a", "alpha")
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
original.getBytes().length)
|
||||
.content(original))));
|
||||
.content(original)));
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
|
||||
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/preprocessed-response/http-response.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
"build/generated-snippets/preprocessed-response/http-response.adoc"))
|
||||
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
prettyPrinted.getBytes().length)
|
||||
.content(prettyPrinted))));
|
||||
.content(prettyPrinted)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -572,20 +538,18 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
|
||||
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/default-preprocessed-response/http-response.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
"build/generated-snippets/default-preprocessed-response/http-response.adoc"))
|
||||
.has(content(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
prettyPrinted.getBytes().length)
|
||||
.content(prettyPrinted))));
|
||||
.content(prettyPrinted)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customSnippetTemplate() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
ClassLoader classLoader = new URLClassLoader(new URL[] {
|
||||
new File("src/test/resources/custom-snippet-templates").toURI().toURL() },
|
||||
getClass().getClassLoader());
|
||||
@@ -600,11 +564,8 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
Thread.currentThread().setContextClassLoader(previous);
|
||||
}
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/custom-snippet-template/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(equalTo("Custom curl request"))));
|
||||
|
||||
mockMvc.perform(get("/")).andDo(document("index", curlRequest(
|
||||
attributes(key("title").value("Access the index using curl")))));
|
||||
"build/generated-snippets/custom-snippet-template/curl-request.adoc"))
|
||||
.hasContent("Custom curl request");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -615,13 +576,11 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
mockMvc.perform(
|
||||
get("/custom/").contextPath("/custom").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("custom-context-path"));
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/custom-context-path/curl-request.adoc"),
|
||||
is(snippet(asciidoctor()).withContents(
|
||||
codeBlock(asciidoctor(), "bash").content(String.format(
|
||||
"$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json'")))));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/custom-context-path/curl-request.adoc"))
|
||||
.has(content(codeBlock(asciidoctor(), "bash").withContent(String
|
||||
.format("$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
|
||||
+ " -H 'Accept: application/json'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -635,10 +594,29 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
|
||||
private void assertExpectedSnippetFilesExist(File directory, String... snippets) {
|
||||
for (String snippet : snippets) {
|
||||
assertTrue(new File(directory, snippet).isFile());
|
||||
assertThat(new File(directory, snippet)).isFile();
|
||||
}
|
||||
}
|
||||
|
||||
private Condition<File> content(final Condition<String> delegate) {
|
||||
return new Condition<File>() {
|
||||
|
||||
@Override
|
||||
public boolean matches(File value) {
|
||||
try {
|
||||
return delegate
|
||||
.matches(FileCopyUtils.copyToString(new InputStreamReader(
|
||||
new FileInputStream(value), StandardCharsets.UTF_8)));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
fail("Failed to read '" + value + "'", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test configuration that enables Spring MVC.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2018 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.
|
||||
@@ -28,9 +28,7 @@ import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.restdocs.generate.RestDocumentationGenerator;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
|
||||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.fileUpload;
|
||||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
|
||||
@@ -144,18 +142,17 @@ public class RestDocumentationRequestBuildersTests {
|
||||
private void assertTemplate(MockHttpServletRequestBuilder builder,
|
||||
HttpMethod httpMethod) {
|
||||
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
|
||||
assertThat(
|
||||
(String) request.getAttribute(
|
||||
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE),
|
||||
is(equalTo("{template}")));
|
||||
assertThat(request.getRequestURI(), is(equalTo("t")));
|
||||
assertThat(request.getMethod(), is(equalTo(httpMethod.name())));
|
||||
assertThat((String) request
|
||||
.getAttribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE))
|
||||
.isEqualTo("{template}");
|
||||
assertThat(request.getRequestURI()).isEqualTo("t");
|
||||
assertThat(request.getMethod()).isEqualTo(httpMethod.name());
|
||||
}
|
||||
|
||||
private void assertUri(MockHttpServletRequestBuilder builder, HttpMethod httpMethod) {
|
||||
MockHttpServletRequest request = builder.buildRequest(this.servletContext);
|
||||
assertThat(request.getRequestURI(), is(equalTo("/uri")));
|
||||
assertThat(request.getMethod(), is(equalTo(httpMethod.name())));
|
||||
assertThat(request.getRequestURI()).isEqualTo("/uri");
|
||||
assertThat(request.getMethod()).isEqualTo(httpMethod.name());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user