AbstractPart implements Part { @Override public void write(OutputStream out) throws IOException { writeContentDisposition(out); writeName(out); writeExtraHeaderIfNotNull(out); writeCRLF(out); writeContentTypeIfNotNull(out); writeCRLF(out); writeContent(out); writeCRLF(out); } AbstractPart(String name); @Override void write(OutputStream out); @Override String getName();  }
@Test public void shouldWriteExpectedOutput() throws IOException { String contentString = "test test test"; byte[] content = contentString.getBytes(); final InputStream in = new ByteArrayInputStream(content); AbstractPart mock = createPartWithInputStream(name, in); ByteArrayOutputStream out = new ByteArrayOutputStream(); Part part = new TestPart(name, mock); String expected = "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + contentString + "\r\n"; part.write(out); byte[] result = out.toByteArray(); assertThat(new String(result), is(expected)); }
@Test public void shouldUseOutputStreamWhenWrite() throws IOException { OutputStream out = mock(OutputStream.class); Part part = new TestPart(name); part.write(out); verify(out, atLeastOnce()).write(Matchers.any(byte[].class)); }
@Test public void shouldCloseInputStream() throws IOException { final TestInputStream in = new TestInputStream(); AbstractPart mock = createPartWithInputStream(name, in); OutputStream out = mock(OutputStream.class); Part part = new TestPart(name, mock); part.write(out); assertThat(in.isClosed(), is(Boolean.TRUE)); }