use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class MockTcpChannel method write.
@Override
public CompletableFuture<Channel> write(ByteBuffer b) {
DataWrapper data = dataGen.wrapByteBuffer(b);
memento = parser.parse(memento, data);
List<HttpPayload> parsedMessages = memento.getParsedMessages();
for (HttpPayload payload : parsedMessages) {
if (payload instanceof HttpResponse) {
sendResponse((HttpResponse) payload);
} else {
sendData((HttpData) payload);
}
}
return CompletableFuture.completedFuture(this);
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestResponseParsing method testPartialHttpMessage.
@Test
public void testPartialHttpMessage() {
HttpResponse response = createOkResponse();
byte[] payload = unwrap(parser.marshalToByteBuffer(state, response));
byte[] firstPart = new byte[10];
byte[] secondPart = new byte[payload.length - firstPart.length];
// let's split the payload up into two pieces
System.arraycopy(payload, 0, firstPart, 0, firstPart.length);
System.arraycopy(payload, firstPart.length, secondPart, 0, secondPart.length);
DataWrapper data1 = dataGen.wrapByteArray(firstPart);
DataWrapper data2 = dataGen.wrapByteArray(secondPart);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, data1);
Assert.assertEquals(0, memento.getParsedMessages().size());
memento = parser.parse(memento, data2);
Assert.assertEquals(1, memento.getParsedMessages().size());
HttpPayload httpMessage = memento.getParsedMessages().get(0);
Assert.assertEquals(response, httpMessage);
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestNewChunkedParsing method testBasic.
@Test
public void testBasic() {
String chunkedData = "4\r\nWiki\r\n5\r\npedia\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n";
HttpResponse resp = TestResponseParsing.createOkResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
byte[] chunked = chunkedData.getBytes();
byte[] all = new byte[bytes.length + chunked.length];
System.arraycopy(bytes, 0, all, 0, bytes.length);
System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
DataWrapper wrapper = dataGen.wrapByteArray(all);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrapper);
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(5, msgs.size());
HttpPayload msg = msgs.get(0).getHttpResponse();
Assert.assertEquals(resp, msg);
HttpData chunk1 = msgs.get(1).getHttpData();
String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals("Wiki", first);
HttpData chunk3 = msgs.get(3).getHttpData();
String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals(" in\r\n\r\nchunks.", third);
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestNewChunkedParsing method testSplitChunkBodyAndResponseAfter.
@Test
public void testSplitChunkBodyAndResponseAfter() {
String chunkedData = "1E\r\n012345678901234567890123456789\r\n7\r\nchunks.\r\n0\r\n\r\n";
HttpResponse resp = TestResponseParsing.createOkResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
byte[] bytes = unwrap(parser.marshalToByteBuffer(state, resp));
byte[] chunked = chunkedData.getBytes();
HttpResponse resp400 = create400Response();
byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
int lengthOfChunked1stHalf = 15;
int lengthOfChunked2ndHalf = chunked.length - 15;
byte[] firstPiece = new byte[bytes.length + lengthOfChunked1stHalf];
byte[] secondPiece = new byte[chunked.length - lengthOfChunked1stHalf + tail.length];
System.arraycopy(bytes, 0, firstPiece, 0, bytes.length);
System.arraycopy(chunked, 0, firstPiece, bytes.length, lengthOfChunked1stHalf);
System.arraycopy(chunked, lengthOfChunked1stHalf, secondPiece, 0, lengthOfChunked2ndHalf);
System.arraycopy(tail, 0, secondPiece, lengthOfChunked2ndHalf, tail.length);
DataWrapper first = dataGen.wrapByteArray(firstPiece);
DataWrapper second = dataGen.wrapByteArray(secondPiece);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, first);
Assert.assertEquals(ParsingState.CHUNK, memento.getUnParsedState().getCurrentlyParsing());
// The new implementation consumes AS MUCH AS POSSIBLE
Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(2, msgs.size());
Assert.assertEquals(resp, msgs.get(0));
HttpData data = (HttpData) msgs.get(1);
Assert.assertTrue(data.isStartOfChunk());
Assert.assertFalse(data.isEndOfChunk());
Assert.assertFalse(data.isEndOfData());
memento = parser.parse(memento, second);
List<HttpPayload> parsedMessages = memento.getParsedMessages();
Assert.assertEquals(4, parsedMessages.size());
Assert.assertEquals(resp400, parsedMessages.get(3));
}
use of org.webpieces.httpparser.api.dto.HttpPayload in project webpieces by deanhiller.
the class TestRequestBody method testBody.
@Test
public void testBody() {
HttpDummyRequest request = createPostRequestWithBody();
byte[] expected = request.getHttpData().getBody().createByteArray();
byte[] data = unwrap(request);
DataWrapper wrap1 = dataGen.wrapByteArray(data);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrap1);
Assert.assertEquals(2, memento.getParsedMessages().size());
HttpPayload msg = memento.getParsedMessages().get(0);
Assert.assertEquals(request.getRequest(), msg);
HttpData data1 = (HttpData) memento.getParsedMessages().get(1);
DataWrapper body = data1.getBody();
byte[] bodyBytesActual = body.createByteArray();
Assert.assertArrayEquals(expected, bodyBytesActual);
}
Aggregations