17
17
package org .springframework .web .reactive .function .client ;
18
18
19
19
import java .net .URI ;
20
+ import java .nio .charset .StandardCharsets ;
20
21
21
22
import org .junit .Test ;
23
+ import reactor .core .publisher .Flux ;
22
24
import reactor .core .publisher .Mono ;
23
25
import reactor .test .StepVerifier ;
24
26
27
+ import org .springframework .core .io .buffer .DataBuffer ;
28
+ import org .springframework .core .io .buffer .DataBufferUtils ;
29
+ import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
30
+ import org .springframework .core .io .buffer .support .DataBufferTestUtils ;
25
31
import org .springframework .http .HttpHeaders ;
32
+ import org .springframework .http .HttpMethod ;
26
33
import org .springframework .http .HttpStatus ;
34
+ import org .springframework .web .reactive .function .BodyExtractors ;
27
35
28
36
import static org .junit .Assert .*;
29
37
import static org .mockito .Mockito .*;
30
- import static org .springframework .http .HttpMethod .GET ;
31
- import static org .springframework .web .reactive .function .client .ExchangeFilterFunctions .Credentials .basicAuthenticationCredentials ;
32
38
33
39
/**
34
40
* @author Arjen Poutsma
35
41
*/
36
- @ SuppressWarnings ("deprecation" )
37
42
public class ExchangeFilterFunctionsTests {
38
43
44
+ private static final URI DEFAULT_URL = URI .create ("http://example.com" );
45
+
46
+
39
47
@ Test
40
48
public void andThen () {
41
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
49
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
42
50
ClientResponse response = mock (ClientResponse .class );
43
51
ExchangeFunction exchange = r -> Mono .just (response );
44
52
@@ -68,7 +76,7 @@ public void andThen() {
68
76
69
77
@ Test
70
78
public void apply () {
71
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
79
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
72
80
ClientResponse response = mock (ClientResponse .class );
73
81
ExchangeFunction exchange = r -> Mono .just (response );
74
82
@@ -86,8 +94,9 @@ public void apply() {
86
94
}
87
95
88
96
@ Test
97
+ @ SuppressWarnings ("deprecation" )
89
98
public void basicAuthenticationUsernamePassword () {
90
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
99
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
91
100
ClientResponse response = mock (ClientResponse .class );
92
101
93
102
ExchangeFunction exchange = r -> {
@@ -109,9 +118,11 @@ public void basicAuthenticationInvalidCharacters() {
109
118
}
110
119
111
120
@ Test
121
+ @ SuppressWarnings ("deprecation" )
112
122
public void basicAuthenticationAttributes () {
113
- ClientRequest request = ClientRequest .create (GET , URI .create ("http://example.com" ))
114
- .attributes (basicAuthenticationCredentials ("foo" , "bar" ))
123
+ ClientRequest request = ClientRequest .create (HttpMethod .GET , DEFAULT_URL )
124
+ .attributes (org .springframework .web .reactive .function .client .ExchangeFilterFunctions
125
+ .Credentials .basicAuthenticationCredentials ("foo" , "bar" ))
115
126
.build ();
116
127
ClientResponse response = mock (ClientResponse .class );
117
128
@@ -128,8 +139,9 @@ public void basicAuthenticationAttributes() {
128
139
}
129
140
130
141
@ Test
142
+ @ SuppressWarnings ("deprecation" )
131
143
public void basicAuthenticationAbsentAttributes () {
132
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
144
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
133
145
ClientResponse response = mock (ClientResponse .class );
134
146
135
147
ExchangeFunction exchange = r -> {
@@ -145,7 +157,7 @@ public void basicAuthenticationAbsentAttributes() {
145
157
146
158
@ Test
147
159
public void statusHandlerMatch () {
148
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
160
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
149
161
ClientResponse response = mock (ClientResponse .class );
150
162
when (response .statusCode ()).thenReturn (HttpStatus .NOT_FOUND );
151
163
@@ -163,23 +175,52 @@ public void statusHandlerMatch() {
163
175
164
176
@ Test
165
177
public void statusHandlerNoMatch () {
166
- ClientRequest request = ClientRequest .create (GET , URI . create ( "http://example.com" ) ).build ();
178
+ ClientRequest request = ClientRequest .create (HttpMethod . GET , DEFAULT_URL ).build ();
167
179
ClientResponse response = mock (ClientResponse .class );
168
180
when (response .statusCode ()).thenReturn (HttpStatus .NOT_FOUND );
169
181
170
- ExchangeFunction exchange = r -> Mono .just (response );
171
-
172
- ExchangeFilterFunction errorHandler = ExchangeFilterFunctions .statusError (
173
- HttpStatus ::is5xxServerError , r -> new MyException ());
174
-
175
- Mono <ClientResponse > result = errorHandler .filter (request , exchange );
182
+ Mono <ClientResponse > result = ExchangeFilterFunctions
183
+ .statusError (HttpStatus ::is5xxServerError , req -> new MyException ())
184
+ .filter (request , req -> Mono .just (response ));
176
185
177
186
StepVerifier .create (result )
178
187
.expectNext (response )
179
188
.expectComplete ()
180
189
.verify ();
181
190
}
182
191
192
+ @ Test
193
+ public void limitResponseSize () {
194
+ DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory ();
195
+ DataBuffer b1 = dataBuffer ("foo" , bufferFactory );
196
+ DataBuffer b2 = dataBuffer ("bar" , bufferFactory );
197
+ DataBuffer b3 = dataBuffer ("baz" , bufferFactory );
198
+
199
+ ClientRequest request = ClientRequest .create (HttpMethod .GET , DEFAULT_URL ).build ();
200
+ ClientResponse response = ClientResponse .create (HttpStatus .OK ).body (Flux .just (b1 , b2 , b3 )).build ();
201
+
202
+ Mono <ClientResponse > result = ExchangeFilterFunctions .limitResponseSize (5 )
203
+ .filter (request , req -> Mono .just (response ));
204
+
205
+ StepVerifier .create (result .flatMapMany (res -> res .body (BodyExtractors .toDataBuffers ())))
206
+ .consumeNextWith (buffer -> assertEquals ("foo" , string (buffer )))
207
+ .consumeNextWith (buffer -> assertEquals ("ba" , string (buffer )))
208
+ .expectComplete ()
209
+ .verify ();
210
+
211
+ }
212
+
213
+ private String string (DataBuffer buffer ) {
214
+ String value = DataBufferTestUtils .dumpString (buffer , StandardCharsets .UTF_8 );
215
+ DataBufferUtils .release (buffer );
216
+ return value ;
217
+ }
218
+
219
+ private DataBuffer dataBuffer (String foo , DefaultDataBufferFactory bufferFactory ) {
220
+ return bufferFactory .wrap (foo .getBytes (StandardCharsets .UTF_8 ));
221
+ }
222
+
223
+
183
224
@ SuppressWarnings ("serial" )
184
225
private static class MyException extends Exception {
185
226
0 commit comments