parent
f1fdc11fab
commit
ee7f60000e
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
|
|
@ -32,7 +32,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
|
||||
|
||||
private final Object status;
|
||||
private final int statusCode;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -41,15 +41,17 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
|
|||
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
|
||||
super(body);
|
||||
Assert.notNull(statusCode, "HttpStatus is required");
|
||||
this.status = statusCode;
|
||||
this.statusCode = statusCode.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with response body as a byte array.
|
||||
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
|
||||
* custom HTTP status code.
|
||||
* @since 5.3.17
|
||||
*/
|
||||
public MockClientHttpResponse(byte[] body, int statusCode) {
|
||||
super(body);
|
||||
this.status = statusCode;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -58,46 +60,34 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
|
|||
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
|
||||
super(body);
|
||||
Assert.notNull(statusCode, "HttpStatus is required");
|
||||
this.status = statusCode;
|
||||
this.statusCode = statusCode.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with response body as InputStream.
|
||||
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
|
||||
* custom HTTP status code.
|
||||
* @since 5.3.17
|
||||
*/
|
||||
public MockClientHttpResponse(InputStream body, int statusCode) {
|
||||
super(body);
|
||||
this.status = statusCode;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return (HttpStatus) this.status;
|
||||
}
|
||||
else {
|
||||
return HttpStatus.valueOf((Integer) this.status);
|
||||
}
|
||||
public HttpStatus getStatusCode() {
|
||||
return HttpStatus.valueOf(this.statusCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return ((HttpStatus) this.status).value();
|
||||
}
|
||||
else {
|
||||
return (Integer) this.status;
|
||||
}
|
||||
public int getRawStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return ((HttpStatus) this.status).getReasonPhrase();
|
||||
}
|
||||
else {
|
||||
return "Custom http status";
|
||||
}
|
||||
public String getStatusText() {
|
||||
HttpStatus status = HttpStatus.resolve(this.statusCode);
|
||||
return (status != null ? status.getReasonPhrase() : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
|
|
@ -17,7 +17,6 @@
|
|||
package org.springframework.test.web.client.response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
|
@ -40,7 +39,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class DefaultResponseCreator implements ResponseCreator {
|
||||
|
||||
private final Object statusCode;
|
||||
private final int statusCode;
|
||||
|
||||
private byte[] content = new byte[0];
|
||||
|
||||
|
|
@ -56,12 +55,13 @@ public class DefaultResponseCreator implements ResponseCreator {
|
|||
*/
|
||||
protected DefaultResponseCreator(HttpStatus statusCode) {
|
||||
Assert.notNull(statusCode, "HttpStatus must not be null");
|
||||
this.statusCode = statusCode;
|
||||
this.statusCode = statusCode.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected constructor.
|
||||
* Use static factory methods in {@link MockRestResponseCreators}.
|
||||
* @since 5.3.17
|
||||
*/
|
||||
protected DefaultResponseCreator(int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
|
|
@ -119,24 +119,9 @@ public class DefaultResponseCreator implements ResponseCreator {
|
|||
|
||||
@Override
|
||||
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
|
||||
MockClientHttpResponse response;
|
||||
if (this.contentResource != null) {
|
||||
InputStream stream = this.contentResource.getInputStream();
|
||||
if (this.statusCode instanceof HttpStatus) {
|
||||
response = new MockClientHttpResponse(stream, (HttpStatus) this.statusCode);
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(stream, (Integer) this.statusCode);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this.statusCode instanceof HttpStatus) {
|
||||
response = new MockClientHttpResponse(this.content, (HttpStatus) this.statusCode);
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(this.content, (Integer) this.statusCode);
|
||||
}
|
||||
}
|
||||
MockClientHttpResponse response = (this.contentResource != null ?
|
||||
new MockClientHttpResponse(this.contentResource.getInputStream(), this.statusCode) :
|
||||
new MockClientHttpResponse(this.content, this.statusCode));
|
||||
response.getHeaders().putAll(this.headers);
|
||||
return response;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
|
|
@ -118,10 +118,11 @@ public abstract class MockRestResponseCreators {
|
|||
}
|
||||
|
||||
/**
|
||||
* {@code ResponseCreator} with a specific HTTP status.
|
||||
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
|
||||
* @param status the response status
|
||||
* @since 5.3.17
|
||||
*/
|
||||
public static DefaultResponseCreator withStatus(int status) {
|
||||
public static DefaultResponseCreator withRawStatus(int status) {
|
||||
return new DefaultResponseCreator(status);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
|
|
@ -130,11 +130,11 @@ class ResponseCreatorsTests {
|
|||
|
||||
@Test
|
||||
void withCustomStatus() throws Exception {
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454);
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertThat(response.getRawStatusCode()).isEqualTo(454);
|
||||
assertThat(response.getStatusText()).isEqualTo("Custom http status");
|
||||
assertThat(response.getStatusText()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue