Skip to content

Commit 03b61ab

Browse files
committed
Use Framework's HttpStatusCode to model response's status
Closes gh-848
1 parent b107d0e commit 03b61ab

File tree

20 files changed

+58
-66
lines changed

20 files changed

+58
-66
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.Map.Entry;
2424

2525
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.HttpStatusCode;
2627
import org.springframework.restdocs.operation.Operation;
2728
import org.springframework.restdocs.operation.OperationResponse;
2829
import org.springframework.restdocs.snippet.Snippet;
@@ -59,15 +60,9 @@ protected Map<String, Object> createModel(Operation operation) {
5960
Map<String, Object> model = new HashMap<>();
6061
model.put("responseBody", responseBody(response));
6162
model.put("headers", headers(response));
62-
HttpStatus status = response.getStatus();
63-
if (status != null) {
64-
model.put("statusCode", status.value());
65-
model.put("statusReason", status.getReasonPhrase());
66-
}
67-
else {
68-
model.put("statusCode", response.getStatusCode());
69-
model.put("statusReason", "");
70-
}
63+
HttpStatusCode status = response.getStatus();
64+
model.put("statusCode", status.value());
65+
model.put("statusReason", (status instanceof HttpStatus) ? ((HttpStatus) status).getReasonPhrase() : "");
7166
return model;
7267
}
7368

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Collection;
2020

2121
import org.springframework.http.HttpHeaders;
22-
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.HttpStatusCode;
2323

2424
/**
2525
* The response that was received as part of performing an operation on a RESTful service.
@@ -33,17 +33,9 @@ public interface OperationResponse {
3333

3434
/**
3535
* Returns the status of the response.
36-
* @return the status or {@code null} if the status is unknown to {@link HttpStatus}
36+
* @return the status, never {@code null}
3737
*/
38-
HttpStatus getStatus();
39-
40-
/**
41-
* Returns the status code of the response.
42-
* @return the status code
43-
*/
44-
default int getStatusCode() {
45-
throw new UnsupportedOperationException();
46-
}
38+
HttpStatusCode getStatus();
4739

4840
/**
4941
* Returns the headers in the response.

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponseFactory.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121

2222
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpStatusCode;
2324

2425
/**
2526
* A factory for creating {@link OperationResponse OperationResponses}.
@@ -37,8 +38,9 @@ public class OperationResponseFactory {
3738
* @param headers the request's headers
3839
* @param content the content of the request
3940
* @return the {@code OperationResponse}
41+
* @since 3.0.0
4042
*/
41-
public OperationResponse create(int status, HttpHeaders headers, byte[] content) {
43+
public OperationResponse create(HttpStatusCode status, HttpHeaders headers, byte[] content) {
4244
return new StandardOperationResponse(status, augmentHeaders(headers, content), content,
4345
Collections.emptyList());
4446
}
@@ -52,9 +54,9 @@ public OperationResponse create(int status, HttpHeaders headers, byte[] content)
5254
* @param content the content of the request
5355
* @param cookies the cookies
5456
* @return the {@code OperationResponse}
55-
* @since 3.0
57+
* @since 3.0.0
5658
*/
57-
public OperationResponse create(int status, HttpHeaders headers, byte[] content,
59+
public OperationResponse create(HttpStatusCode status, HttpHeaders headers, byte[] content,
5860
Collection<ResponseCookie> cookies) {
5961
return new StandardOperationResponse(status, augmentHeaders(headers, content), content, cookies);
6062
}
@@ -69,8 +71,8 @@ public OperationResponse create(int status, HttpHeaders headers, byte[] content,
6971
* @return the new response with the new content
7072
*/
7173
public OperationResponse createFrom(OperationResponse original, byte[] newContent) {
72-
return new StandardOperationResponse(original.getStatusCode(),
73-
getUpdatedHeaders(original.getHeaders(), newContent), newContent, original.getCookies());
74+
return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent),
75+
newContent, original.getCookies());
7476
}
7577

7678
/**
@@ -81,7 +83,7 @@ public OperationResponse createFrom(OperationResponse original, byte[] newConten
8183
* @return the new response with the new headers
8284
*/
8385
public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) {
84-
return new StandardOperationResponse(original.getStatusCode(), newHeaders, original.getContent(),
86+
return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent(),
8587
original.getCookies());
8688
}
8789

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Collection;
2020

2121
import org.springframework.http.HttpHeaders;
22-
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.HttpStatusCode;
2323

