package com.elopteryx.paint.upload;

import com.elopteryx.paint.upload.impl.AsyncUploadParser;
import com.elopteryx.paint.upload.impl.BlockingUploadParser;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Mockito.*;
import static com.elopteryx.paint.upload.FunctionSupplier.*;
import static com.elopteryx.paint.upload.Servlets.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class UploadParserTest {

    @Test
    public void createAsyncParser() throws Exception {
        HttpServletRequest request = newRequest();
        HttpServletResponse response = newResponse();

        when(request.isAsyncSupported()).thenReturn(true);

        UploadParser asyncParser = UploadParser.newParser(request, response);
        assertThat(asyncParser, instanceOf(AsyncUploadParser.class));
    }

    @Test
    public void createBlockingParser() throws Exception {
        HttpServletRequest request = newRequest();
        HttpServletResponse response = newResponse();
        
        when(request.isAsyncSupported()).thenReturn(false);

        UploadParser blockingParser = UploadParser.newParser(request, response);
        assertThat(blockingParser, instanceOf(BlockingUploadParser.class));
    }

    @Test
    public void useTheFullApi() throws Exception {
        HttpServletRequest request = newRequest();
        HttpServletResponse response = newResponse();

        when(request.startAsync()).thenReturn(new MockAsyncContext(request, response));

        UploadParser.newParser(request, response)
                .onPartBegin(partBeginCallback())
                .onPartEnd(partEndCallback())
                .onRequestComplete(requestCallback())
                .onError(errorCallback())
                .sizeThreshold(1024 * 1024 * 10)
                .maxPartSize(1024 * 1024 * 50)
                .maxRequestSize(1024 * 1024 * 50)
                .setup();
    }

}

@@ -173,16 +173,55 @@
 .Test;%0A%0A
+import javax.servlet.ServletException;%0A
 import j
@@ -581,37 +581,572 @@
 Test
-%0A    public void createAsyncP
+(expected = ServletException.class)%0A    public void valid_and_invalid_content_type() throws Exception %7B%0A        HttpServletRequest request = newRequest();%0A        HttpServletResponse response = newResponse();%0A%0A        when(request.getContentType()).thenReturn(%22multipart/%22);%0A        assertTrue(UploadParser.isMultipart(request));%0A%0A        when(request.getContentType()).thenReturn(%22text/plain;charset=UTF-8%22);%0A        assertFalse(UploadParser.isMultipart(request));%0A        UploadParser.newParser(request, response);%0A    %7D%0A%0A    @Test%0A    public void create_async_p
 arse
@@ -1525,17 +1525,19 @@
 eate
-B
+_b
 locking
-P
+_p
 arse
@@ -1931,16 +1931,19 @@
  use
-TheFullA
+_the_full_a
 pi()

Test added for content type detection.