Using buffered boundary stream in multipart parser. But currently not working
This commit is contained in:
parent
53dec4603c
commit
077963ae28
11 changed files with 198 additions and 152 deletions
|
|
@ -29,13 +29,14 @@ import org.junit.Test;
|
|||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public class BufferedBoundaryInputStreamTest {
|
||||
|
||||
@Test
|
||||
public void testReadB1() throws IOException {
|
||||
public void normal() throws IOException {
|
||||
StringInputStream inin = new StringInputStream("aaa#aaaaaaaaaaaaaaaa#aaaaaaaaaaaaaaa#");
|
||||
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
|
||||
|
||||
|
|
@ -61,7 +62,16 @@ public class BufferedBoundaryInputStreamTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyBoundaries() throws IOException {
|
||||
public void startWithBound() throws IOException {
|
||||
StringInputStream inin = new StringInputStream("#aaa");
|
||||
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
|
||||
in.setBoundary("#");
|
||||
|
||||
assertEquals(-1, in.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onlyBoundaries() throws IOException {
|
||||
StringInputStream inin = new StringInputStream("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
||||
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
|
||||
|
||||
|
|
@ -72,14 +82,14 @@ public class BufferedBoundaryInputStreamTest {
|
|||
assertEquals(-1, in.read());
|
||||
assertEquals(-1, in.read());
|
||||
in.next();
|
||||
if(!in.isBoundary())
|
||||
if(!in.hasNext())
|
||||
break;
|
||||
}
|
||||
assertEquals(35, n);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoBounds() throws IOException {
|
||||
public void noBounds() throws IOException {
|
||||
String data = "1234567891011121314151617181920";
|
||||
StringInputStream inin = new StringInputStream(data);
|
||||
BufferedBoundaryInputStream in = new BufferedBoundaryInputStream(inin);
|
||||
|
|
|
|||
|
|
@ -1,48 +1,47 @@
|
|||
package zutil.net.http.multipart;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.io.StringInputStream;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.StringReader;
|
||||
import java.util.Iterator;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* JUnit test for MultipartParser class
|
||||
*
|
||||
* <p>
|
||||
* Created by Ziver on 2016-07-06.
|
||||
*/
|
||||
public class MultipartParserTest {
|
||||
|
||||
@Test
|
||||
public void singleFormDataField(){
|
||||
public void singleFormDataField() {
|
||||
String input =
|
||||
"------------------------------83ff53821b7c\n" +
|
||||
"Content-Disposition: form-data; name=\"foo\"\n" +
|
||||
"\n" +
|
||||
"bar\n" +
|
||||
"------------------------------83ff53821b7c--";
|
||||
"------------------------------83ff53821b7c\n" +
|
||||
"Content-Disposition: form-data; name=\"foo\"\n" +
|
||||
"\n" +
|
||||
"bar\n" +
|
||||
"------------------------------83ff53821b7c--";
|
||||
MultipartParser parser = new MultipartParser(
|
||||
new BufferedReader(new StringReader(input)),
|
||||
new StringInputStream(input),
|
||||
"----------------------------83ff53821b7c",
|
||||
input.length());
|
||||
|
||||
int count = 0;
|
||||
for(MultipartField field : parser){
|
||||
assertEquals(0, count);
|
||||
if (field instanceof MultipartStringField) {
|
||||
MultipartStringField stringField = (MultipartStringField)field;
|
||||
assertEquals("foo", stringField.getName());
|
||||
assertEquals("bar", stringField.getValue());
|
||||
}
|
||||
else fail("Field is not an instance of "+MultipartStringField.class);
|
||||
++count;
|
||||
}
|
||||
// Assertions
|
||||
Iterator<MultipartField> it = parser.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MultipartField field = it.next();
|
||||
assertTrue(field instanceof MultipartStringField);
|
||||
MultipartStringField stringField = (MultipartStringField) field;
|
||||
assertEquals("foo", stringField.getName());
|
||||
assertEquals("bar", stringField.getValue());
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void singleFileUpload(){
|
||||
public void singleFileUpload() {
|
||||
String input =
|
||||
"------------------------------83ff53821b7c\n" +
|
||||
"Content-Disposition: form-data; name=\"img\"; filename=\"a.png\"\n" +
|
||||
|
|
@ -57,7 +56,7 @@ public class MultipartParserTest {
|
|||
" IDAc????????-IEND?B`?\n" +
|
||||
"------------------------------83ff53821b7c--";
|
||||
MultipartParser parser = new MultipartParser(
|
||||
new BufferedReader(new StringReader(input)),
|
||||
new StringInputStream(input),
|
||||
"----------------------------83ff53821b7c",
|
||||
input.length());
|
||||
|
||||
|
|
@ -65,31 +64,31 @@ public class MultipartParserTest {
|
|||
}
|
||||
|
||||
//Test
|
||||
public void multiFileUpload(){
|
||||
public void multiFileUpload() {
|
||||
String input =
|
||||
" --AaB03x\n" +
|
||||
" Content-Disposition: form-data; name=\"submit-name\"\n" +
|
||||
"--AaB03x\n" +
|
||||
"Content-Disposition: form-data; name=\"submit-name\"\n" +
|
||||
"\n" +
|
||||
" Larry\n" +
|
||||
" --AaB03x\n" +
|
||||
" Content-Disposition: form-data; name=\"files\"\n" +
|
||||
" Content-Type: multipart/mixed; boundary=BbC04y\n" +
|
||||
"Larry\n" +
|
||||
"--AaB03x\n" +
|
||||
"Content-Disposition: form-data; name=\"files\"\n" +
|
||||
"Content-Type: multipart/mixed; boundary=BbC04y\n" +
|
||||
"\n" +
|
||||
" --BbC04y\n" +
|
||||
" Content-Disposition: file; filename=\"file1.txt\"\n" +
|
||||
" Content-Type: text/plain\n" +
|
||||
"--BbC04y\n" +
|
||||
"Content-Disposition: file; filename=\"file1.txt\"\n" +
|
||||
"Content-Type: text/plain\n" +
|
||||
"\n" +
|
||||
" ... contents of file1.txt ...\n" +
|
||||
" --BbC04y\n" +
|
||||
" Content-Disposition: file; filename=\"file2.gif\"\n" +
|
||||
" Content-Type: image/gif\n" +
|
||||
" Content-Transfer-Encoding: binary\n" +
|
||||
"... contents of file1.txt ...\n" +
|
||||
"--BbC04y\n" +
|
||||
"Content-Disposition: file; filename=\"file2.gif\"\n" +
|
||||
"Content-Type: image/gif\n" +
|
||||
"Content-Transfer-Encoding: binary\n" +
|
||||
"\n" +
|
||||
" ...contents of file2.gif...\n" +
|
||||
" --BbC04y--\n" +
|
||||
" --AaB03x--";
|
||||
"...contents of file2.gif...\n" +
|
||||
"--BbC04y--\n" +
|
||||
"--AaB03x--";
|
||||
MultipartParser parser = new MultipartParser(
|
||||
new BufferedReader(new StringReader(input)),
|
||||
new StringInputStream(input),
|
||||
"AaB03x",
|
||||
input.length());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,19 +83,16 @@ public class CircularBufferTest {
|
|||
} catch (IndexOutOfBoundsException e) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void iteratorEmpty() {
|
||||
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(10);
|
||||
Iterator<Integer> it = buff.iterator();
|
||||
|
||||
assert (!it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
fail("NoSuchElementException was not thrown");
|
||||
} catch (NoSuchElementException e) {}
|
||||
it.next(); // Exception
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void iteratorThreeElements() {
|
||||
CircularBuffer<Integer> buff = new CircularBuffer<Integer>(10);
|
||||
buff.add(10);
|
||||
|
|
@ -111,9 +108,6 @@ public class CircularBufferTest {
|
|||
assertEquals((Integer) 10, it.next());
|
||||
assert (!it.hasNext());
|
||||
|
||||
try {
|
||||
it.next();
|
||||
fail("NoSuchElementException was not thrown");
|
||||
} catch (NoSuchElementException e) {}
|
||||
it.next(); // Exception
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue