Merge branch '5.3.x' into main
This commit is contained in:
@@ -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 HttpStatus status;
|
||||
private final int statusCode;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,7 +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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.statusCode = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,23 +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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return this.status;
|
||||
public HttpStatus getStatusCode() {
|
||||
return HttpStatus.valueOf(this.statusCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return this.status.value();
|
||||
public int getRawStatusCode() {
|
||||
return this.statusCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return this.status.getReasonPhrase();
|
||||
public String getStatusText() {
|
||||
HttpStatus status = HttpStatus.resolve(this.statusCode);
|
||||
return (status != null ? status.getReasonPhrase() : "");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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 HttpStatus statusCode;
|
||||
private final int statusCode;
|
||||
|
||||
private byte[] content = new byte[0];
|
||||
|
||||
@@ -56,6 +55,15 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
*/
|
||||
protected DefaultResponseCreator(HttpStatus statusCode) {
|
||||
Assert.notNull(statusCode, "HttpStatus must not be null");
|
||||
this.statusCode = statusCode.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected constructor.
|
||||
* Use static factory methods in {@link MockRestResponseCreators}.
|
||||
* @since 5.3.17
|
||||
*/
|
||||
protected DefaultResponseCreator(int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
@@ -111,14 +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();
|
||||
response = new MockClientHttpResponse(stream, this.statusCode);
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(this.content, 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.
|
||||
@@ -117,6 +117,15 @@ public abstract class MockRestResponseCreators {
|
||||
return new DefaultResponseCreator(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
|
||||
* @param status the response status
|
||||
* @since 5.3.17
|
||||
*/
|
||||
public static DefaultResponseCreator withRawStatus(int status) {
|
||||
return new DefaultResponseCreator(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ResponseCreator} with an internal application {@code IOException}.
|
||||
* <p>For example, one could use this to simulate a {@code SocketTimeoutException}.
|
||||
|
||||
Reference in New Issue
Block a user