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.
34
34
import org .springframework .http .HttpHeaders ;
35
35
import org .springframework .http .MediaType ;
36
36
37
- import static org .junit .Assert .*;
38
- import static org .mockito .Mockito .*;
37
+ import static org .junit .Assert .assertEquals ;
38
+ import static org .junit .Assert .assertFalse ;
39
+ import static org .junit .Assert .assertNull ;
40
+ import static org .mockito .Mockito .any ;
41
+ import static org .mockito .Mockito .mock ;
42
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
43
+ import static org .mockito .Mockito .verifyZeroInteractions ;
44
+ import static org .mockito .Mockito .when ;
39
45
40
46
/**
41
47
* Unit tests for {@link DefaultWebClient}.
42
48
*
43
49
* @author Rossen Stoyanchev
50
+ * @author Brian Clozel
44
51
*/
45
52
public class DefaultWebClientTests {
46
53
@@ -56,14 +63,15 @@ public class DefaultWebClientTests {
56
63
public void setup () {
57
64
MockitoAnnotations .initMocks (this );
58
65
this .exchangeFunction = mock (ExchangeFunction .class );
59
- when (this .exchangeFunction .exchange (this .captor .capture ())).thenReturn (Mono .empty ());
66
+ ClientResponse mockResponse = mock (ClientResponse .class );
67
+ when (this .exchangeFunction .exchange (this .captor .capture ())).thenReturn (Mono .just (mockResponse ));
60
68
this .builder = WebClient .builder ().baseUrl ("/base" ).exchangeFunction (this .exchangeFunction );
61
69
}
62
70
63
71
64
72
@ Test
65
73
public void basic () {
66
- this .builder .build ().get ().uri ("/path" ).exchange ();
74
+ this .builder .build ().get ().uri ("/path" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
67
75
68
76
ClientRequest request = verifyAndGetRequest ();
69
77
assertEquals ("/base/path" , request .url ().toString ());
@@ -75,7 +83,8 @@ public void basic() {
75
83
public void uriBuilder () {
76
84
this .builder .build ().get ()
77
85
.uri (builder -> builder .path ("/path" ).queryParam ("q" , "12" ).build ())
78
- .exchange ();
86
+ .exchange ()
87
+ .block (Duration .ofSeconds (10 ));
79
88
80
89
ClientRequest request = verifyAndGetRequest ();
81
90
assertEquals ("/base/path?q=12" , request .url ().toString ());
@@ -86,7 +95,8 @@ public void uriBuilder() {
86
95
public void uriBuilderWithPathOverride () {
87
96
this .builder .build ().get ()
88
97
.uri (builder -> builder .replacePath ("/path" ).build ())
89
- .exchange ();
98
+ .exchange ()
99
+ .block (Duration .ofSeconds (10 ));
90
100
91
101
ClientRequest request = verifyAndGetRequest ();
92
102
assertEquals ("/path" , request .url ().toString ());
@@ -97,7 +107,8 @@ public void uriBuilderWithPathOverride() {
97
107
public void requestHeaderAndCookie () {
98
108
this .builder .build ().get ().uri ("/path" ).accept (MediaType .APPLICATION_JSON )
99
109
.cookies (cookies -> cookies .add ("id" , "123" )) // SPR-16178
100
- .exchange ();
110
+ .exchange ()
111
+ .block (Duration .ofSeconds (10 ));
101
112
102
113
ClientRequest request = verifyAndGetRequest ();
103
114
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
@@ -111,7 +122,7 @@ public void defaultHeaderAndCookie() {
111
122
.defaultHeader ("Accept" , "application/json" ).defaultCookie ("id" , "123" )
112
123
.build ();
113
124
114
- client .get ().uri ("/path" ).exchange ();
125
+ client .get ().uri ("/path" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
115
126
116
127
ClientRequest request = verifyAndGetRequest ();
117
128
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
@@ -126,7 +137,8 @@ public void defaultHeaderAndCookieOverrides() {
126
137
.defaultCookie ("id" , "123" )
127
138
.build ();
128
139
129
- client .get ().uri ("/path" ).header ("Accept" , "application/xml" ).cookie ("id" , "456" ).exchange ();
140
+ client .get ().uri ("/path" ).header ("Accept" , "application/xml" ).cookie ("id" , "456" )
141
+ .exchange ().block (Duration .ofSeconds (10 ));
130
142
131
143
ClientRequest request = verifyAndGetRequest ();
132
144
assertEquals ("application/xml" , request .headers ().getFirst ("Accept" ));
@@ -151,7 +163,7 @@ public void defaultRequest() {
151
163
152
164
try {
153
165
context .set ("bar" );
154
- client .get ().uri ("/path" ).attribute ("foo" , "bar" ).exchange ();
166
+ client .get ().uri ("/path" ).attribute ("foo" , "bar" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
155
167
}
156
168
finally {
157
169
context .remove ();
@@ -219,7 +231,8 @@ public void withStringAttribute() {
219
231
this .builder .filter (filter ).build ()
220
232
.get ().uri ("/path" )
221
233
.attribute ("foo" , "bar" )
222
- .exchange ();
234
+ .exchange ()
235
+ .block (Duration .ofSeconds (10 ));
223
236
224
237
assertEquals ("bar" , actual .get ("foo" ));
225
238
@@ -238,7 +251,8 @@ public void withNullAttribute() {
238
251
this .builder .filter (filter ).build ()
239
252
.get ().uri ("/path" )
240
253
.attribute ("foo" , null )
241
- .exchange ();
254
+ .exchange ()
255
+ .block (Duration .ofSeconds (10 ));
242
256
243
257
assertNull (actual .get ("foo" ));
244
258
@@ -254,7 +268,7 @@ public void apply() {
254
268
.defaultCookie ("id" , "123" ))
255
269
.build ();
256
270
257
- client .get ().uri ("/path" ).exchange ();
271
+ client .get ().uri ("/path" ).exchange (). block ( Duration . ofSeconds ( 10 )) ;
258
272
259
273
ClientRequest request = verifyAndGetRequest ();
260
274
assertEquals ("application/json" , request .headers ().getFirst ("Accept" ));
@@ -264,11 +278,31 @@ public void apply() {
264
278
265
279
@ Test
266
280
public void switchToErrorOnEmptyClientResponseMono () {
281
+ ExchangeFunction exchangeFunction = mock (ExchangeFunction .class );
282
+ when (exchangeFunction .exchange (any ())).thenReturn (Mono .empty ());
283
+ WebClient .Builder builder = WebClient .builder ().baseUrl ("/base" ).exchangeFunction (exchangeFunction );
267
284
StepVerifier .create (builder .build ().get ().uri ("/path" ).exchange ())
268
285
.expectErrorMessage ("The underlying HTTP client completed without emitting a response." )
269
286
.verify (Duration .ofSeconds (5 ));
270
287
}
271
288
289
+ @ Test // gh-23909
290
+ public void shouldApplyFiltersAtSubscription () {
291
+ WebClient client = this .builder
292
+ .filter ((request , next ) ->
293
+ next .exchange (ClientRequest
294
+ .from (request )
295
+ .header ("Custom" , "value" )
296
+ .build ()))
297
+ .build ();
298
+ Mono <ClientResponse > exchange = client .get ().uri ("/path" ).exchange ();
299
+ verifyZeroInteractions (this .exchangeFunction );
300
+ exchange .block (Duration .ofSeconds (10 ));
301
+ ClientRequest request = verifyAndGetRequest ();
302
+ assertEquals ("value" , request .headers ().getFirst ("Custom" ));
303
+ }
304
+
305
+
272
306
private ClientRequest verifyAndGetRequest () {
273
307
ClientRequest request = this .captor .getValue ();
274
308
Mockito .verify (this .exchangeFunction ).exchange (request );
0 commit comments