MultipartRequestHandler { public void handle() throws IOException, MalformedInputException { if (wasHandledBefore) { throw new IllegalStateException("Handle method was not expected to be called more than once"); } wasHandledBefore = true; skipToTheFirstPart(); handleBody(); } MultipartRequestHandler(final Parser<MultipartHeadersPart> multipartHeadersPartParser,
                                   final InputStream in, final int expectedPostLength,
                                   final String boundary, final String temporaryUploadsDirectory,
                                   final int bufferLength); void handle(); Map<String, String> getPost(); Collection<UploadedFile> getUploadedFiles();  }
@Test(expected = IllegalStateException.class) public void shouldBeHandledOnceOnly() throws MalformedInputException { String data = new MultipartInputBuilder(BOUNDARY) .withField("field_1", "A123") .withField("field_2", "B123") .withField("field_3", "C123") .build(); MultipartRequestHandler mrh = new MultipartRequestHandler(PARSER, getStreamOutOfString(data), data.length(), BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); try { mrh.handle(); mrh.handle(); } catch (IOException e) { fail("Should not throw IOException: " + e.getMessage()); } }
@Test(expected = IOException.class) public void shouldThrowIOExceptionOnEndedBoundary() throws MalformedInputException, IOException { String data = "--123"; MultipartRequestHandler mrh = new MultipartRequestHandler(PARSER, getStreamOutOfString(data), data.length() * 2, BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); mrh.handle(); }
@Test(expected = PayloadTooLargeProtocolException.class) public void shouldStopParsingOnWrongContentLengthInBeforeBoundary() throws MalformedInputException { String data = "--------------------------------------------------------" + new MultipartInputBuilder(BOUNDARY) .withField("field_1", "A123") .build(); MultipartRequestHandler mrh = new MultipartRequestHandler(PARSER, getStreamOutOfString(data), 5, BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); try { mrh.handle(); } catch (IOException e) { fail("Should not throw IOException: " + e.getMessage()); } }
@Test(expected = PayloadTooLargeProtocolException.class) public void shouldStopParsingOnWrongContentLengthInBody() throws MalformedInputException { String begin = new MultipartInputBuilder(BOUNDARY) .withField("field_1", "A123") .build(); String data = begin + new MultipartInputBuilder(BOUNDARY) .withField("field_2", "B123") .withField("field_3", "C123") .build(); MultipartRequestHandler mrh = new MultipartRequestHandler(PARSER, getStreamOutOfString(data), begin.length(), BOUNDARY, TEMPORARY_UPLOADS_DIRECTORY, 2048); try { mrh.handle(); } catch (IOException e) { fail("Should not throw IOException: " + e.getMessage()); } }