11/*
2- * Copyright 2002-2020 the original author or authors.
2+ * Copyright 2002-2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .web .server .adapter ;
1818
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+
1925import org .junit .jupiter .api .Test ;
26+ import reactor .core .publisher .Flux ;
27+ import reactor .core .publisher .Mono ;
28+
2029import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2130import org .springframework .context .annotation .Bean ;
2231import org .springframework .context .annotation .Configuration ;
3342import org .springframework .web .server .WebHandler ;
3443import org .springframework .web .testfixture .http .server .reactive .MockServerHttpRequest ;
3544import 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 ;
4445
4546import static java .time .Duration .ofMillis ;
4647import static org .assertj .core .api .Assertions .assertThat ;
@@ -141,21 +142,16 @@ void httpHandlerDecorator() {
141142 }
142143
143144 @ Test
144- void httpHandlerDecoratorBeans () {
145+ void httpHandlerDecoratorFactoryBeans () {
146+ HttpHandler handler = WebHttpHandlerBuilder .applicationContext (
147+ new AnnotationConfigApplicationContext (HttpHandlerDecoratorFactoryBeansConfig .class )).build ();
145148
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 ();
158151
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" ));
159155 }
160156
161157 private static Mono <Void > writeToResponse (ServerWebExchange exchange , String value ) {
@@ -164,56 +160,41 @@ private static Mono<Void> writeToResponse(ServerWebExchange exchange, String val
164160 return exchange .getResponse ().writeWith (Flux .just (buffer ));
165161 }
166162
167- @ Configuration
168- static class HttpHandlerDecoratorBeansConfig {
169-
170- @ Bean
171- public WebHandler webHandler () {
172- return exchange -> Mono .empty ();
173- }
174163
175- @ Bean
176- public AtomicLong decorator1NanoTime () {
177- return new AtomicLong ();
178- }
164+ @ Configuration
165+ static class HttpHandlerDecoratorFactoryBeansConfig {
179166
180167 @ Bean
181168 @ 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 ) ;
186173 };
187174 }
188175
189- @ Bean
190- public AtomicLong decorator2NanoTime () {
191- return new AtomicLong ();
192- }
193-
194176 @ Bean
195177 @ 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 ) ;
200182 };
201183 }
202184
203- @ Bean
204- public AtomicLong decorator3NanoTime () {
205- return new AtomicLong ();
206- }
207-
208185 @ Bean
209186 @ 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 ) ;
214191 };
215192 }
216193
194+ @ Bean
195+ public WebHandler webHandler () {
196+ return exchange -> Mono .empty ();
197+ }
217198 }
218199
219200
0 commit comments