work in progress EPILOGUE and END states implemented, in process of testing them

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2018-03-13 17:52:57 +11:00
parent 0435eea708
commit a51542ee03
2 changed files with 137 additions and 5 deletions

View File

@ -302,7 +302,7 @@ public class MultiPartParser
* @param buffer the buffer to parse
* @return True if an {@link RequestHandler} method was called and it returned true;
*/
public boolean parse(ByteBuffer buffer,boolean last)
public boolean parse(ByteBuffer buffer, boolean last)
{
boolean handle = false;
while(handle==false && BufferUtil.hasContent(buffer))
@ -327,14 +327,11 @@ public class MultiPartParser
handle = parseOctetContent(buffer);
break;
case EPILOGUE:
// TODO
handle = true;
BufferUtil.clear(buffer);
break;
case END:
// TODO
handle = true;
break;
@ -344,6 +341,12 @@ public class MultiPartParser
}
}
if(last && _state == State.EPILOGUE)
{
_state = State.END;
_handler.messageComplete();
}
return handle;
}

View File

@ -442,4 +442,133 @@ public class MultiPartParserTest
+ "The quick brown fox jumped over the lazy dog.\r\n","<<LAST>>"));
}
@Test
public void testEpilogue() {
List<String> fields = new ArrayList<>();
List<String> content = new ArrayList<>();
MultiPartParser parser = new MultiPartParser(new MultiPartParser.Handler()
{
@Override
public void parsedHeader(String name, String value)
{
fields.add(name+": "+value);
}
@Override
public boolean headerComplete()
{
fields.add("<<COMPLETE>>");
return false;
}
@Override
public boolean content(ByteBuffer buffer, boolean last)
{
if (BufferUtil.hasContent(buffer))
content.add(BufferUtil.toString(buffer));
if (last)
content.add("<<LAST>>");
return last;
}
},"BOUNDARY");
ByteBuffer data = BufferUtil.toBuffer("");
data = BufferUtil.toBuffer("--BOUNDARY\r\n"
+ "name: value\n"
+ "\r\n"
+ "Hello\r\n"
+ "--BOUNDARY--"
+ "epilogue here:"
+ "\r\n"
+ "--BOUNDARY--"
+ "\r\n"
+ "--BOUNDARY");
parser.parse(data,false);
assertThat(parser.getState(), is(State.DELIMITER));
assertThat(fields,Matchers.contains("name: value", "<<COMPLETE>>"));
assertThat(content,Matchers.contains("Hello","<<LAST>>"));
parser.parse(data,false);
assertThat(parser.getState(), is(State.EPILOGUE));
assertThat(data.remaining(),is(0));
parser.parse(data,true);
assertThat(parser.getState(), is(State.END));
}
@Test
public void testMultipleContent() {
List<String> fields = new ArrayList<>();
List<String> content = new ArrayList<>();
MultiPartParser parser = new MultiPartParser(new MultiPartParser.Handler()
{
@Override
public void parsedHeader(String name, String value)
{
fields.add(name+": "+value);
}
@Override
public boolean headerComplete()
{
fields.add("<<COMPLETE>>");
return false;
}
@Override
public boolean content(ByteBuffer buffer, boolean last)
{
if (BufferUtil.hasContent(buffer))
content.add(BufferUtil.toString(buffer));
if (last)
content.add("<<LAST>>");
return last;
}
},"BOUNDARY");
ByteBuffer data = BufferUtil.toBuffer("");
data = BufferUtil.toBuffer("--BOUNDARY\r\n"
+ "name: value\n"
+ "\r\n"
+ "Hello"
+ "\r\n"
+ "--BOUNDARY\r\n"
+ "powerLevel: 9001"
+ "\r\n"
+ "secondary"
+ "\r\n"
+ "content"
+ "\r\n--BOUNDARY--"
+ "epilogue here");
/* Test First Content Section */
parser.parse(data,false);
assertThat(parser.getState(), is(State.DELIMITER));
assertThat(fields,Matchers.contains("name: value", "<<COMPLETE>>"));
assertThat(content,Matchers.contains("Hello","<<LAST>>"));
/* Test Second Content Section */
parser.parse(data,false);
assertThat(parser.getState(), is(State.DELIMITER));
assertThat(fields,Matchers.contains("name: value", "<<COMPLETE>>","powerLevel: 9001"));
assertThat(content,Matchers.contains("Hello","<<LAST>>","secondary\r\ncontent"));
/* Test Progression to EPILOGUE State */
parser.parse(data,false);
assertThat(parser.getState(), is(State.EPILOGUE));
assertThat(data.remaining(),is(0));
/* Test Progression to END State */
parser.parse(data,false);
assertThat(parser.getState(), is(State.END));
assertThat(data.remaining(),is(0));
}
}