/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.jboss.netty.handler.codec.http.multipart;

import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.*;
import org.jboss.netty.util.CharsetUtil;
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

/** {@link HttpPostRequestDecoder} test case. */
public class HttpPostRequestDecoderTest {
    @Test
    public void testBinaryStreamUpload() throws Exception {
        final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";

        final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");

        req.setContent(ChannelBuffers.EMPTY_BUFFER);
        req.setHeader(HttpHeaders.Names.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
        req.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
        req.setChunked(true);

        // Force to use memory-based data.
        final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);

        for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
            final String body = ""
                + "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" +
                "Content-Type: image/gif\r\n" +
                "\r\n" +
                data + "\r\n" +
                "--" + boundary + "--\r\n";

            // Create decoder instance to test.
            final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);

            decoder.offer(new DefaultHttpChunk(ChannelBuffers.copiedBuffer(body, CharsetUtil.UTF_8)));
            decoder.offer(new DefaultHttpChunk(ChannelBuffers.EMPTY_BUFFER));

            // Validate it's enough chunks to decode upload.
            assertTrue(decoder.hasNext());

            // Decode binary upload.
            MemoryFileUpload upload = (MemoryFileUpload) decoder.next();

            // Validate data has been parsed correctly as it was passed into request.
            assertArrayEquals("Invalid decoded data [data=" + data + "upload=" + upload + ']',
                data.getBytes(), upload.get());
        }
    }
}

@@ -771,17 +771,32 @@
 ec.http.
-*
+DefaultHttpChunk
 ;%0Aimport
@@ -812,16 +812,242 @@
 s.netty.
+handler.codec.http.DefaultHttpRequest;%0Aimport org.jboss.netty.handler.codec.http.HttpHeaders;%0Aimport org.jboss.netty.handler.codec.http.HttpMethod;%0Aimport org.jboss.netty.handler.codec.http.HttpVersion;%0Aimport org.jboss.netty.
 util.Cha
@@ -1106,62 +1106,12 @@
 rray
-s;%0A%0Aimport static org.junit.Assert.assertArrayEqual
 s;%0A
+%0A
 impo
@@ -1137,26 +1137,17 @@
 .Assert.
-assertTrue
+*
 ;%0A%0A/** %7B
@@ -2006,11 +2006,8 @@
 dy =
- %22%22
 %0A   
@@ -2019,17 +2019,19 @@
         
-+
+   
  %22--%22 + 
@@ -2042,32 +2042,36 @@
 dary + %22%5Cr%5Cn%22 +%0A
+    
                 
@@ -2156,32 +2156,36 @@
                 
+   
+ 
 %22Content-Type: i
@@ -2216,16 +2216,20 @@
         
+    
 %22%5Cr%5Cn%22 +
@@ -2241,24 +2241,28 @@
             
+  
+  
 data + %22%5Cr%5Cn
@@ -2257,32 +2257,36 @@
 data + %22%5Cr%5Cn%22 +%0A
+    
                 

Fix inspector warnings