1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
* Unit tests for {@link DefaultWebClient}.
42
42
*
43
43
* @author Rossen Stoyanchev
44
+ * @author Brian Clozel
44
45
*/
45
46
public class DefaultWebClientTests {
46
47
@@ -56,14 +57,16 @@ public class DefaultWebClientTests {
56
57
public void setup () {
57
58
MockitoAnnotations .initMocks (this );
58
59
this .exchangeFunction = mock (ExchangeFunction .class );
59
- when (this .exchangeFunction .exchange (this .captor .capture ())).thenReturn (Mono .empty ());
60
+ ClientResponse mockResponse = mock (ClientResponse .class );
61
+ when (this .exchangeFunction .exchange (this .captor .capture ())).thenReturn (Mono .just (mockResponse ));
60
62
this .builder = WebClient .builder ().baseUrl ("/base" ).exchangeFunction (this .exchangeFunction );
61
63
}
62
64
63
65
64
66
@ Test
65
67
public void basic () {
66
- this .builder .build ().get ().uri ("/path" ).exchange ();
68
+ this .builder .build ().get ().uri ("/path" )
69
+ .exchange ().block (Duration .ofSeconds (10 ));
67
70
68
71
ClientRequest request = verifyAndGetRequest ();
69
72
assertEquals ("/base/path" , request .url ().toString ());
@@ -75,34 +78,31 @@ public void basic() {
75
78
public void uriBuilder () {
76
79
this .builder .build ().get ()
77
80
.uri (builder -> builder .path ("/path" ).queryParam ("q" , "12" ).build ())
78
- .exchange ();
81
+ .exchange (). block ( Duration . ofSeconds ( 10 )) ;
79
82
80
83
ClientRequest request = verifyAndGetRequest ();
81
84
assertEquals ("/base/path?q=12" , request .url ().toString ());
82
- verifyNoMoreInteractions (this .exchangeFunction );
83
85
}
84
86
85
87
@ Test
86
88
public void uriBuilderWithPathOverride () {
87
89
this .builder .build ().get ()
88
90
.uri (builder -> builder .replacePath ("/path" ).build ())
89
- .exchange ();
91
+ .exchange (). block ( Duration . ofSeconds ( 10 )) ;
90
92
91
93
ClientRequest request = verifyAndGetRequest ();
92
94
assertEquals ("/path" , request .url ().toString ());
93
- verifyNoMoreInteractions (this .exchangeFunction );
94
95
}
95
96
96
97
@ Test
97
98
public void requestHeaderAndCookie () {
98
99
this .builder .build ().get ().uri ("/path" ).accept (MediaType .APPLICATION_JSON )
99
100
.cookies (cookies -> cookies .add ("id" , "123" )) // SPR-16178
100
- .exchange ();
101
+ .exchange (). block ( Duration . ofSeconds ( 10 )) ;
101
102
102
103
ClientRequest request = verifyAndGetRequest ();
103
104
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
104
105
assertEquals ("123" , request .cookies ().getFirst ("id" ));
105
- verifyNoMoreInteractions (this .exchangeFunction );
106
106
}
107
107
108
108
@ Test
@@ -111,12 +111,11 @@ public void defaultHeaderAndCookie() {
111
111
.defaultHeader ("Accept" , "application/json" ).defaultCookie ("id" , "123" )
112
112
.build ();
113
113
114
- client .get ().uri ("/path" ).exchange ();
114
+ client .get ().uri ("/path" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
115
115
116
116
ClientRequest request = verifyAndGetRequest ();
117
117
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
118
118
assertEquals ("123" , request .cookies ().getFirst ("id" ));
119
- verifyNoMoreInteractions (this .exchangeFunction );
120
119
}
121
120
122
121
@ Test
@@ -126,12 +125,14 @@ public void defaultHeaderAndCookieOverrides() {
126
125
.defaultCookie ("id" , "123" )
127
126
.build ();
128
127
129
- client .get ().uri ("/path" ).header ("Accept" , "application/xml" ).cookie ("id" , "456" ).exchange ();
128
+ client .get ().uri ("/path" )
129
+ .header ("Accept" , "application/xml" )
130
+ .cookie ("id" , "456" )
131
+ .exchange ().block (Duration .ofSeconds (10 ));
130
132
131
133
ClientRequest request = verifyAndGetRequest ();
132
134
assertEquals ("application/xml" , request .headers ().getFirst ("Accept" ));
133
135
assertEquals ("456" , request .cookies ().getFirst ("id" ));
134
- verifyNoMoreInteractions (this .exchangeFunction );
135
136
}
136
137
137
138
@ Test
@@ -151,7 +152,8 @@ public void defaultRequest() {
151
152
152
153
try {
153
154
context .set ("bar" );
154
- client .get ().uri ("/path" ).attribute ("foo" , "bar" ).exchange ();
155
+ client .get ().uri ("/path" ).attribute ("foo" , "bar" )
156
+ .exchange ().block (Duration .ofSeconds (10 ));
155
157
}
156
158
finally {
157
159
context .remove ();
@@ -219,7 +221,7 @@ public void withStringAttribute() {
219
221
this .builder .filter (filter ).build ()
220
222
.get ().uri ("/path" )
221
223
.attribute ("foo" , "bar" )
222
- .exchange ();
224
+ .exchange (). block ( Duration . ofSeconds ( 10 )) ;
223
225
224
226
assertEquals ("bar" , actual .get ("foo" ));
225
227
@@ -238,7 +240,7 @@ public void withNullAttribute() {
238
240
this .builder .filter (filter ).build ()
239
241
.get ().uri ("/path" )
240
242
.attribute ("foo" , null )
241
- .exchange ();
243
+ .exchange (). block ( Duration . ofSeconds ( 10 )) ;
242
244
243
245
assertNull (actual .get ("foo" ));
244
246
@@ -254,21 +256,40 @@ public void apply() {
254
256
.defaultCookie ("id" , "123" ))
255
257
.build ();
256
258
257
- client .get ().uri ("/path" ).exchange ();
259
+ client .get ().uri ("/path" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
258
260
259
261
ClientRequest request = verifyAndGetRequest ();
260
262
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
261
263
assertEquals ("123" , request .cookies ().getFirst ("id" ));
262
- verifyNoMoreInteractions (this .exchangeFunction );
263
264
}
264
265
265
266
@ Test
266
267
public void switchToErrorOnEmptyClientResponseMono () {
268
+ ExchangeFunction exchangeFunction = mock (ExchangeFunction .class );
269
+ when (exchangeFunction .exchange (any ())).thenReturn (Mono .empty ());
270
+ WebClient .Builder builder = WebClient .builder ().baseUrl ("/base" ).exchangeFunction (exchangeFunction );
267
271
StepVerifier .create (builder .build ().get ().uri ("/path" ).exchange ())
268
272
.expectErrorMessage ("The underlying HTTP client completed without emitting a response." )
269
273
.verify (Duration .ofSeconds (5 ));
270
274
}
271
275
276
+ @ Test
277
+ public void shouldApplyFiltersAtSubscription () {
278
+ WebClient client = this .builder
279
+ .filter ((request , next ) -> {
280
+ return next .exchange (ClientRequest
281
+ .from (request )
282
+ .header ("Custom" , "value" )
283
+ .build ());
284
+ })
285
+ .build ();
286
+ Mono <ClientResponse > exchange = client .get ().uri ("/path" ).exchange ();
287
+ verifyZeroInteractions (this .exchangeFunction );
288
+ exchange .block (Duration .ofSeconds (10 ));
289
+ ClientRequest request = verifyAndGetRequest ();
290
+ assertEquals ("value" , request .headers ().getFirst ("Custom" ));
291
+ }
292
+
272
293
private ClientRequest verifyAndGetRequest () {
273
294
ClientRequest request = this .captor .getValue ();
274
295
Mockito .verify (this .exchangeFunction ).exchange (request );
0 commit comments