From e3559518bca2cb89ba0938759b8352228cff9d37 Mon Sep 17 00:00:00 2001 From: ursa Date: Wed, 6 Feb 2013 18:21:57 +0400 Subject: [PATCH] Add test for HttpPostRequestDecoder: validate '\r' symbols in the end of binary stream are parsed correctly. --- .../multipart/HttpPostRequestDecoderTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java diff --git a/src/test/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java b/src/test/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java new file mode 100644 index 0000000000..7321a7e97c --- /dev/null +++ b/src/test/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoderTest.java @@ -0,0 +1,70 @@ +/* + * 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()); + } + } +}