MultipartFormDataWriter { public void writeItems(Collection<OutputItem> items, OutputStream output, byte[] boundary) throws IOException { for (OutputItem item : items) { writeItem(item, output, boundary); } output.write(HYPHENS); output.write(boundary); output.write(HYPHENS); output.write(NEW_LINE); } MultipartFormDataWriter(Providers providers); void writeItems(Collection<OutputItem> items, OutputStream output, byte[] boundary);  }
@Test public void failsWhenMessageBodyWriterForItemEntityIsNotAvailable() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); OutputItem item1 = anOutputItem().withName("item1").withEntity("item1 entity").withMediaType(TEXT_PLAIN_TYPE).withFilename("item1.txt").build(); thrown.expect(RuntimeException.class); multipartFormDataWriter.writeItems(newArrayList(item1), bOut, "1234567".getBytes()); }
@Test public void writesItemsOfMultipartContentToOutputStream() throws Exception { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); MessageBodyWriter<String> messageBodyWriter = mockStringMessageBodyWriter(bOut); when(providers.getMessageBodyWriter(eq(String.class), any(), any(Annotation[].class), any(MediaType.class))).thenReturn(messageBodyWriter); OutputItem item1 = anOutputItem().withName("item1").withEntity("item1 entity").withMediaType(TEXT_PLAIN_TYPE).withFilename("item1.txt").build(); OutputItem item2 = anOutputItem().withName("item2").withEntity("{\"item2\":\"entity\"}").withMediaType(APPLICATION_JSON_TYPE).withFilename("item2.json").build(); multipartFormDataWriter.writeItems(newArrayList(item1, item2), bOut, "1234567".getBytes()); String expectedContent = "--1234567\r\n" + "Content-Disposition: form-data; name=\"item1\"; filename=\"item1.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "item1 entity\r\n" + "--1234567\r\n" + "Content-Disposition: form-data; name=\"item2\"; filename=\"item2.json\"\r\n" + "Content-Type: application/json\r\n" + "\r\n" + "{\"item2\":\"entity\"}\r\n" + "--1234567--\r\n"; assertEquals(expectedContent, bOut.toString()); }