Allow responses with non-standard status codes to be documented

Fixes gh-639
This commit is contained in:
Andy Wilkinson
2019-09-12 17:03:21 +01:00
parent d586d9f15b
commit 3597ebc35a
17 changed files with 107 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2019 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.
@@ -30,10 +30,18 @@ public interface OperationResponse {
/**
* Returns the status of the response.
* @return the status
* @return the status or {@code null} if the status is unknown to {@link HttpStatus}
*/
HttpStatus getStatus();
/**
* Returns the status code of the response.
* @return the status code
*/
default int getStatusCode() {
throw new UnsupportedOperationException();
}
/**
* Returns the headers in the response.
* @return the headers

View File

@@ -34,8 +34,14 @@ public class OperationResponseFactory {
* @param headers the request's headers
* @param content the content of the request
* @return the {@code OperationResponse}
* @deprecated since 2.0.4 in favor of {@link #create(int, HttpHeaders, byte[])}
*/
@Deprecated
public OperationResponse create(HttpStatus status, HttpHeaders headers, byte[] content) {
return this.create(status.value(), headers, content);
}
public OperationResponse create(int status, HttpHeaders headers, byte[] content) {
return new StandardOperationResponse(status, augmentHeaders(headers, content), content);
}
@@ -49,8 +55,8 @@ public class OperationResponseFactory {
* @return the new response with the new content
*/
public OperationResponse createFrom(OperationResponse original, byte[] newContent) {
return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent),
newContent);
return new StandardOperationResponse(original.getStatusCode(),
getUpdatedHeaders(original.getHeaders(), newContent), newContent);
}
/**
@@ -61,7 +67,7 @@ public class OperationResponseFactory {
* @return the new response with the new headers
*/
public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) {
return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent());
return new StandardOperationResponse(original.getStatusCode(), newHeaders, original.getContent());
}
private HttpHeaders augmentHeaders(HttpHeaders originalHeaders, byte[] content) {

View File

@@ -26,7 +26,7 @@ import org.springframework.http.HttpStatus;
*/
class StandardOperationResponse extends AbstractOperationMessage implements OperationResponse {
private final HttpStatus status;
private final int status;
/**
* Creates a new response with the given {@code status}, {@code headers}, and
@@ -35,13 +35,18 @@ class StandardOperationResponse extends AbstractOperationMessage implements Oper
* @param headers the headers of the response
* @param content the content of the response
*/
StandardOperationResponse(HttpStatus status, HttpHeaders headers, byte[] content) {
StandardOperationResponse(int status, HttpHeaders headers, byte[] content) {
super(content, headers);
this.status = status;
}
@Override
public HttpStatus getStatus() {
return HttpStatus.resolve(this.status);
}
@Override
public int getStatusCode() {
return this.status;
}

View File

@@ -141,7 +141,7 @@ public class UriModifyingOperationPreprocessor implements OperationPreprocessor
@Override
public OperationResponse preprocess(OperationResponse response) {
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatus(),
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatusCode(),
modify(response.getHeaders()), response.getContent()));
}

View File

@@ -69,7 +69,7 @@ public class RestDocumentationGeneratorTests {
private final OperationRequest operationRequest = new OperationRequestFactory()
.create(URI.create("http://localhost:8080"), null, null, new HttpHeaders(), null, null);
private final OperationResponse operationResponse = new OperationResponseFactory().create(null, null, null);
private final OperationResponse operationResponse = new OperationResponseFactory().create(0, null, null);
private final Snippet snippet = mock(Snippet.class);
@@ -197,7 +197,7 @@ public class RestDocumentationGeneratorTests {
}
private static OperationResponse createResponse() {
return new OperationResponseFactory().create(null, null, null);
return new OperationResponseFactory().create(0, null, null);
}
}

View File

@@ -217,7 +217,7 @@ public class RestDocumentationConfigurerTests {
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR);
HttpHeaders headers = new HttpHeaders();
headers.add("Foo", "value");
OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK, headers, null);
OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK.value(), headers, null);
assertThat(preprocessor.preprocess(response).getHeaders()).doesNotContainKey("Foo");
}

View File

@@ -49,7 +49,7 @@ public class ContentTypeLinkExtractorTests {
public void extractionFailsWithNullContentType() throws IOException {
this.thrown.expect(IllegalStateException.class);
new ContentTypeLinkExtractor()
.extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null));
.extractLinks(this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), null));
}
@Test
@@ -59,7 +59,7 @@ public class ContentTypeLinkExtractorTests {
extractors.put(MediaType.APPLICATION_JSON, extractor);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null);
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), httpHeaders, null);
new ContentTypeLinkExtractor(extractors).extractLinks(response);
verify(extractor).extractLinks(response);
}
@@ -71,7 +71,7 @@ public class ContentTypeLinkExtractorTests {
extractors.put(MediaType.APPLICATION_JSON, extractor);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.parseMediaType("application/json;foo=bar"));
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null);
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), httpHeaders, null);
new ContentTypeLinkExtractor(extractors).extractLinks(response);
verify(extractor).extractLinks(response);
}

View File

@@ -106,7 +106,7 @@ public class LinkExtractorsPayloadTests {
}
private OperationResponse createResponse(String contentName) throws IOException {
return this.responseFactory.create(HttpStatus.OK, null,
return this.responseFactory.create(HttpStatus.OK.value(), null,
FileCopyUtils.copyToByteArray(getPayloadFile(contentName)));
}

View File

@@ -67,7 +67,7 @@ public class ContentModifyingOperationPreprocessorTests {
@Test
public void modifyResponseContent() {
OperationResponse response = this.responseFactory.create(HttpStatus.OK, new HttpHeaders(),
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(),
"content".getBytes());
OperationResponse preprocessed = this.preprocessor.preprocess(response);
assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes());

View File

@@ -87,7 +87,7 @@ public class HeaderRemovingOperationPreprocessorTests {
}
private OperationResponse createResponse(String... extraHeaders) {
return this.responseFactory.create(HttpStatus.OK, getHttpHeaders(extraHeaders), new byte[0]);
return this.responseFactory.create(HttpStatus.OK.value(), getHttpHeaders(extraHeaders), new byte[0]);
}
private HttpHeaders getHttpHeaders(String... extraHeaders) {

View File

@@ -321,13 +321,13 @@ public class UriModifyingOperationPreprocessorTests {
}
private OperationResponse createResponseWithContent(String content) {
return this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), content.getBytes());
return this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), content.getBytes());
}
private OperationResponse createResponseWithHeader(String name, String value) {
HttpHeaders headers = new HttpHeaders();
headers.add(name, value);
return this.responseFactory.create(HttpStatus.OK, headers, new byte[0]);
return this.responseFactory.create(HttpStatus.OK.value(), headers, new byte[0]);
}
}

View File

@@ -261,7 +261,7 @@ public class OperationBuilder extends OperationTestRule {
*/
public final class OperationResponseBuilder {
private HttpStatus status = HttpStatus.OK;
private int status = HttpStatus.OK.value();
private HttpHeaders headers = new HttpHeaders();
@@ -272,7 +272,7 @@ public class OperationBuilder extends OperationTestRule {
}
public OperationResponseBuilder status(int status) {
this.status = HttpStatus.valueOf(status);
this.status = status;
return this;
}