2424
/**
2525
* Standard implementation of {@link OperationResponse}.
@@ -29,7 +29,7 @@
2929
*/
3030
class StandardOperationResponse extends AbstractOperationMessage implements OperationResponse {
3131

32-
private final int status;
32+
private final HttpStatusCode status;
3333

3434
private Collection<ResponseCookie> cookies;
3535

@@ -41,19 +41,15 @@ class StandardOperationResponse extends AbstractOperationMessage implements Oper
4141
* @param content the content of the response
4242
* @param cookies any cookies included in the response
4343
*/
44-
StandardOperationResponse(int status, HttpHeaders headers, byte[] content, Collection<ResponseCookie> cookies) {
44+
StandardOperationResponse(HttpStatusCode status, HttpHeaders headers, byte[] content,
45+
Collection<ResponseCookie> cookies) {
4546
super(content, headers);
4647
this.status = status;
4748
this.cookies = cookies;
4849
}
4950

5051
@Override
51-
public HttpStatus getStatus() {
52-
return HttpStatus.resolve(this.status);
53-
}
54-
55-
@Override
56-
public int getStatusCode() {
52+
public HttpStatusCode getStatus() {
5753
return this.status;
5854
}
5955

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public OperationRequest preprocess(OperationRequest request) {
141141

142142
@Override
143143
public OperationResponse preprocess(OperationResponse response) {
144-
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatusCode(),
144+
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatus(),
145145
modify(response.getHeaders()), response.getContent(), response.getCookies()));
146146
}
147147

spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.mockito.Mockito;
2929

3030
import org.springframework.http.HttpHeaders;
31+
import org.springframework.http.HttpStatus;
3132
import org.springframework.restdocs.generate.RestDocumentationGenerator;
3233
import org.springframework.restdocs.operation.Operation;
3334
import org.springframework.restdocs.operation.OperationRequest;
@@ -69,7 +70,8 @@ public class RestDocumentationGeneratorTests {
6970
private final OperationRequest operationRequest = new OperationRequestFactory()
7071
.create(URI.create("http://localhost:8080"), null, null, new HttpHeaders(), null, null);
7172

72-
private final OperationResponse operationResponse = new OperationResponseFactory().create(0, null, null);
73+
private final OperationResponse operationResponse = new OperationResponseFactory().create(HttpStatus.OK, null,
74+
null);
7375

7476
private final Snippet snippet = mock(Snippet.class);
7577

@@ -197,7 +199,7 @@ private static OperationRequest createRequest() {
197199
}
198200

199201
private static OperationResponse createResponse() {
200-
return new OperationResponseFactory().create(0, null, null);
202+
return new OperationResponseFactory().create(HttpStatus.OK, null, null);
201203
}
202204

203205
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void customDefaultOperationResponsePreprocessor() {
223223
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR);
224224
HttpHeaders headers = new HttpHeaders();
225225
headers.add("Foo", "value");
226-
OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK.value(), headers, null);
226+
OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK, headers, null);
227227
assertThat(preprocessor.preprocess(response).getHeaders()).doesNotContainKey("Foo");
228228
}
229229

spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.http.HttpHeaders;
2424
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.HttpStatusCode;
2526
import org.springframework.http.MediaType;
2627
import org.springframework.restdocs.AbstractSnippetTests;
2728
import org.springframework.restdocs.templates.TemplateEngine;
@@ -55,8 +56,7 @@ public void basicResponse() throws IOException {
5556

5657
@Test
5758
public void nonOkResponse() throws IOException {
58-
new HttpResponseSnippet()
59-
.document(this.operationBuilder.response().status(HttpStatus.BAD_REQUEST.value()).build());
59+
new HttpResponseSnippet().document(this.operationBuilder.response().status(HttpStatus.BAD_REQUEST).build());
6060
assertThat(this.generatedSnippets.httpResponse()).is(httpResponse(HttpStatus.BAD_REQUEST));
6161
}
6262

@@ -99,7 +99,8 @@ public void responseWithCustomSnippetAttributes() throws IOException {
9999

100100
@Test
101101
public void responseWithCustomStatus() throws IOException {
102-
new HttpResponseSnippet().document(this.operationBuilder.response().status(215).build());
102+
new HttpResponseSnippet()
103+
.document(this.operationBuilder.response().status(HttpStatusCode.valueOf(215)).build());
103104
assertThat(this.generatedSnippets.httpResponse()).is(httpResponse(215));
104105
}
105106

spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public class ContentTypeLinkExtractorTests {
4949
public void extractionFailsWithNullContentType() throws IOException {
5050
this.thrown.expect(IllegalStateException.class);
5151
new ContentTypeLinkExtractor()
52-
.extractLinks(this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), null));
52+
.extractLinks(this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null));
5353
}
5454

5555
@Test
@@ -59,7 +59,7 @@ public void extractorCalledWithMatchingContextType() throws IOException {
5959
extractors.put(MediaType.APPLICATION_JSON, extractor);
6060
HttpHeaders httpHeaders = new HttpHeaders();
6161
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
62-
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), httpHeaders, null);
62+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null);
6363
new ContentTypeLinkExtractor(extractors).extractLinks(response);
6464
verify(extractor).extractLinks(response);
6565
}
@@ -71,7 +71,7 @@ public void extractorCalledWithCompatibleContextType() throws IOException {
7171
extractors.put(MediaType.APPLICATION_JSON, extractor);
7272
HttpHeaders httpHeaders = new HttpHeaders();
7373
httpHeaders.setContentType(MediaType.parseMediaType("application/json;foo=bar"));
74-
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), httpHeaders, null);
74+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null);
7575
new ContentTypeLinkExtractor(extractors).extractLinks(response);
7676
verify(extractor).extractLinks(response);
7777
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,7 +106,7 @@ private void assertLinks(List<Link> expectedLinks, Map<String, List<Link>> actua
106106
}
107107

