Use Framework's HttpStatusCode to model response's status

Closes gh-848
This commit is contained in:
Andy Wilkinson
2022-10-12 11:26:01 +01:00
parent b107d0e8ed
commit 03b61ab075
20 changed files with 58 additions and 66 deletions

View File

@@ -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<String, Object> 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;
}

View File

@@ -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.

View File

@@ -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<ResponseCookie> 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());
}

View File

@@ -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<ResponseCookie> 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<ResponseCookie> cookies) {
StandardOperationResponse(HttpStatusCode status, HttpHeaders headers, byte[] content,
Collection<ResponseCookie> 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;
}

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.getStatusCode(),
return this.contentModifyingDelegate.preprocess(new OperationResponseFactory().create(response.getStatus(),
modify(response.getHeaders()), response.getContent(), response.getCookies()));
}

View File

@@ -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);
}
}

View File

@@ -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");
}

View File

@@ -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));
}

View File

@@ -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);
}

View File

@@ -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)));
}

View File

@@ -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());

View File

@@ -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]);
}
}

View File

@@ -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]);
}
}

View File

@@ -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;
}

View File

@@ -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<MockHttpServletRespo
public OperationResponse convert(MockHttpServletResponse mockResponse) {
HttpHeaders headers = extractHeaders(mockResponse);
Collection<ResponseCookie> cookies = extractCookies(mockResponse);
return new OperationResponseFactory().create(mockResponse.getStatus(), headers,
return new OperationResponseFactory().create(HttpStatusCode.valueOf(mockResponse.getStatus()), headers,
mockResponse.getContentAsByteArray(), cookies);
}

View File

@@ -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));
}
}

View File

@@ -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<Response> {
public OperationResponse convert(Response response) {
HttpHeaders headers = extractHeaders(response);
Collection<ResponseCookie> 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<ResponseCookie> extractCookies(Response response, HttpHeaders headers) {

View File

@@ -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));
}
}

View File

@@ -40,7 +40,7 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
@Override
public OperationResponse convert(ExchangeResult result) {
Collection<ResponseCookie> cookies = extractCookies(result);
return new OperationResponseFactory().create(result.getStatus().value(), extractHeaders(result),
return new OperationResponseFactory().create(result.getStatus(), extractHeaders(result),
result.getResponseBodyContent(), cookies);
}

View File

@@ -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));
}
}