1
1
/*
2
- * Copyright 2014-2016 the original author or authors.
2
+ * Copyright 2014-2017 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.
28
28
import com .jayway .restassured .RestAssured ;
29
29
import com .jayway .restassured .specification .FilterableRequestSpecification ;
30
30
import com .jayway .restassured .specification .RequestSpecification ;
31
+ import org .junit .ClassRule ;
31
32
import org .junit .Rule ;
32
33
import org .junit .Test ;
33
34
import org .junit .rules .ExpectedException ;
34
- import org .junit .runner .RunWith ;
35
35
36
- import org .springframework .beans .factory .annotation .Value ;
37
- import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
38
- import org .springframework .boot .test .IntegrationTest ;
39
- import org .springframework .boot .test .SpringApplicationConfiguration ;
40
- import org .springframework .context .annotation .Configuration ;
41
36
import org .springframework .http .HttpMethod ;
42
37
import org .springframework .http .MediaType ;
43
38
import org .springframework .restdocs .operation .OperationRequest ;
44
39
import org .springframework .restdocs .operation .OperationRequestPart ;
45
- import org .springframework .restdocs .restassured .RestAssuredRequestConverterTests .TestApplication ;
46
- import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
47
- import org .springframework .test .context .web .WebAppConfiguration ;
48
- import org .springframework .web .bind .annotation .RequestMapping ;
49
- import org .springframework .web .bind .annotation .RestController ;
50
40
51
41
import static org .hamcrest .CoreMatchers .equalTo ;
52
42
import static org .hamcrest .CoreMatchers .is ;
57
47
*
58
48
* @author Andy Wilkinson
59
49
*/
60
- @ RunWith (SpringJUnit4ClassRunner .class )
61
- @ SpringApplicationConfiguration (classes = TestApplication .class )
62
- @ WebAppConfiguration
63
- @ IntegrationTest ("server.port=0" )
64
50
public class RestAssuredRequestConverterTests {
65
51
52
+ @ ClassRule
53
+ public static TomcatServer tomcat = new TomcatServer ();
54
+
66
55
@ Rule
67
56
public final ExpectedException thrown = ExpectedException .none ();
68
57
69
58
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter ();
70
59
71
- @ Value ("${local.server.port}" )
72
- private int port ;
73
-
74
60
@ Test
75
61
public void requestUri () {
76
- RequestSpecification requestSpec = RestAssured .given ().port (this . port );
62
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () );
77
63
requestSpec .get ("/foo/bar" );
78
64
OperationRequest request = this .factory
79
65
.convert ((FilterableRequestSpecification ) requestSpec );
80
- assertThat (request .getUri (),
81
- is ( equalTo ( URI .create ("http://localhost:" + this . port + "/foo/bar" ))));
66
+ assertThat (request .getUri (), is ( equalTo (
67
+ URI .create ("http://localhost:" + tomcat . getPort () + "/foo/bar" ))));
82
68
}
83
69
84
70
@ Test
85
71
public void requestMethod () {
86
- RequestSpecification requestSpec = RestAssured .given ().port (this . port );
72
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () );
87
73
requestSpec .head ("/foo/bar" );
88
74
OperationRequest request = this .factory
89
75
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -92,7 +78,7 @@ public void requestMethod() {
92
78
93
79
@ Test
94
80
public void queryStringParameters () {
95
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
81
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
96
82
.queryParam ("foo" , "bar" );
97
83
requestSpec .get ("/" );
98
84
OperationRequest request = this .factory
@@ -103,7 +89,7 @@ public void queryStringParameters() {
103
89
104
90
@ Test
105
91
public void queryStringFromUrlParameters () {
106
- RequestSpecification requestSpec = RestAssured .given ().port (this . port );
92
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () );
107
93
requestSpec .get ("/?foo=bar" );
108
94
OperationRequest request = this .factory
109
95
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -113,7 +99,7 @@ public void queryStringFromUrlParameters() {
113
99
114
100
@ Test
115
101
public void formParameters () {
116
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
102
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
117
103
.formParameter ("foo" , "bar" );
118
104
requestSpec .get ("/" );
119
105
OperationRequest request = this .factory
@@ -124,7 +110,7 @@ public void formParameters() {
124
110
125
111
@ Test
126
112
public void requestParameters () {
127
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
113
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
128
114
.parameter ("foo" , "bar" );
129
115
requestSpec .get ("/" );
130
116
OperationRequest request = this .factory
@@ -135,7 +121,7 @@ public void requestParameters() {
135
121
136
122
@ Test
137
123
public void headers () {
138
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
124
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
139
125
.header ("Foo" , "bar" );
140
126
requestSpec .get ("/" );
141
127
OperationRequest request = this .factory
@@ -144,15 +130,15 @@ public void headers() {
144
130
assertThat (request .getHeaders ().get ("Foo" ), is (equalTo (Arrays .asList ("bar" ))));
145
131
assertThat (request .getHeaders ().get ("Accept" ), is (equalTo (Arrays .asList ("*/*" ))));
146
132
assertThat (request .getHeaders ().get ("Host" ),
147
- is (equalTo (Arrays .asList ("localhost:" + this . port ))));
133
+ is (equalTo (Arrays .asList ("localhost:" + tomcat . getPort () ))));
148
134
}
149
135
150
136
@ Test
151
137
public void multipart () {
152
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
138
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
153
139
.multiPart ("a" , "a.txt" , "alpha" , null )
154
140
.multiPart ("b" , new ObjectBody ("bar" ), "application/json" );
155
- requestSpec .post (). then (). statusCode ( 200 ) ;
141
+ requestSpec .post ();
156
142
OperationRequest request = this .factory
157
143
.convert ((FilterableRequestSpecification ) requestSpec );
158
144
Collection <OperationRequestPart > parts = request .getParts ();
@@ -174,15 +160,15 @@ public void multipart() {
174
160
@ Test
175
161
public void byteArrayBody () {
176
162
RequestSpecification requestSpec = RestAssured .given ().body ("body" .getBytes ())
177
- .port (this . port );
163
+ .port (tomcat . getPort () );
178
164
requestSpec .post ();
179
165
this .factory .convert ((FilterableRequestSpecification ) requestSpec );
180
166
}
181
167
182
168
@ Test
183
169
public void stringBody () {
184
170
RequestSpecification requestSpec = RestAssured .given ().body ("body" )
185
- .port (this . port );
171
+ .port (tomcat . getPort () );
186
172
requestSpec .post ();
187
173
OperationRequest request = this .factory
188
174
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -192,7 +178,7 @@ public void stringBody() {
192
178
@ Test
193
179
public void objectBody () {
194
180
RequestSpecification requestSpec = RestAssured .given ().body (new ObjectBody ("bar" ))
195
- .port (this . port );
181
+ .port (tomcat . getPort () );
196
182
requestSpec .post ();
197
183
OperationRequest request = this .factory
198
184
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -203,7 +189,7 @@ public void objectBody() {
203
189
public void byteArrayInputStreamBody () {
204
190
RequestSpecification requestSpec = RestAssured .given ()
205
191
.body (new ByteArrayInputStream (new byte [] { 1 , 2 , 3 , 4 }))
206
- .port (this . port );
192
+ .port (tomcat . getPort () );
207
193
requestSpec .post ();
208
194
OperationRequest request = this .factory
209
195
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -213,7 +199,7 @@ public void byteArrayInputStreamBody() {
213
199
@ Test
214
200
public void fileBody () {
215
201
RequestSpecification requestSpec = RestAssured .given ()
216
- .body (new File ("src/test/resources/body.txt" )).port (this . port );
202
+ .body (new File ("src/test/resources/body.txt" )).port (tomcat . getPort () );
217
203
requestSpec .post ();
218
204
OperationRequest request = this .factory
219
205
.convert ((FilterableRequestSpecification ) requestSpec );
@@ -224,7 +210,7 @@ public void fileBody() {
224
210
public void fileInputStreamBody () throws FileNotFoundException {
225
211
FileInputStream inputStream = new FileInputStream ("src/test/resources/body.txt" );
226
212
RequestSpecification requestSpec = RestAssured .given ().body (inputStream )
227
- .port (this . port );
213
+ .port (tomcat . getPort () );
228
214
requestSpec .post ();
229
215
this .thrown .expect (IllegalStateException .class );
230
216
this .thrown .expectMessage ("Cannot read content from input stream " + inputStream
@@ -234,7 +220,7 @@ public void fileInputStreamBody() throws FileNotFoundException {
234
220
235
221
@ Test
236
222
public void multipartWithByteArrayInputStreamBody () {
237
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
223
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
238
224
.multiPart ("foo" , "foo.txt" , new ByteArrayInputStream ("foo" .getBytes ()));
239
225
requestSpec .post ();
240
226
OperationRequest request = this .factory
@@ -245,7 +231,7 @@ public void multipartWithByteArrayInputStreamBody() {
245
231
246
232
@ Test
247
233
public void multipartWithStringBody () {
248
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
234
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
249
235
.multiPart ("control" , "foo" );
250
236
requestSpec .post ();
251
237
OperationRequest request = this .factory
@@ -256,7 +242,7 @@ public void multipartWithStringBody() {
256
242
257
243
@ Test
258
244
public void multipartWithByteArrayBody () {
259
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
245
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
260
246
.multiPart ("control" , "file" , "foo" .getBytes ());
261
247
requestSpec .post ();
262
248
OperationRequest request = this .factory
@@ -267,7 +253,7 @@ public void multipartWithByteArrayBody() {
267
253
268
254
@ Test
269
255
public void multipartWithFileBody () {
270
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
256
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
271
257
.multiPart (new File ("src/test/resources/body.txt" ));
272
258
requestSpec .post ();
273
259
OperationRequest request = this .factory
@@ -279,7 +265,7 @@ public void multipartWithFileBody() {
279
265
@ Test
280
266
public void multipartWithFileInputStreamBody () throws FileNotFoundException {
281
267
FileInputStream inputStream = new FileInputStream ("src/test/resources/body.txt" );
282
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
268
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
283
269
.multiPart ("foo" , "foo.txt" , inputStream );
284
270
requestSpec .post ();
285
271
this .thrown .expect (IllegalStateException .class );
@@ -290,7 +276,7 @@ public void multipartWithFileInputStreamBody() throws FileNotFoundException {
290
276
291
277
@ Test
292
278
public void multipartWithObjectBody () {
293
- RequestSpecification requestSpec = RestAssured .given ().port (this . port )
279
+ RequestSpecification requestSpec = RestAssured .given ().port (tomcat . getPort () )
294
280
.multiPart ("control" , new ObjectBody ("bar" ));
295
281
requestSpec .post ();
296
282
OperationRequest request = this .factory
@@ -299,20 +285,6 @@ public void multipartWithObjectBody() {
299
285
is (equalTo ("{\" foo\" :\" bar\" }" )));
300
286
}
301
287
302
- /**
303
- * Minimal test web application.
304
- */
305
- @ Configuration
306
- @ EnableAutoConfiguration
307
- @ RestController
308
- static class TestApplication {
309
-
310
- @ RequestMapping ("/" )
311
- void handle () {
312
- }
313
-
314
- }
315
-
316
288
/**
317
289
* Sample object body to verify JSON serialization.
318
290
*/
0 commit comments