108108
private OperationResponse createResponse(String contentName) throws IOException {
109-
return this.responseFactory.create(HttpStatus.OK.value(), null,
109+
return this.responseFactory.create(HttpStatus.OK, null,
110110
FileCopyUtils.copyToByteArray(getPayloadFile(contentName)));
111111
}
112112

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void modifyRequestContent() {
6565

6666
@Test
6767
public void modifyResponseContent() {
68-
OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(),
68+
OperationResponse response = this.responseFactory.create(HttpStatus.OK, new HttpHeaders(),
6969
"content".getBytes());
7070
OperationResponse preprocessed = this.preprocessor.preprocess(response);
7171
assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes());

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessorTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private OperationResponse createResponse(Consumer<HttpHeaders> headersCustomizer
160160
if (headersCustomizer != null) {
161161
headersCustomizer.accept(headers);
162162
}
163-
return new OperationResponseFactory().create(HttpStatus.OK.value(), headers, new byte[0]);
163+
return new OperationResponseFactory().create(HttpStatus.OK, headers, new byte[0]);
164164
}
165165

166166
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,13 @@ private OperationRequest createRequestWithPartWithContent(String content) {
340340
}
341341

342342
private OperationResponse createResponseWithContent(String content) {
343-
return this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), content.getBytes());
343+
return this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), content.getBytes());
344344
}
345345

346346
private OperationResponse createResponseWithHeader(String name, String value) {
347347
HttpHeaders headers = new HttpHeaders();
348348
headers.add(name, value);
349-
return this.responseFactory.create(HttpStatus.OK.value(), headers, new byte[0]);
349+
return this.responseFactory.create(HttpStatus.OK, headers, new byte[0]);
350350
}
351351

352352
}

spring-restdocs-core/src/testFixtures/java/org/springframework/restdocs/testfixtures/OperationBuilder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.http.HttpHeaders;
3232
import org.springframework.http.HttpMethod;
3333
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.HttpStatusCode;
3435
import org.springframework.restdocs.ManualRestDocumentation;
3536
import org.springframework.restdocs.RestDocumentationContext;
3637
import org.springframework.restdocs.mustache.Mustache;
@@ -247,7 +248,7 @@ public OperationRequestPartBuilder header(String name, String value) {
247248
*/
248249
public final class OperationResponseBuilder {
249250

250-
private int status = HttpStatus.OK.value();
251+
private HttpStatusCode status = HttpStatus.OK;
251252

252253
private HttpHeaders headers = new HttpHeaders();
253254

@@ -259,7 +260,7 @@ private OperationResponse buildResponse() {
259260
return new OperationResponseFactory().create(this.status, this.headers, this.content, this.cookies);
260261
}
261262

262-
public OperationResponseBuilder status(int status) {
263+
public OperationResponseBuilder status(HttpStatusCode status) {
263264
this.status = status;
264265
return this;
265266
}

spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import jakarta.servlet.http.Cookie;
2525

2626
import org.springframework.http.HttpHeaders;
27+
import org.springframework.http.HttpStatusCode;
2728
import org.springframework.mock.web.MockHttpServletResponse;
2829
import org.springframework.restdocs.operation.OperationResponse;
2930
import org.springframework.restdocs.operation.OperationResponseFactory;
@@ -44,7 +45,7 @@ class MockMvcResponseConverter implements ResponseConverter<MockHttpServletRespo
4445
public OperationResponse convert(MockHttpServletResponse mockResponse) {
4546
HttpHeaders headers = extractHeaders(mockResponse);
4647
Collection<ResponseCookie> cookies = extractCookies(mockResponse);
47-
return new OperationResponseFactory().create(mockResponse.getStatus(), headers,
48+
return new OperationResponseFactory().create(HttpStatusCode.valueOf(mockResponse.getStatus()), headers,
4849
mockResponse.getContentAsByteArray(), cookies);
4950
}
5051

spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverterTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.http.HttpHeaders;
2626
import org.springframework.http.HttpStatus;
27+
import org.springframework.http.HttpStatusCode;
2728
import org.springframework.mock.web.MockHttpServletResponse;
2829
import org.springframework.restdocs.operation.OperationResponse;
2930
import org.springframework.restdocs.operation.ResponseCookie;
@@ -69,8 +70,7 @@ public void responseWithCustomStatus() {
6970
MockHttpServletResponse response = new MockHttpServletResponse();
7071
response.setStatus(600);
7172
OperationResponse operationResponse = this.factory.convert(response);
72-
assertThat(operationResponse.getStatus()).isNull();
73-
assertThat(operationResponse.getStatusCode()).isEqualTo(600);
73+
assertThat(operationResponse.getStatus()).isEqualTo(HttpStatusCode.valueOf(600));
7474
}
7575

7676
}

0 commit comments

Comments
 (0)