|
18 | 18 |
|
19 | 19 | import java.io.ByteArrayInputStream; |
20 | 20 | import java.io.File; |
| 21 | +import java.io.FileInputStream; |
| 22 | +import java.io.FileNotFoundException; |
21 | 23 | import java.net.URI; |
22 | 24 | import java.util.Arrays; |
23 | 25 | import java.util.Collection; |
@@ -198,25 +200,105 @@ public void objectBody() { |
198 | 200 | } |
199 | 201 |
|
200 | 202 | @Test |
201 | | -public void inputStreamBodyIsNotSupported() { |
| 203 | +public void byteArrayInputStreamBody() { |
202 | 204 | RequestSpecification requestSpec = RestAssured.given() |
203 | 205 | .body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })) |
204 | 206 | .port(this.port); |
205 | 207 | requestSpec.post(); |
206 | | -this.thrown.expectMessage( |
207 | | -equalTo("Unsupported request content: java.io.ByteArrayInputStream")); |
208 | | -this.factory.convert((FilterableRequestSpecification) requestSpec); |
| 208 | +OperationRequest request = this.factory |
| 209 | +.convert((FilterableRequestSpecification) requestSpec); |
| 210 | +assertThat(request.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 }))); |
209 | 211 | } |
210 | 212 |
|
211 | 213 | @Test |
212 | | -public void fileBodyIsNotSupported() { |
| 214 | +public void fileBody() { |
213 | 215 | RequestSpecification requestSpec = RestAssured.given() |
214 | 216 | .body(new File("src/test/resources/body.txt")).port(this.port); |
215 | 217 | requestSpec.post(); |
216 | | -this.thrown.expectMessage(equalTo("Unsupported request content: java.io.File")); |
| 218 | +OperationRequest request = this.factory |
| 219 | +.convert((FilterableRequestSpecification) requestSpec); |
| 220 | +assertThat(request.getContentAsString(), is(equalTo("file"))); |
| 221 | +} |
| 222 | + |
| 223 | +@Test |
| 224 | +public void fileInputStreamBody() throws FileNotFoundException { |
| 225 | +FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt"); |
| 226 | +RequestSpecification requestSpec = RestAssured.given().body(inputStream) |
| 227 | +.port(this.port); |
| 228 | +requestSpec.post(); |
| 229 | +this.thrown.expect(IllegalStateException.class); |
| 230 | +this.thrown.expectMessage("Cannot read content from input stream " + inputStream |
| 231 | ++ " due to reset() failure"); |
217 | 232 | this.factory.convert((FilterableRequestSpecification) requestSpec); |
218 | 233 | } |
219 | 234 |
|
| 235 | +@Test |
| 236 | +public void multipartWithByteArrayInputStreamBody() { |
| 237 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 238 | +.multiPart("foo", "foo.txt", new ByteArrayInputStream("foo".getBytes())); |
| 239 | +requestSpec.post(); |
| 240 | +OperationRequest request = this.factory |
| 241 | +.convert((FilterableRequestSpecification) requestSpec); |
| 242 | +assertThat(request.getParts().iterator().next().getContentAsString(), |
| 243 | +is(equalTo("foo"))); |
| 244 | +} |
| 245 | + |
| 246 | +@Test |
| 247 | +public void multipartWithStringBody() { |
| 248 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 249 | +.multiPart("control", "foo"); |
| 250 | +requestSpec.post(); |
| 251 | +OperationRequest request = this.factory |
| 252 | +.convert((FilterableRequestSpecification) requestSpec); |
| 253 | +assertThat(request.getParts().iterator().next().getContentAsString(), |
| 254 | +is(equalTo("foo"))); |
| 255 | +} |
| 256 | + |
| 257 | +@Test |
| 258 | +public void multipartWithByteArrayBody() { |
| 259 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 260 | +.multiPart("control", "file", "foo".getBytes()); |
| 261 | +requestSpec.post(); |
| 262 | +OperationRequest request = this.factory |
| 263 | +.convert((FilterableRequestSpecification) requestSpec); |
| 264 | +assertThat(request.getParts().iterator().next().getContentAsString(), |
| 265 | +is(equalTo("foo"))); |
| 266 | +} |
| 267 | + |
| 268 | +@Test |
| 269 | +public void multipartWithFileBody() { |
| 270 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 271 | +.multiPart(new File("src/test/resources/body.txt")); |
| 272 | +requestSpec.post(); |
| 273 | +OperationRequest request = this.factory |
| 274 | +.convert((FilterableRequestSpecification) requestSpec); |
| 275 | +assertThat(request.getParts().iterator().next().getContentAsString(), |
| 276 | +is(equalTo("file"))); |
| 277 | +} |
| 278 | + |
| 279 | +@Test |
| 280 | +public void multipartWithFileInputStreamBody() throws FileNotFoundException { |
| 281 | +FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt"); |
| 282 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 283 | +.multiPart("foo", "foo.txt", inputStream); |
| 284 | +requestSpec.post(); |
| 285 | +this.thrown.expect(IllegalStateException.class); |
| 286 | +this.thrown.expectMessage("Cannot read content from input stream " + inputStream |
| 287 | ++ " due to reset() failure"); |
| 288 | +this.factory.convert((FilterableRequestSpecification) requestSpec); |
| 289 | +} |
| 290 | + |
| 291 | +@Test |
| 292 | +public void multipartWithObjectBody() { |
| 293 | +RequestSpecification requestSpec = RestAssured.given().port(this.port) |
| 294 | +.multiPart("control", new ObjectBody("bar")); |
| 295 | +requestSpec.post(); |
| 296 | +OperationRequest request = this.factory |
| 297 | +.convert((FilterableRequestSpecification) requestSpec); |
| 298 | +assertThat(request.getParts().iterator().next().getContentAsString(), |
| 299 | +is(equalTo("{\"foo\":\"bar\"}"))); |
| 300 | +} |
| 301 | + |
220 | 302 | /** |
221 | 303 | * Minimal test web application. |
222 | 304 | */ |
|
0 commit comments