44
44
import org .springframework .http .client .HttpComponentsAsyncClientHttpRequestFactory ;
45
45
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
46
46
import org .springframework .util .FileCopyUtils ;
47
+ import org .springframework .util .SocketUtils ;
47
48
import org .springframework .util .StreamUtils ;
48
49
import org .springframework .util .concurrent .ListenableFuture ;
49
50
50
51
import static org .hamcrest .Matchers .equalTo ;
52
+ import static org .hamcrest .Matchers .lessThan ;
51
53
import static org .hamcrest .Matchers .notNullValue ;
52
54
import static org .junit .Assert .assertEquals ;
53
55
import static org .junit .Assert .assertThat ;
@@ -88,18 +90,17 @@ public void startServlet() throws Exception {
88
90
this .container = factory
89
91
.getEmbeddedServletContainer (exampleServletRegistration ());
90
92
this .container .start ();
91
- assertThat (getResponse ("http://localhost:8080/ hello" ), equalTo ("Hello World" ));
93
+ assertThat (getResponse (getLocalUrl ( "/ hello") ), equalTo ("Hello World" ));
92
94
}
93
95
94
96
@ Test
95
- public void emptyServerWhenPortIsZero () throws Exception {
97
+ public void emptyServerWhenPortIsMinusOne () throws Exception {
96
98
AbstractEmbeddedServletContainerFactory factory = getFactory ();
97
- factory .setPort (0 );
99
+ factory .setPort (- 1 );
98
100
this .container = factory
99
101
.getEmbeddedServletContainer (exampleServletRegistration ());
100
102
this .container .start ();
101
- this .thrown .expect (IOException .class );
102
- getResponse ("http://localhost:8080/hello" );
103
+ assertThat (this .container .getPort (), lessThan (0 )); // Jetty is -2
103
104
}
104
105
105
106
@ Test
@@ -110,7 +111,7 @@ public void stopServlet() throws Exception {
110
111
this .container .start ();
111
112
this .container .stop ();
112
113
this .thrown .expect (IOException .class );
113
- getResponse ("http://localhost:8080/ hello" );
114
+ getResponse (getLocalUrl ( "/ hello") );
114
115
}
115
116
116
117
@ Test
@@ -121,8 +122,8 @@ public void restartWithKeepAlive() throws Exception {
121
122
this .container .start ();
122
123
HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory ();
123
124
ListenableFuture <ClientHttpResponse > response1 = clientHttpRequestFactory
124
- .createAsyncRequest (new URI ("http://localhost:8080/ hello" ),
125
- HttpMethod . GET ) .executeAsync ();
125
+ .createAsyncRequest (new URI (getLocalUrl ( "/ hello" )), HttpMethod . GET )
126
+ .executeAsync ();
126
127
assertThat (response1 .get (10 , TimeUnit .SECONDS ).getRawStatusCode (), equalTo (200 ));
127
128
128
129
this .container .stop ();
@@ -131,8 +132,8 @@ public void restartWithKeepAlive() throws Exception {
131
132
this .container .start ();
132
133
133
134
ListenableFuture <ClientHttpResponse > response2 = clientHttpRequestFactory
134
- .createAsyncRequest (new URI ("http://localhost:8080/ hello" ),
135
- HttpMethod . GET ) .executeAsync ();
135
+ .createAsyncRequest (new URI (getLocalUrl ( "/ hello" )), HttpMethod . GET )
136
+ .executeAsync ();
136
137
assertThat (response2 .get (10 , TimeUnit .SECONDS ).getRawStatusCode (), equalTo (200 ));
137
138
}
138
139
@@ -143,7 +144,7 @@ public void startServletAndFilter() throws Exception {
143
144
exampleServletRegistration (), new FilterRegistrationBean (
144
145
new ExampleFilter ()));
145
146
this .container .start ();
146
- assertThat (getResponse ("http://localhost:8080/ hello" ), equalTo ("[Hello World]" ));
147
+ assertThat (getResponse (getLocalUrl ( "/ hello") ), equalTo ("[Hello World]" ));
147
148
}
148
149
149
150
@ Test
@@ -188,12 +189,14 @@ public void onStartup(ServletContext servletContext)
188
189
@ Test
189
190
public void specificPort () throws Exception {
190
191
AbstractEmbeddedServletContainerFactory factory = getFactory ();
191
- factory .setPort (8081 );
192
+ int specificPort = SocketUtils .findAvailableTcpPort (40000 );
193
+ factory .setPort (specificPort );
192
194
this .container = factory
193
195
.getEmbeddedServletContainer (exampleServletRegistration ());
194
196
this .container .start ();
195
- assertThat (getResponse ("http://localhost:8081/hello" ), equalTo ("Hello World" ));
196
- assertEquals (8081 , this .container .getPort ());
197
+ assertThat (getResponse ("http://localhost:" + specificPort + "/hello" ),
198
+ equalTo ("Hello World" ));
199
+ assertEquals (specificPort , this .container .getPort ());
197
200
}
198
201
199
202
@ Test
@@ -203,7 +206,7 @@ public void specificContextRoot() throws Exception {
203
206
this .container = factory
204
207
.getEmbeddedServletContainer (exampleServletRegistration ());
205
208
this .container .start ();
206
- assertThat (getResponse ("http://localhost:8080/ say/hello" ), equalTo ("Hello World" ));
209
+ assertThat (getResponse (getLocalUrl ( "/ say/hello") ), equalTo ("Hello World" ));
207
210
}
208
211
209
212
@ Test
@@ -264,7 +267,7 @@ public void documentRoot() throws Exception {
264
267
factory .setDocumentRoot (this .temporaryFolder .getRoot ());
265
268
this .container = factory .getEmbeddedServletContainer ();
266
269
this .container .start ();
267
- assertThat (getResponse ("http://localhost:8080/ test.txt" ), equalTo ("test" ));
270
+ assertThat (getResponse (getLocalUrl ( "/ test.txt") ), equalTo ("test" ));
268
271
}
269
272
270
273
@ Test
@@ -278,7 +281,7 @@ public void mimeType() throws Exception {
278
281
factory .setMimeMappings (mimeMappings );
279
282
this .container = factory .getEmbeddedServletContainer ();
280
283
this .container .start ();
281
- ClientHttpResponse response = getClientResponse ("http://localhost:8080/ test.xxcss" );
284
+ ClientHttpResponse response = getClientResponse (getLocalUrl ( "/ test.xxcss") );
282
285
assertThat (response .getHeaders ().getContentType ().toString (), equalTo ("text/css" ));
283
286
response .close ();
284
287
}
@@ -290,8 +293,12 @@ public void errorPage() throws Exception {
290
293
this .container = factory .getEmbeddedServletContainer (
291
294
exampleServletRegistration (), errorServletRegistration ());
292
295
this .container .start ();
293
- assertThat (getResponse ("http://localhost:8080/hello" ), equalTo ("Hello World" ));
294
- assertThat (getResponse ("http://localhost:8080/bang" ), equalTo ("Hello World" ));
296
+ assertThat (getResponse (getLocalUrl ("/hello" )), equalTo ("Hello World" ));
297
+ assertThat (getResponse (getLocalUrl ("/bang" )), equalTo ("Hello World" ));
298
+ }
299
+
300
+ protected String getLocalUrl (String resourcePath ) {
301
+ return "http://localhost:" + this .container .getPort () + resourcePath ;
295
302
}
296
303
297
304
protected String getResponse (String url ) throws IOException , URISyntaxException {
0 commit comments