Skip to content

Commit

Permalink
Merge branch '5.3.x' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 16, 2022
2 parents 03179aa + ee7f600 commit e7b97f5
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -81,7 +81,7 @@ protected AbstractEncoderTests(E encoder) {
* Helper methods that tests for a variety of encoding scenarios. This methods
* invokes:
* <ul>
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
Expand All @@ -94,30 +94,32 @@ protected AbstractEncoderTests(E encoder) {
*/
protected <T> void testEncodeAll(Publisher<? extends T> input, Class<? extends T> inputClass,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
testEncodeAll(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);

testEncodeAll(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
}

/**
* Helper methods that tests for a variety of decoding scenarios. This methods
* invokes:
* <ul>
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
* </ul>
*
* @param <T> the output type
* @param input the input to be provided to the encoder
* @param inputType the input type
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
* @param mimeType the mime type to use for decoding. May be {@code null}.
* @param hints the hints used for decoding. May be {@code null}.
* @param <T> the output type
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
*/
protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType inputType,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
testEncode(input, inputType, stepConsumer, mimeType, hints);
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {

testEncode(input, inputType, mimeType, hints, stepConsumer);
testEncodeError(input, inputType, mimeType, hints);
testEncodeCancel(input, inputType, mimeType, hints);
testEncodeEmpty(inputType, mimeType, hints);
Expand All @@ -133,25 +135,25 @@ protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType in
*/
protected <T> void testEncode(Publisher<? extends T> input, Class<? extends T> inputClass,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
testEncode(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);

testEncode(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
}

/**
* Test a standard {@link Encoder#encode encode} scenario.
*
* @param <T> the output type
* @param input the input to be provided to the encoder
* @param inputType the input type
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
* @param mimeType the mime type to use for decoding. May be {@code null}.
* @param hints the hints used for decoding. May be {@code null}.
* @param <T> the output type
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
*/
protected <T> void testEncode(Publisher<? extends T> input, ResolvableType inputType,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {

Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType,
mimeType, hints);
Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType, mimeType, hints);
StepVerifier.FirstStep<DataBuffer> step = StepVerifier.create(result);
stepConsumer.accept(step);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,7 +32,7 @@
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {

private final HttpStatus status;
private final int statusCode;


/**
Expand All @@ -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;
}

/**
Expand All @@ -50,23 +60,34 @@ public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -40,7 +39,7 @@
*/
public class DefaultResponseCreator implements ResponseCreator {

private final HttpStatus statusCode;
private final int statusCode;

private byte[] content = new byte[0];

Expand All @@ -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;
}

Expand Down Expand Up @@ -111,14 +119,9 @@ public DefaultResponseCreator headers(HttpHeaders headers) {

@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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -117,6 +117,15 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
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}.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -128,6 +128,15 @@ void withStatus() throws Exception {
assertThat(StreamUtils.copyToByteArray(response.getBody()).length).isEqualTo(0);
}

@Test
void withCustomStatus() throws Exception {
DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

assertThat(response.getRawStatusCode()).isEqualTo(454);
assertThat(response.getStatusText()).isEmpty();
}

@Test
void withException() {
ResponseCreator responseCreator = MockRestResponseCreators.withException(new SocketTimeoutException());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -196,21 +196,25 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
if (mapper == null) {
throw new IllegalStateException("No ObjectMapper for " + valueType);
}
Class<?> jsonView = null;
FilterProvider filters = null;
if (value instanceof MappingJacksonValue mappingJacksonValue) {
value = mappingJacksonValue.getValue();
valueType = ResolvableType.forInstance(value);
jsonView = mappingJacksonValue.getSerializationView();
filters = mappingJacksonValue.getFilters();
}

ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
if (mapper == null) {
throw new IllegalStateException("No ObjectMapper for " + valueType);
}

ObjectWriter writer = createObjectWriter(mapper, valueType, mimeType, jsonView, hints);
if (filters != null) {
writer = writer.with(filters);
}

ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
try {
JsonEncoding encoding = getJsonEncoding(mimeType);
Expand Down
Loading

0 comments on commit e7b97f5

Please sign in to comment.