Skip to content

Commit f6e2c58

Browse files
committed
Refactor tests to use toExchange() shortcuts
Issue: SPR-15350
1 parent 41c413a commit f6e2c58

File tree

71 files changed

+1281
-1857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1281
-1857
lines changed

spring-web/src/test/java/org/springframework/web/bind/support/WebExchangeDataBinderTests.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@
2727

2828
import org.springframework.http.MediaType;
2929
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
30-
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
3130
import org.springframework.tests.sample.beans.ITestBean;
3231
import org.springframework.tests.sample.beans.TestBean;
3332
import org.springframework.util.LinkedMultiValueMap;
3433
import org.springframework.util.MultiValueMap;
3534
import org.springframework.web.server.ServerWebExchange;
36-
import org.springframework.web.server.adapter.DefaultServerWebExchange;
3735

3836
import static junit.framework.TestCase.assertFalse;
3937
import static org.junit.Assert.assertEquals;
@@ -173,8 +171,7 @@ public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception
173171
public void testBindingWithQueryParams() throws Exception {
174172
String url = "/path?spouse=someValue&spouse.name=test";
175173
MockServerHttpRequest request = MockServerHttpRequest.post(url).build();
176-
ServerWebExchange exchange = new DefaultServerWebExchange(request, new MockServerHttpResponse());
177-
this.binder.bind(exchange).block(Duration.ofMillis(5000));
174+
this.binder.bind(request.toExchange()).block(Duration.ofSeconds(5));
178175

179176
assertNotNull(this.testBean.getSpouse());
180177
assertEquals("test", this.testBean.getSpouse().getName());
@@ -208,13 +205,11 @@ private String generateForm(MultiValueMap<String, String> form) {
208205
}
209206

210207
private ServerWebExchange exchange(MultiValueMap<String, String> formData) {
211-
212-
MockServerHttpRequest request = MockServerHttpRequest
208+
return MockServerHttpRequest
213209
.post("/")
214210
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
215-
.body(generateForm(formData));
216-
217-
return new DefaultServerWebExchange(request, new MockServerHttpResponse());
211+
.body(generateForm(formData))
212+
.toExchange();
218213
}
219214

220215

spring-web/src/test/java/org/springframework/web/cors/reactive/DefaultCorsProcessorTests.java

Lines changed: 166 additions & 139 deletions
Large diffs are not rendered by default.

spring-web/src/test/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSourceTests.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818

1919
import org.junit.Test;
2020

21-
import org.springframework.http.HttpMethod;
22-
import org.springframework.http.server.reactive.ServerHttpRequest;
2321
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
24-
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
2522
import org.springframework.web.cors.CorsConfiguration;
2623
import org.springframework.web.server.ServerWebExchange;
27-
import org.springframework.web.server.adapter.DefaultServerWebExchange;
2824

2925
import static org.junit.Assert.assertEquals;
3026
import static org.junit.Assert.assertNull;
@@ -42,7 +38,7 @@ public class UrlBasedCorsConfigurationSourceTests {
4238

4339
@Test
4440
public void empty() {
45-
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/bar/test.html");
41+
ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
4642
assertNull(this.configSource.getCorsConfiguration(exchange));
4743
}
4844

@@ -51,10 +47,10 @@ public void registerAndMatch() {
5147
CorsConfiguration config = new CorsConfiguration();
5248
this.configSource.registerCorsConfiguration("/bar/**", config);
5349

54-
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/foo/test.html");
50+
ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
5551
assertNull(this.configSource.getCorsConfiguration(exchange));
5652

57-
exchange = createExchange(HttpMethod.GET, "/bar/test.html");
53+
exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
5854
assertEquals(config, this.configSource.getCorsConfiguration(exchange));
5955
}
6056

@@ -63,10 +59,4 @@ public void unmodifiableConfigurationsMap() {
6359
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
6460
}
6561

66-
private ServerWebExchange createExchange(HttpMethod httpMethod, String url) {
67-
ServerHttpRequest request = MockServerHttpRequest.method(httpMethod, url).build();
68-
MockServerHttpResponse response = new MockServerHttpResponse();
69-
return new DefaultServerWebExchange(request, response);
70-
}
71-
7262
}

spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@
2727
import org.springframework.http.HttpMethod;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
30-
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
3130
import org.springframework.web.server.ServerWebExchange;
3231
import org.springframework.web.server.WebFilterChain;
33-
import org.springframework.web.server.adapter.DefaultServerWebExchange;
3432

3533
import static org.junit.Assert.assertEquals;
3634
import static org.junit.Assert.assertThat;
@@ -145,9 +143,7 @@ private ServerWebExchange createExchange(String methodName, Optional<String> opt
145143
.map(method -> builder.body(methodName + "=" + method))
146144
.orElse(builder.build());
147145

148-
MockServerHttpResponse response = new MockServerHttpResponse();
149-
150-
return new DefaultServerWebExchange(request, response);
146+
return request.toExchange();
151147
}
152148

153149
}

0 commit comments

Comments
 (0)