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