From 03b61ab075bd205fd0b7eb097104478860ddd640 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 12 Oct 2022 11:26:01 +0100 Subject: [PATCH] Use Framework's HttpStatusCode to model response's status Closes gh-848 --- .../restdocs/http/HttpResponseSnippet.java | 15 +++++---------- .../restdocs/operation/OperationResponse.java | 14 +++----------- .../operation/OperationResponseFactory.java | 14 ++++++++------ .../operation/StandardOperationResponse.java | 14 +++++--------- .../UriModifyingOperationPreprocessor.java | 2 +- .../restdocs/RestDocumentationGeneratorTests.java | 8 +++++--- .../config/RestDocumentationConfigurerTests.java | 2 +- .../restdocs/http/HttpResponseSnippetTests.java | 9 +++++---- .../hypermedia/ContentTypeLinkExtractorTests.java | 8 ++++---- .../hypermedia/LinkExtractorsPayloadTests.java | 4 ++-- ...ontentModifyingOperationPreprocessorTests.java | 2 +- ...eadersModifyingOperationPreprocessorTests.java | 2 +- .../UriModifyingOperationPreprocessorTests.java | 4 ++-- .../restdocs/testfixtures/OperationBuilder.java | 5 +++-- .../mockmvc/MockMvcResponseConverter.java | 3 ++- .../mockmvc/MockMvcResponseConverterTests.java | 4 ++-- .../restassured/RestAssuredResponseConverter.java | 5 +++-- .../RestAssuredResponseConverterTests.java | 4 ++-- .../WebTestClientResponseConverter.java | 2 +- .../WebTestClientResponseConverterTests.java | 3 ++- 20 files changed, 58 insertions(+), 66 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java index 7e4a9c11..ec56d397 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpResponseSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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. @@ -23,6 +23,7 @@ import java.util.Map; import java.util.Map.Entry; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.snippet.Snippet; @@ -59,15 +60,9 @@ public class HttpResponseSnippet extends TemplatedSnippet { Map model = new HashMap<>(); model.put("responseBody", responseBody(response)); model.put("headers", headers(response)); - HttpStatus status = response.getStatus(); - if (status != null) { - model.put("statusCode", status.value()); - model.put("statusReason", status.getReasonPhrase()); - } - else { - model.put("statusCode", response.getStatusCode()); - model.put("statusReason", ""); - } + HttpStatusCode status = response.getStatus(); + model.put("statusCode", status.value()); + model.put("statusReason", (status instanceof HttpStatus) ? ((HttpStatus) status).getReasonPhrase() : ""); return model; } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java index 8490ade2..344b4e9a 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponse.java @@ -19,7 +19,7 @@ package org.springframework.restdocs.operation; import java.util.Collection; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; /** * The response that was received as part of performing an operation on a RESTful service. @@ -33,17 +33,9 @@ public interface OperationResponse { /** * Returns the status of the response. - * @return the status or {@code null} if the status is unknown to {@link HttpStatus} + * @return the status, never {@code null} */ - HttpStatus getStatus(); - - /** - * Returns the status code of the response. - * @return the status code - */ - default int getStatusCode() { - throw new UnsupportedOperationException(); - } + HttpStatusCode getStatus(); /** * Returns the headers in the response. diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponseFactory.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponseFactory.java index 125f744b..b9c749ab 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponseFactory.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/OperationResponseFactory.java @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.Collections; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatusCode; /** * A factory for creating {@link OperationResponse OperationResponses}. @@ -37,8 +38,9 @@ public class OperationResponseFactory { * @param headers the request's headers * @param content the content of the request * @return the {@code OperationResponse} + * @since 3.0.0 */ - public OperationResponse create(int status, HttpHeaders headers, byte[] content) { + public OperationResponse create(HttpStatusCode status, HttpHeaders headers, byte[] content) { return new StandardOperationResponse(status, augmentHeaders(headers, content), content, Collections.emptyList()); } @@ -52,9 +54,9 @@ public class OperationResponseFactory { * @param content the content of the request * @param cookies the cookies * @return the {@code OperationResponse} - * @since 3.0 + * @since 3.0.0 */ - public OperationResponse create(int status, HttpHeaders headers, byte[] content, + public OperationResponse create(HttpStatusCode status, HttpHeaders headers, byte[] content, Collection cookies) { return new StandardOperationResponse(status, augmentHeaders(headers, content), content, cookies); } @@ -69,8 +71,8 @@ public class OperationResponseFactory { * @return the new response with the new content */ public OperationResponse createFrom(OperationResponse original, byte[] newContent) { - return new StandardOperationResponse(original.getStatusCode(), - getUpdatedHeaders(original.getHeaders(), newContent), newContent, original.getCookies()); + return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent), + newContent, original.getCookies()); } /** @@ -81,7 +83,7 @@ public class OperationResponseFactory { * @return the new response with the new headers */ public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) { - return new StandardOperationResponse(original.getStatusCode(), newHeaders, original.getContent(), + return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent(), original.getCookies()); } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java index 5d9045af..667d03ca 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/StandardOperationResponse.java @@ -19,7 +19,7 @@ package org.springframework.restdocs.operation; import java.util.Collection; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; /** * Standard implementation of {@link OperationResponse}. @@ -29,7 +29,7 @@ import org.springframework.http.HttpStatus; */ class StandardOperationResponse extends AbstractOperationMessage implements OperationResponse { - private final int status; + private final HttpStatusCode status; private Collection cookies; @@ -41,19 +41,15 @@ class StandardOperationResponse extends AbstractOperationMessage implements Oper * @param content the content of the response * @param cookies any cookies included in the response */ - StandardOperationResponse(int status, HttpHeaders headers, byte[] content, Collection cookies) { + StandardOperationResponse(HttpStatusCode status, HttpHeaders headers, byte[] content, + Collection cookies) { super(content, headers); this.status = status; this.cookies = cookies; } @Override - public HttpStatus getStatus() { - return HttpStatus.resolve(this.status); - } - - @Override - public int getStatusCode() { + public HttpStatusCode getStatus() { return this.status; } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java index ef2f2223..ab293f32 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java @@ -141,7 +141,7 @@ public class UriModifyingOperationPreprocessor implements OperationPreprocessor @Override public OperationResponse preprocess(OperationResponse response) { - return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatusCode(), + return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatus(), modify(response.getHeaders()), response.getContent(), response.getCookies())); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java index c84ec96e..8c3df612 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/RestDocumentationGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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,6 +28,7 @@ import org.mockito.InOrder; import org.mockito.Mockito; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.restdocs.generate.RestDocumentationGenerator; import org.springframework.restdocs.operation.Operation; import org.springframework.restdocs.operation.OperationRequest; @@ -69,7 +70,8 @@ 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(0, null, null); + private final OperationResponse operationResponse = new OperationResponseFactory().create(HttpStatus.OK, null, + null); private final Snippet snippet = mock(Snippet.class); @@ -197,7 +199,7 @@ public class RestDocumentationGeneratorTests { } private static OperationResponse createResponse() { - return new OperationResponseFactory().create(0, null, null); + return new OperationResponseFactory().create(HttpStatus.OK, null, null); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java index 2141dee8..e9545b75 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/config/RestDocumentationConfigurerTests.java @@ -223,7 +223,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.value(), headers, null); + OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK, headers, null); assertThat(preprocessor.preprocess(response).getHeaders()).doesNotContainKey("Foo"); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java index 30287183..0b255af8 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpResponseSnippetTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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. @@ -22,6 +22,7 @@ import org.junit.Test; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.restdocs.AbstractSnippetTests; import org.springframework.restdocs.templates.TemplateEngine; @@ -55,8 +56,7 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests { @Test public void nonOkResponse() throws IOException { - new HttpResponseSnippet() - .document(this.operationBuilder.response().status(HttpStatus.BAD_REQUEST.value()).build()); + new HttpResponseSnippet().document(this.operationBuilder.response().status(HttpStatus.BAD_REQUEST).build()); assertThat(this.generatedSnippets.httpResponse()).is(httpResponse(HttpStatus.BAD_REQUEST)); } @@ -99,7 +99,8 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests { @Test public void responseWithCustomStatus() throws IOException { - new HttpResponseSnippet().document(this.operationBuilder.response().status(215).build()); + new HttpResponseSnippet() + .document(this.operationBuilder.response().status(HttpStatusCode.valueOf(215)).build()); assertThat(this.generatedSnippets.httpResponse()).is(httpResponse(215)); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java index e987a5af..7544b315 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/ContentTypeLinkExtractorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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. @@ -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.value(), new HttpHeaders(), null)); + .extractLinks(this.responseFactory.create(HttpStatus.OK, 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.value(), httpHeaders, null); + OperationResponse response = this.responseFactory.create(HttpStatus.OK, 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.value(), httpHeaders, null); + OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null); new ContentTypeLinkExtractor(extractors).extractLinks(response); verify(extractor).extractLinks(response); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java index 1eb99f19..62238011 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 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. @@ -106,7 +106,7 @@ public class LinkExtractorsPayloadTests { } private OperationResponse createResponse(String contentName) throws IOException { - return this.responseFactory.create(HttpStatus.OK.value(), null, + return this.responseFactory.create(HttpStatus.OK, null, FileCopyUtils.copyToByteArray(getPayloadFile(contentName))); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java index 7e26b6da..dbbee922 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ContentModifyingOperationPreprocessorTests.java @@ -65,7 +65,7 @@ public class ContentModifyingOperationPreprocessorTests { @Test public void modifyResponseContent() { - OperationResponse response = this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), + OperationResponse response = this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), "content".getBytes()); OperationResponse preprocessed = this.preprocessor.preprocess(response); assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes()); diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessorTests.java index c33153f4..c6ce70f0 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessorTests.java @@ -160,7 +160,7 @@ public class HeadersModifyingOperationPreprocessorTests { if (headersCustomizer != null) { headersCustomizer.accept(headers); } - return new OperationResponseFactory().create(HttpStatus.OK.value(), headers, new byte[0]); + return new OperationResponseFactory().create(HttpStatus.OK, headers, new byte[0]); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java index d0353540..4ac459c2 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java @@ -340,13 +340,13 @@ public class UriModifyingOperationPreprocessorTests { } private OperationResponse createResponseWithContent(String content) { - return this.responseFactory.create(HttpStatus.OK.value(), new HttpHeaders(), content.getBytes()); + return this.responseFactory.create(HttpStatus.OK, 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.value(), headers, new byte[0]); + return this.responseFactory.create(HttpStatus.OK, headers, new byte[0]); } } diff --git a/spring-restdocs-core/src/testFixtures/java/org/springframework/restdocs/testfixtures/OperationBuilder.java b/spring-restdocs-core/src/testFixtures/java/org/springframework/restdocs/testfixtures/OperationBuilder.java index a546cbd7..e3d3d40c 100644 --- a/spring-restdocs-core/src/testFixtures/java/org/springframework/restdocs/testfixtures/OperationBuilder.java +++ b/spring-restdocs-core/src/testFixtures/java/org/springframework/restdocs/testfixtures/OperationBuilder.java @@ -31,6 +31,7 @@ import org.junit.runners.model.Statement; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.restdocs.ManualRestDocumentation; import org.springframework.restdocs.RestDocumentationContext; import org.springframework.restdocs.mustache.Mustache; @@ -247,7 +248,7 @@ public class OperationBuilder extends OperationTestRule { */ public final class OperationResponseBuilder { - private int status = HttpStatus.OK.value(); + private HttpStatusCode status = HttpStatus.OK; private HttpHeaders headers = new HttpHeaders(); @@ -259,7 +260,7 @@ public class OperationBuilder extends OperationTestRule { return new OperationResponseFactory().create(this.status, this.headers, this.content, this.cookies); } - public OperationResponseBuilder status(int status) { + public OperationResponseBuilder status(HttpStatusCode status) { this.status = status; return this; } diff --git a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverter.java b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverter.java index 56d44f7c..e3caca60 100644 --- a/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverter.java +++ b/spring-restdocs-mockmvc/src/main/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverter.java @@ -24,6 +24,7 @@ import java.util.List; import jakarta.servlet.http.Cookie; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatusCode; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.OperationResponseFactory; @@ -44,7 +45,7 @@ class MockMvcResponseConverter implements ResponseConverter cookies = extractCookies(mockResponse); - return new OperationResponseFactory().create(mockResponse.getStatus(), headers, + return new OperationResponseFactory().create(HttpStatusCode.valueOf(mockResponse.getStatus()), headers, mockResponse.getContentAsByteArray(), cookies); } diff --git a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverterTests.java b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverterTests.java index 40338091..1d208b43 100644 --- a/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverterTests.java +++ b/spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcResponseConverterTests.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.ResponseCookie; @@ -69,8 +70,7 @@ public class MockMvcResponseConverterTests { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(600); OperationResponse operationResponse = this.factory.convert(response); - assertThat(operationResponse.getStatus()).isNull(); - assertThat(operationResponse.getStatusCode()).isEqualTo(600); + assertThat(operationResponse.getStatus()).isEqualTo(HttpStatusCode.valueOf(600)); } } diff --git a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredResponseConverter.java b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredResponseConverter.java index 09b95978..a049ea1a 100644 --- a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredResponseConverter.java +++ b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/RestAssuredResponseConverter.java @@ -26,6 +26,7 @@ import io.restassured.http.Header; import io.restassured.response.Response; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatusCode; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.OperationResponseFactory; import org.springframework.restdocs.operation.ResponseConverter; @@ -44,8 +45,8 @@ class RestAssuredResponseConverter implements ResponseConverter { public OperationResponse convert(Response response) { HttpHeaders headers = extractHeaders(response); Collection cookies = extractCookies(response, headers); - return new OperationResponseFactory().create(response.getStatusCode(), extractHeaders(response), - extractContent(response), cookies); + return new OperationResponseFactory().create(HttpStatusCode.valueOf(response.getStatusCode()), + extractHeaders(response), extractContent(response), cookies); } private Collection extractCookies(Response response, HttpHeaders headers) { diff --git a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredResponseConverterTests.java b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredResponseConverterTests.java index c3f79015..3760c656 100644 --- a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredResponseConverterTests.java +++ b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredResponseConverterTests.java @@ -21,6 +21,7 @@ import io.restassured.response.Response; import io.restassured.response.ResponseBody; import org.junit.Test; +import org.springframework.http.HttpStatusCode; import org.springframework.restdocs.operation.OperationResponse; import static org.assertj.core.api.Assertions.assertThat; @@ -45,8 +46,7 @@ public class RestAssuredResponseConverterTests { given(response.getBody()).willReturn(body); given(body.asByteArray()).willReturn(new byte[0]); OperationResponse operationResponse = this.converter.convert(response); - assertThat(operationResponse.getStatus()).isNull(); - assertThat(operationResponse.getStatusCode()).isEqualTo(600); + assertThat(operationResponse.getStatus()).isEqualTo(HttpStatusCode.valueOf(600)); } } diff --git a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java index fc7d4147..f74c8c12 100644 --- a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java +++ b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java @@ -40,7 +40,7 @@ class WebTestClientResponseConverter implements ResponseConverter cookies = extractCookies(result); - return new OperationResponseFactory().create(result.getStatus().value(), extractHeaders(result), + return new OperationResponseFactory().create(result.getStatus(), extractHeaders(result), result.getResponseBodyContent(), cookies); } diff --git a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverterTests.java b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverterTests.java index 3c7ad67a..622e0708 100644 --- a/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverterTests.java +++ b/spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverterTests.java @@ -22,6 +22,7 @@ import org.junit.Test; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.ResponseCookie; @@ -83,7 +84,7 @@ public class WebTestClientResponseConverterTests { .configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody() .returnResult(); OperationResponse response = this.converter.convert(result); - assertThat(response.getStatusCode()).isEqualTo(210); + assertThat(response.getStatus()).isEqualTo(HttpStatusCode.valueOf(210)); } }