Skip to content

Commit b9efa91

Browse files
committed
Polishing in client tests
1 parent 3f7d3cb commit b9efa91

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

spring-web/src/test/java/org/springframework/http/client/AbstractMockWebServerTests.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -16,16 +16,13 @@
1616

1717
package org.springframework.http.client;
1818

19-
import java.util.Collections;
20-
2119
import okhttp3.mockwebserver.Dispatcher;
2220
import okhttp3.mockwebserver.MockResponse;
2321
import okhttp3.mockwebserver.MockWebServer;
2422
import okhttp3.mockwebserver.RecordedRequest;
2523
import org.junit.jupiter.api.AfterEach;
2624
import org.junit.jupiter.api.BeforeEach;
2725

28-
import org.springframework.http.MediaType;
2926
import org.springframework.util.StringUtils;
3027

3128
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,8 +38,6 @@ public abstract class AbstractMockWebServerTests {
4138

4239
protected String baseUrl;
4340

44-
protected static final MediaType textContentType =
45-
new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8"));
4641

4742
@BeforeEach
4843
void setUp() throws Exception {
@@ -58,13 +53,14 @@ void tearDown() throws Exception {
5853
this.server.shutdown();
5954
}
6055

56+
6157
protected class TestDispatcher extends Dispatcher {
58+
6259
@Override
6360
public MockResponse dispatch(RecordedRequest request) {
6461
try {
6562
if (request.getPath().equals("/echo")) {
66-
assertThat(request.getHeader("Host"))
67-
.contains("localhost:" + port);
63+
assertThat(request.getHeader("Host")).contains("localhost:" + port);
6864
MockResponse response = new MockResponse()
6965
.setHeaders(request.getHeaders())
7066
.setHeader("Content-Length", request.getBody().size())
@@ -80,8 +76,7 @@ else if(request.getPath().equals("/status/notfound")) {
8076
return new MockResponse().setResponseCode(404);
8177
}
8278
else if (request.getPath().equals("/status/299")) {
83-
assertThat(request.getHeader("Expect"))
84-
.contains("299");
79+
assertThat(request.getHeader("Expect")).contains("299");
8580
return new MockResponse().setResponseCode(299);
8681
}
8782
else if(request.getPath().startsWith("/params")) {
@@ -112,8 +107,8 @@ else if(request.getPath().startsWith("/header/")) {
112107
}
113108
return new MockResponse().setResponseCode(404);
114109
}
115-
catch (Throwable exc) {
116-
return new MockResponse().setResponseCode(500).setBody(exc.toString());
110+
catch (Throwable ex) {
111+
return new MockResponse().setResponseCode(500).setBody(ex.toString());
117112
}
118113
}
119114
}

spring-web/src/test/java/org/springframework/http/client/BufferingClientHttpRequestFactoryTests.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -18,7 +18,6 @@
1818

1919
import java.net.URI;
2020
import java.nio.charset.StandardCharsets;
21-
import java.util.Arrays;
2221

2322
import org.junit.jupiter.api.Test;
2423

@@ -37,30 +36,29 @@ protected ClientHttpRequestFactory createRequestFactory() {
3736

3837
@Test
3938
void repeatableRead() throws Exception {
39+
4040
ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/echo"), HttpMethod.PUT);
41-
assertThat(request.getMethod()).as("Invalid HTTP method").isEqualTo(HttpMethod.PUT);
42-
String headerName = "MyHeader";
43-
String headerValue1 = "value1";
44-
request.getHeaders().add(headerName, headerValue1);
45-
String headerValue2 = "value2";
46-
request.getHeaders().add(headerName, headerValue2);
41+
assertThat(request.getMethod()).isEqualTo(HttpMethod.PUT);
42+
43+
String header = "MyHeader";
44+
request.getHeaders().add(header, "value1");
45+
request.getHeaders().add(header, "value2");
46+
4747
byte[] body = "Hello World".getBytes(StandardCharsets.UTF_8);
48-
request.getHeaders().setContentLength(body.length);
4948
FileCopyUtils.copy(body, request.getBody());
50-
try (ClientHttpResponse response = request.execute()) {
51-
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
52-
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
49+
request.getHeaders().setContentLength(body.length);
5350

54-
assertThat(response.getHeaders().containsHeader(headerName)).as("Header not found").isTrue();
55-
assertThat(response.getHeaders().containsHeader(headerName)).as("Header not found").isTrue();
51+
try (ClientHttpResponse response = request.execute()) {
52+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
5653

57-
assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2));
58-
assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2));
54+
assertThat(response.getHeaders().get(header)).containsExactly("value1", "value2");
55+
assertThat(response.getHeaders().get(header)).containsExactly("value1", "value2");
5956

6057
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
61-
assertThat(Arrays.equals(body, result)).as("Invalid body").isTrue();
62-
FileCopyUtils.copyToByteArray(response.getBody());
63-
assertThat(Arrays.equals(body, result)).as("Invalid body").isTrue();
58+
assertThat(result).isEqualTo(body);
59+
60+
result = FileCopyUtils.copyToByteArray(response.getBody());
61+
assertThat(result).isEqualTo(body);
6462
}
6563
}
6664

spring-web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void beforeEach() {
5656
}
5757

5858
@Test
59-
void shouldInvokeInterceptors() throws Exception {
59+
void invokeInterceptors() throws Exception {
6060
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
6161
interceptors.add(new NoOpInterceptor());
6262
interceptors.add(new NoOpInterceptor());
@@ -74,7 +74,7 @@ void shouldInvokeInterceptors() throws Exception {
7474
}
7575

7676
@Test
77-
void shouldSkipIntercetor() throws Exception {
77+
void skipInterceptor() throws Exception {
7878
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
7979
interceptors.add((request, body, execution) -> responseMock);
8080
interceptors.add(new NoOpInterceptor());
@@ -89,7 +89,7 @@ void shouldSkipIntercetor() throws Exception {
8989
}
9090

9191
@Test
92-
void interceptorShouldUpdateRequestHeader() throws Exception {
92+
void updateRequestHeader() throws Exception {
9393
final String headerName = "Foo";
9494
final String headerValue = "Bar";
9595
final String otherValue = "Baz";
@@ -115,7 +115,7 @@ protected ClientHttpResponse executeInternal() {
115115
}
116116

117117
@Test
118-
void interceptorShouldUpdateRequestAttribute() throws Exception {
118+
void updateRequestAttribute() throws Exception {
119119
final String attrName = "Foo";
120120
final String attrValue = "Bar";
121121

@@ -137,7 +137,7 @@ protected ClientHttpResponse executeInternal() {
137137
}
138138

139139
@Test
140-
void interceptorShouldUpdateRequestURI() throws Exception {
140+
void updateRequestURI() throws Exception {
141141
final URI changedUri = URI.create("https://example.com/2");
142142

143143
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(new HttpRequestWrapper(request) {
@@ -161,7 +161,7 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
161161
}
162162

163163
@Test
164-
void interceptorShouldUpdateRequestMethod() throws Exception {
164+
void updateRequestMethod() throws Exception {
165165
final HttpMethod changedMethod = HttpMethod.POST;
166166

167167
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(new HttpRequestWrapper(request) {
@@ -185,7 +185,7 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
185185
}
186186

187187
@Test
188-
void interceptorShouldUpdateRequestBody() throws Exception {
188+
void updateRequestBody() throws Exception {
189189
final byte[] changedBody = "Foo".getBytes();
190190
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> execution.execute(request, changedBody);
191191
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
@@ -197,7 +197,7 @@ void interceptorShouldUpdateRequestBody() throws Exception {
197197
}
198198

199199
@Test
200-
void interceptorShouldAlwaysExecuteNextInterceptor() throws Exception {
200+
void multipleExecutions() throws Exception {
201201
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
202202
interceptors.add(new MultipleExecutionInterceptor());
203203
interceptors.add(new NoOpInterceptor());
@@ -217,18 +217,21 @@ private static class NoOpInterceptor implements ClientHttpRequestInterceptor {
217217
private int invocationCount = 0;
218218

219219
@Override
220-
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
221-
throws IOException {
220+
public ClientHttpResponse intercept(
221+
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
222+
222223
invocationCount++;
223224
return execution.execute(request, body);
224225
}
225226
}
226227

228+
227229
private static class MultipleExecutionInterceptor implements ClientHttpRequestInterceptor {
228230

229231
@Override
230-
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
231-
throws IOException {
232+
public ClientHttpResponse intercept(
233+
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
234+
232235
// execute another request first
233236
execution.execute(new MockClientHttpRequest(), body);
234237
return execution.execute(request, body);

spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -39,6 +39,7 @@ class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
3939

4040
private static @Nullable String originalPropertyValue;
4141

42+
4243
@BeforeAll
4344
static void setProperty() {
4445
originalPropertyValue = System.getProperty("jdk.httpclient.allowRestrictedHeaders");
@@ -55,6 +56,7 @@ static void restoreProperty() {
5556
}
5657
}
5758

59+
5860
@Override
5961
protected ClientHttpRequestFactory createRequestFactory() {
6062
return new JdkClientHttpRequestFactory();
@@ -69,7 +71,8 @@ void httpMethods() throws Exception {
6971

7072
@Test
7173
void customizeDisallowedHeaders() throws IOException {
72-
ClientHttpRequest request = this.factory.createRequest(URI.create(this.baseUrl + "/status/299"), HttpMethod.PUT);
74+
URI uri = URI.create(this.baseUrl + "/status/299");
75+
ClientHttpRequest request = this.factory.createRequest(uri, HttpMethod.PUT);
7376
request.getHeaders().set("Expect", "299");
7477

7578
try (ClientHttpResponse response = request.execute()) {
@@ -79,8 +82,9 @@ void customizeDisallowedHeaders() throws IOException {
7982

8083
@Test // gh-31451
8184
public void contentLength0() throws IOException {
82-
BufferingClientHttpRequestFactory bufferingFactory = new BufferingClientHttpRequestFactory(this.factory);
83-
ClientHttpRequest request = bufferingFactory.createRequest(URI.create(this.baseUrl + "/methods/get"), HttpMethod.GET);
85+
URI uri = URI.create(this.baseUrl + "/methods/get");
86+
ClientHttpRequest request =
87+
new BufferingClientHttpRequestFactory(this.factory).createRequest(uri, HttpMethod.GET);
8488

8589
try (ClientHttpResponse response = request.execute()) {
8690
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);

0 commit comments

Comments
 (0)