Use Framework's HttpStatusCode to model response's status
Closes gh-848
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user