17
17
package org .springframework .boot .autoconfigure .web .embedded ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .Map ;
20
21
22
+ import io .netty .bootstrap .ServerBootstrap ;
23
+ import io .netty .channel .ChannelOption ;
21
24
import org .junit .Before ;
22
25
import org .junit .Test ;
26
+ import org .mockito .ArgumentCaptor ;
27
+ import org .mockito .Captor ;
28
+ import org .mockito .MockitoAnnotations ;
29
+ import reactor .netty .http .server .HttpServer ;
30
+ import reactor .netty .tcp .TcpServer ;
23
31
24
32
import org .springframework .boot .autoconfigure .web .ServerProperties ;
25
33
import org .springframework .boot .context .properties .source .ConfigurationPropertySources ;
26
34
import org .springframework .boot .web .embedded .netty .NettyReactiveWebServerFactory ;
27
35
import org .springframework .boot .web .embedded .netty .NettyServerCustomizer ;
28
36
import org .springframework .mock .env .MockEnvironment ;
37
+ import org .springframework .test .util .ReflectionTestUtils ;
29
38
30
- import static org .mockito .Mockito .any ;
39
+ import static org .assertj .core .api .Assertions .assertThat ;
40
+ import static org .mockito .ArgumentMatchers .any ;
31
41
import static org .mockito .Mockito .mock ;
42
+ import static org .mockito .Mockito .never ;
32
43
import static org .mockito .Mockito .times ;
33
44
import static org .mockito .Mockito .verify ;
34
45
@@ -46,20 +57,18 @@ public class NettyWebServerFactoryCustomizerTests {
46
57
47
58
private NettyWebServerFactoryCustomizer customizer ;
48
59
60
+ @ Captor
61
+ private ArgumentCaptor <NettyServerCustomizer > customizerCaptor ;
62
+
49
63
@ Before
50
64
public void setup () {
65
+ MockitoAnnotations .initMocks (this );
51
66
this .environment = new MockEnvironment ();
52
67
this .serverProperties = new ServerProperties ();
53
68
ConfigurationPropertySources .attach (this .environment );
54
69
this .customizer = new NettyWebServerFactoryCustomizer (this .environment , this .serverProperties );
55
70
}
56
71
57
- private void clear () {
58
- this .serverProperties .setUseForwardHeaders (null );
59
- this .serverProperties .setMaxHttpHeaderSize (null );
60
- this .serverProperties .setConnectionTimeout (null );
61
- }
62
-
63
72
@ Test
64
73
public void deduceUseForwardHeaders () {
65
74
this .environment .setProperty ("DYNO" , "-" );
@@ -85,22 +94,47 @@ public void setUseForwardHeaders() {
85
94
86
95
@ Test
87
96
public void setConnectionTimeoutAsZero () {
88
- clear ();
89
- this .serverProperties .setConnectionTimeout (Duration .ZERO );
90
-
97
+ setupConnectionTimeout (Duration .ZERO );
91
98
NettyReactiveWebServerFactory factory = mock (NettyReactiveWebServerFactory .class );
92
99
this .customizer .customize (factory );
93
- verify (factory , times ( 0 )). addServerCustomizers ( any ( NettyServerCustomizer . class ) );
100
+ verifyConnectionTimeout (factory , null );
94
101
}
95
102
96
103
@ Test
97
104
public void setConnectionTimeoutAsMinusOne () {
98
- clear ();
99
- this .serverProperties .setConnectionTimeout (Duration .ofNanos (-1 ));
105
+ setupConnectionTimeout (Duration .ofNanos (-1 ));
106
+ NettyReactiveWebServerFactory factory = mock (NettyReactiveWebServerFactory .class );
107
+ this .customizer .customize (factory );
108
+ verifyConnectionTimeout (factory , 0 );
109
+ }
100
110
111
+ @ Test
112
+ public void setConnectionTimeout () {
113
+ setupConnectionTimeout (Duration .ofSeconds (1 ));
101
114
NettyReactiveWebServerFactory factory = mock (NettyReactiveWebServerFactory .class );
102
115
this .customizer .customize (factory );
103
- verify (factory , times (1 )).addServerCustomizers (any (NettyServerCustomizer .class ));
116
+ verifyConnectionTimeout (factory , 1000 );
117
+ }
118
+
119
+ @ SuppressWarnings ("unchecked" )
120
+ private void verifyConnectionTimeout (NettyReactiveWebServerFactory factory , Integer expected ) {
121
+ if (expected == null ) {
122
+ verify (factory , never ()).addServerCustomizers (any (NettyServerCustomizer .class ));
123
+ return ;
124
+ }
125
+ verify (factory , times (1 )).addServerCustomizers (this .customizerCaptor .capture ());
126
+ NettyServerCustomizer serverCustomizer = this .customizerCaptor .getValue ();
127
+ HttpServer httpServer = serverCustomizer .apply (HttpServer .create ());
128
+ TcpServer tcpConfiguration = ReflectionTestUtils .invokeMethod (httpServer , "tcpConfiguration" );
129
+ ServerBootstrap bootstrap = tcpConfiguration .configure ();
130
+ Map <Object , Object > options = (Map <Object , Object >) ReflectionTestUtils .getField (bootstrap , "options" );
131
+ assertThat (options ).containsEntry (ChannelOption .CONNECT_TIMEOUT_MILLIS , expected );
132
+ }
133
+
134
+ private void setupConnectionTimeout (Duration connectionTimeout ) {
135
+ this .serverProperties .setUseForwardHeaders (null );
136
+ this .serverProperties .setMaxHttpHeaderSize (null );
137
+ this .serverProperties .setConnectionTimeout (connectionTimeout );
104
138
}
105
139
106
140
}
0 commit comments