1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 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.
16
16
17
17
package org .springframework .web .server .adapter ;
18
18
19
+ import java .nio .charset .StandardCharsets ;
20
+ import java .util .Collections ;
21
+ import java .util .concurrent .atomic .AtomicBoolean ;
22
+ import java .util .function .BiFunction ;
23
+ import java .util .function .Function ;
24
+
19
25
import org .junit .jupiter .api .Test ;
26
+ import reactor .core .publisher .Flux ;
27
+ import reactor .core .publisher .Mono ;
28
+
20
29
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
21
30
import org .springframework .context .annotation .Bean ;
22
31
import org .springframework .context .annotation .Configuration ;
33
42
import org .springframework .web .server .WebHandler ;
34
43
import org .springframework .web .testfixture .http .server .reactive .MockServerHttpRequest ;
35
44
import org .springframework .web .testfixture .http .server .reactive .MockServerHttpResponse ;
36
- import reactor .core .publisher .Flux ;
37
- import reactor .core .publisher .Mono ;
38
-
39
- import java .nio .charset .StandardCharsets ;
40
- import java .util .Collections ;
41
- import java .util .concurrent .atomic .AtomicBoolean ;
42
- import java .util .concurrent .atomic .AtomicLong ;
43
- import java .util .function .BiFunction ;
44
45
45
46
import static java .time .Duration .ofMillis ;
46
47
import static org .assertj .core .api .Assertions .assertThat ;
@@ -141,21 +142,16 @@ void httpHandlerDecorator() {
141
142
}
142
143
143
144
@ Test
144
- void httpHandlerDecoratorBeans () {
145
+ void httpHandlerDecoratorFactoryBeans () {
146
+ HttpHandler handler = WebHttpHandlerBuilder .applicationContext (
147
+ new AnnotationConfigApplicationContext (HttpHandlerDecoratorFactoryBeansConfig .class )).build ();
145
148
146
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
147
- context .register (HttpHandlerDecoratorBeansConfig .class );
148
- context .refresh ();
149
- HttpHandler builder = WebHttpHandlerBuilder .applicationContext (context ).build ();
150
-
151
- builder .handle (MockServerHttpRequest .get ("/" ).build (), new MockServerHttpResponse ()).block ();
152
-
153
- AtomicLong decorator1NanoTime = context .getBean ("decorator1NanoTime" , AtomicLong .class );
154
- AtomicLong decorator2NanoTime = context .getBean ("decorator2NanoTime" , AtomicLong .class );
155
- AtomicLong decorator3NanoTime = context .getBean ("decorator3NanoTime" , AtomicLong .class );
156
- assertThat (decorator1NanoTime ).hasValueLessThan (decorator3NanoTime .get ());
157
- assertThat (decorator3NanoTime ).hasValueLessThan (decorator2NanoTime .get ());
149
+ MockServerHttpResponse response = new MockServerHttpResponse ();
150
+ handler .handle (MockServerHttpRequest .get ("/" ).build (), response ).block ();
158
151
152
+ Function <String , Long > headerValue = name -> Long .valueOf (response .getHeaders ().getFirst (name ));
153
+ assertThat (headerValue .apply ("decoratorA" )).isLessThan (headerValue .apply ("decoratorB" ));
154
+ assertThat (headerValue .apply ("decoratorC" )).isLessThan (headerValue .apply ("decoratorB" ));
159
155
}
160
156
161
157
private static Mono <Void > writeToResponse (ServerWebExchange exchange , String value ) {
@@ -164,56 +160,41 @@ private static Mono<Void> writeToResponse(ServerWebExchange exchange, String val
164
160
return exchange .getResponse ().writeWith (Flux .just (buffer ));
165
161
}
166
162
167
- @ Configuration
168
- static class HttpHandlerDecoratorBeansConfig {
169
-
170
- @ Bean
171
- public WebHandler webHandler () {
172
- return exchange -> Mono .empty ();
173
- }
174
163
175
- @ Bean
176
- public AtomicLong decorator1NanoTime () {
177
- return new AtomicLong ();
178
- }
164
+ @ Configuration
165
+ static class HttpHandlerDecoratorFactoryBeansConfig {
179
166
180
167
@ Bean
181
168
@ Order (1 )
182
- public HttpHandlerDecoratorFactory decorator1 () {
183
- return handler -> {
184
- decorator1NanoTime ().set (System .nanoTime ());
185
- return handler ;
169
+ public HttpHandlerDecoratorFactory decoratorFactoryA () {
170
+ return delegate -> ( HttpHandler ) ( request , response ) -> {
171
+ response . getHeaders ().set ("decoratorA" , String . valueOf ( System .nanoTime () ));
172
+ return delegate . handle ( request , response ) ;
186
173
};
187
174
}
188
175
189
- @ Bean
190
- public AtomicLong decorator2NanoTime () {
191
- return new AtomicLong ();
192
- }
193
-
194
176
@ Bean
195
177
@ Order (3 )
196
- public HttpHandlerDecoratorFactory decorator2 () {
197
- return handler -> {
198
- decorator2NanoTime ().set (System .nanoTime ());
199
- return handler ;
178
+ public HttpHandlerDecoratorFactory decoratorFactoryB () {
179
+ return delegate -> ( HttpHandler ) ( request , response ) -> {
180
+ response . getHeaders ().set ("decoratorB" , String . valueOf ( System .nanoTime () ));
181
+ return delegate . handle ( request , response ) ;
200
182
};
201
183
}
202
184
203
- @ Bean
204
- public AtomicLong decorator3NanoTime () {
205
- return new AtomicLong ();
206
- }
207
-
208
185
@ Bean
209
186
@ Order (2 )
210
- public HttpHandlerDecoratorFactory decorator3 () {
211
- return handler -> {
212
- decorator3NanoTime ().set (System .nanoTime ());
213
- return handler ;
187
+ public HttpHandlerDecoratorFactory decoratorFactoryC () {
188
+ return delegate -> ( HttpHandler ) ( request , response ) -> {
189
+ response . getHeaders ().set ("decoratorC" , String . valueOf ( System .nanoTime () ));
190
+ return delegate . handle ( request , response ) ;
214
191
};
215
192
}
216
193
194
+ @ Bean
195
+ public WebHandler webHandler () {
196
+ return exchange -> Mono .empty ();
197
+ }
217
198
}
218
199
219
200
0 commit comments