Browse Source

Polishing contribution

Closes gh-28105
pull/27953/head
rstoyanchev 4 years ago
parent
commit
ee7f60000e
  1. 48
      spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java
  2. 29
      spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java
  3. 7
      spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java
  4. 6
      spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java

48
spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { 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) { public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
super(body); super(body);
Assert.notNull(statusCode, "HttpStatus is required"); 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) { public MockClientHttpResponse(byte[] body, int statusCode) {
super(body); 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) { public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
super(body); super(body);
Assert.notNull(statusCode, "HttpStatus is required"); 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) { public MockClientHttpResponse(InputStream body, int statusCode) {
super(body); super(body);
this.status = statusCode; this.statusCode = statusCode;
} }
@Override @Override
public HttpStatus getStatusCode() throws IOException { public HttpStatus getStatusCode() {
if (this.status instanceof HttpStatus) { return HttpStatus.valueOf(this.statusCode);
return (HttpStatus) this.status;
}
else {
return HttpStatus.valueOf((Integer) this.status);
}
} }
@Override @Override
public int getRawStatusCode() throws IOException { public int getRawStatusCode() {
if (this.status instanceof HttpStatus) { return this.statusCode;
return ((HttpStatus) this.status).value();
}
else {
return (Integer) this.status;
}
} }
@Override @Override
public String getStatusText() throws IOException { public String getStatusText() {
if (this.status instanceof HttpStatus) { HttpStatus status = HttpStatus.resolve(this.statusCode);
return ((HttpStatus) this.status).getReasonPhrase(); return (status != null ? status.getReasonPhrase() : "");
}
else {
return "Custom http status";
}
} }
@Override @Override

29
spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; package org.springframework.test.web.client.response;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -40,7 +39,7 @@ import org.springframework.util.Assert;
*/ */
public class DefaultResponseCreator implements ResponseCreator { public class DefaultResponseCreator implements ResponseCreator {
private final Object statusCode; private final int statusCode;
private byte[] content = new byte[0]; private byte[] content = new byte[0];
@ -56,12 +55,13 @@ public class DefaultResponseCreator implements ResponseCreator {
*/ */
protected DefaultResponseCreator(HttpStatus statusCode) { protected DefaultResponseCreator(HttpStatus statusCode) {
Assert.notNull(statusCode, "HttpStatus must not be null"); Assert.notNull(statusCode, "HttpStatus must not be null");
this.statusCode = statusCode; this.statusCode = statusCode.value();
} }
/** /**
* Protected constructor. * Protected constructor.
* Use static factory methods in {@link MockRestResponseCreators}. * Use static factory methods in {@link MockRestResponseCreators}.
* @since 5.3.17
*/ */
protected DefaultResponseCreator(int statusCode) { protected DefaultResponseCreator(int statusCode) {
this.statusCode = statusCode; this.statusCode = statusCode;
@ -119,24 +119,9 @@ public class DefaultResponseCreator implements ResponseCreator {
@Override @Override
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException { public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
MockClientHttpResponse response; MockClientHttpResponse response = (this.contentResource != null ?
if (this.contentResource != null) { new MockClientHttpResponse(this.contentResource.getInputStream(), this.statusCode) :
InputStream stream = this.contentResource.getInputStream(); new MockClientHttpResponse(this.content, this.statusCode));
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);
}
}
response.getHeaders().putAll(this.headers); response.getHeaders().putAll(this.headers);
return response; return response;
} }

7
spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @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); return new DefaultResponseCreator(status);
} }

6
spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -130,11 +130,11 @@ class ResponseCreatorsTests {
@Test @Test
void withCustomStatus() throws Exception { void withCustomStatus() throws Exception {
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454); DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
assertThat(response.getRawStatusCode()).isEqualTo(454); assertThat(response.getRawStatusCode()).isEqualTo(454);
assertThat(response.getStatusText()).isEqualTo("Custom http status"); assertThat(response.getStatusText()).isEmpty();
} }
@Test @Test

Loading…
Cancel
Save