Skip to content

Commit e7b97f5

Browse files
committed
Merge branch '5.3.x' into main
2 parents 03179aa + ee7f600 commit e7b97f5

File tree

10 files changed

+163
-101
lines changed

10 files changed

+163
-101
lines changed

spring-core/src/testFixtures/java/org/springframework/core/testfixture/codec/AbstractEncoderTests.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ protected AbstractEncoderTests(E encoder) {
8181
* Helper methods that tests for a variety of encoding scenarios. This methods
8282
* invokes:
8383
* <ul>
84-
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
84+
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
8585
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
8686
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
8787
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
@@ -94,30 +94,32 @@ protected AbstractEncoderTests(E encoder) {
9494
*/
9595
protected <T> void testEncodeAll(Publisher<? extends T> input, Class<? extends T> inputClass,
9696
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
97-
testEncodeAll(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);
97+
98+
testEncodeAll(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
9899
}
99100

100101
/**
101102
* Helper methods that tests for a variety of decoding scenarios. This methods
102103
* invokes:
103104
* <ul>
104-
* <li>{@link #testEncode(Publisher, ResolvableType, Consumer, MimeType, Map)}</li>
105+
* <li>{@link #testEncode(Publisher, ResolvableType, MimeType, Map, Consumer)}</li>
105106
* <li>{@link #testEncodeError(Publisher, ResolvableType, MimeType, Map)}</li>
106107
* <li>{@link #testEncodeCancel(Publisher, ResolvableType, MimeType, Map)}</li>
107108
* <li>{@link #testEncodeEmpty(ResolvableType, MimeType, Map)}</li>
108109
* </ul>
109110
*
111+
* @param <T> the output type
110112
* @param input the input to be provided to the encoder
111113
* @param inputType the input type
112-
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
113114
* @param mimeType the mime type to use for decoding. May be {@code null}.
114115
* @param hints the hints used for decoding. May be {@code null}.
115-
* @param <T> the output type
116+
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
116117
*/
117118
protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType inputType,
118-
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
119-
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
120-
testEncode(input, inputType, stepConsumer, mimeType, hints);
119+
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
120+
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
121+
122+
testEncode(input, inputType, mimeType, hints, stepConsumer);
121123
testEncodeError(input, inputType, mimeType, hints);
122124
testEncodeCancel(input, inputType, mimeType, hints);
123125
testEncodeEmpty(inputType, mimeType, hints);
@@ -133,25 +135,25 @@ protected <T> void testEncodeAll(Publisher<? extends T> input, ResolvableType in
133135
*/
134136
protected <T> void testEncode(Publisher<? extends T> input, Class<? extends T> inputClass,
135137
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
136-
testEncode(input, ResolvableType.forClass(inputClass), stepConsumer, null, null);
138+
139+
testEncode(input, ResolvableType.forClass(inputClass), null, null, stepConsumer);
137140
}
138141

139142
/**
140143
* Test a standard {@link Encoder#encode encode} scenario.
141144
*
145+
* @param <T> the output type
142146
* @param input the input to be provided to the encoder
143147
* @param inputType the input type
144-
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
145148
* @param mimeType the mime type to use for decoding. May be {@code null}.
146149
* @param hints the hints used for decoding. May be {@code null}.
147-
* @param <T> the output type
150+
* @param stepConsumer a consumer to {@linkplain StepVerifier verify} the output
148151
*/
149152
protected <T> void testEncode(Publisher<? extends T> input, ResolvableType inputType,
150-
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer,
151-
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
153+
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints,
154+
Consumer<StepVerifier.FirstStep<DataBuffer>> stepConsumer) {
152155

153-
Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType,
154-
mimeType, hints);
156+
Flux<DataBuffer> result = encoder().encode(input, this.bufferFactory, inputType, mimeType, hints);
155157
StepVerifier.FirstStep<DataBuffer> step = StepVerifier.create(result);
156158
stepConsumer.accept(step);
157159
}

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232
*/
3333
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
3434

35-
private final HttpStatus status;
35+
private final int statusCode;
3636

3737

3838
/**
@@ -41,7 +41,17 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
4141
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
4242
super(body);
4343
Assert.notNull(statusCode, "HttpStatus is required");
44-
this.status = statusCode;
44+
this.statusCode = statusCode.value();
45+
}
46+
47+
/**
48+
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
49+
* custom HTTP status code.
50+
* @since 5.3.17
51+
*/
52+
public MockClientHttpResponse(byte[] body, int statusCode) {
53+
super(body);
54+
this.statusCode = statusCode;
4555
}
4656

4757
/**
@@ -50,23 +60,34 @@ public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
5060
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
5161
super(body);
5262
Assert.notNull(statusCode, "HttpStatus is required");
53-
this.status = statusCode;
63+
this.statusCode = statusCode.value();
64+
}
65+
66+
/**
67+
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
68+
* custom HTTP status code.
69+
* @since 5.3.17
70+
*/
71+
public MockClientHttpResponse(InputStream body, int statusCode) {
72+
super(body);
73+
this.statusCode = statusCode;
5474
}
5575

5676

5777
@Override
58-
public HttpStatus getStatusCode() throws IOException {
59-
return this.status;
78+
public HttpStatus getStatusCode() {
79+
return HttpStatus.valueOf(this.statusCode);
6080
}
6181

6282
@Override
63-
public int getRawStatusCode() throws IOException {
64-
return this.status.value();
83+
public int getRawStatusCode() {
84+
return this.statusCode;
6585
}
6686

6787
@Override
68-
public String getStatusText() throws IOException {
69-
return this.status.getReasonPhrase();
88+
public String getStatusText() {
89+
HttpStatus status = HttpStatus.resolve(this.statusCode);
90+
return (status != null ? status.getReasonPhrase() : "");
7091
}
7192

7293
@Override

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.test.web.client.response;
1818

1919
import java.io.IOException;
20-
import java.io.InputStream;
2120
import java.net.URI;
2221
import java.nio.charset.StandardCharsets;
2322

@@ -40,7 +39,7 @@
4039
*/
4140
public class DefaultResponseCreator implements ResponseCreator {
4241

43-
private final HttpStatus statusCode;
42+
private final int statusCode;
4443

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

@@ -56,6 +55,15 @@ public class DefaultResponseCreator implements ResponseCreator {
5655
*/
5756
protected DefaultResponseCreator(HttpStatus statusCode) {
5857
Assert.notNull(statusCode, "HttpStatus must not be null");
58+
this.statusCode = statusCode.value();
59+
}
60+
61+
/**
62+
* Protected constructor.
63+
* Use static factory methods in {@link MockRestResponseCreators}.
64+
* @since 5.3.17
65+
*/
66+
protected DefaultResponseCreator(int statusCode) {
5967
this.statusCode = statusCode;
6068
}
6169

@@ -111,14 +119,9 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
111119

112120
@Override
113121
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
114-
MockClientHttpResponse response;
115-
if (this.contentResource != null) {
116-
InputStream stream = this.contentResource.getInputStream();
117-
response = new MockClientHttpResponse(stream, this.statusCode);
118-
}
119-
else {
120-
response = new MockClientHttpResponse(this.content, this.statusCode);
121-
}
122+
MockClientHttpResponse response = (this.contentResource != null ?
123+
new MockClientHttpResponse(this.contentResource.getInputStream(), this.statusCode) :
124+
new MockClientHttpResponse(this.content, this.statusCode));
122125
response.getHeaders().putAll(this.headers);
123126
return response;
124127
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -117,6 +117,15 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
117117
return new DefaultResponseCreator(status);
118118
}
119119

120+
/**
121+
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
122+
* @param status the response status
123+
* @since 5.3.17
124+
*/
125+
public static DefaultResponseCreator withRawStatus(int status) {
126+
return new DefaultResponseCreator(status);
127+
}
128+
120129
/**
121130
* {@code ResponseCreator} with an internal application {@code IOException}.
122131
* <p>For example, one could use this to simulate a {@code SocketTimeoutException}.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,6 +128,15 @@ void withStatus() throws Exception {
128128
assertThat(StreamUtils.copyToByteArray(response.getBody()).length).isEqualTo(0);
129129
}
130130

131+
@Test
132+
void withCustomStatus() throws Exception {
133+
DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
134+
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
135+
136+
assertThat(response.getRawStatusCode()).isEqualTo(454);
137+
assertThat(response.getStatusText()).isEmpty();
138+
}
139+
131140
@Test
132141
void withException() {
133142
ResponseCreator responseCreator = MockRestResponseCreators.withException(new SocketTimeoutException());

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Encoder.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -196,21 +196,25 @@ public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory buffe
196196
public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
197197
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
198198

199-
ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
200-
if (mapper == null) {
201-
throw new IllegalStateException("No ObjectMapper for " + valueType);
202-
}
203199
Class<?> jsonView = null;
204200
FilterProvider filters = null;
205201
if (value instanceof MappingJacksonValue mappingJacksonValue) {
206202
value = mappingJacksonValue.getValue();
203+
valueType = ResolvableType.forInstance(value);
207204
jsonView = mappingJacksonValue.getSerializationView();
208205
filters = mappingJacksonValue.getFilters();
209206
}
207+
208+
ObjectMapper mapper = selectObjectMapper(valueType, mimeType);
209+
if (mapper == null) {
210+
throw new IllegalStateException("No ObjectMapper for " + valueType);
211+
}
212+
210213
ObjectWriter writer = createObjectWriter(mapper, valueType, mimeType, jsonView, hints);
211214
if (filters != null) {
212215
writer = writer.with(filters);
213216
}
217+
214218
ByteArrayBuilder byteBuilder = new ByteArrayBuilder(writer.getFactory()._getBufferRecycler());
215219
try {
216220
JsonEncoding encoding = getJsonEncoding(mimeType);

0 commit comments

Comments
 (0)