public Request bodyFile(final File file, final ContentType contentType) { return body(new FileEntity(file, contentType)); }
public Request bodyFile(final File file, final ContentType contentType) { return body(new FileEntity(file, contentType)); }
@Test public void testIllegalConstructor() throws Exception { try { new FileEntity(null, ContentType.TEXT_PLAIN); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testWriteTo() throws Exception { final File tmpfile = File.createTempFile("testfile", ".txt"); tmpfile.deleteOnExit(); final FileOutputStream outStream = new FileOutputStream(tmpfile); outStream.write(0); outStream.write(1); outStream.write(2); outStream.write(3); outStream.close(); final FileEntity httpentity = new FileEntity(tmpfile, ContentType.TEXT_PLAIN); final ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); final byte[] bytes = out.toByteArray(); Assert.assertNotNull(bytes); Assert.assertEquals(tmpfile.length(), bytes.length); for (int i = 0; i < 4; i++) { Assert.assertEquals(i, bytes[i]); } if (!tmpfile.delete()){ Assert.fail("Failed to delete: "+tmpfile); } try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testBasics() throws Exception { final File tmpfile = File.createTempFile("testfile", ".txt"); tmpfile.deleteOnExit(); final FileEntity httpentity = new FileEntity(tmpfile, ContentType.TEXT_PLAIN); Assert.assertEquals(tmpfile.length(), httpentity.getContentLength()); final InputStream content = httpentity.getContent(); Assert.assertNotNull(content); content.close(); Assert.assertTrue(httpentity.isRepeatable()); Assert.assertFalse(httpentity.isStreaming()); if (!tmpfile.delete()){ Assert.fail("Failed to delete: "+tmpfile); } }
e.setContentType(ContentType.DEFAULT_BINARY.toString()); } else if (this.file != null) { e = new FileEntity(this.file, getContentOrDefault(ContentType.DEFAULT_BINARY)); } else { e = new BasicHttpEntity();