use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestChunkedParsing method testResponseAfterChunked.
@Test
public void testResponseAfterChunked() {
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();
HttpResponse resp400 = create400Response();
byte[] tail = unwrap(parser.marshalToByteBuffer(state, resp400));
byte[] all = new byte[bytes.length + chunked.length + tail.length];
System.arraycopy(bytes, 0, all, 0, bytes.length);
System.arraycopy(chunked, 0, all, bytes.length, chunked.length);
System.arraycopy(tail, 0, all, bytes.length + chunked.length, tail.length);
DataWrapper wrapper = dataGen.wrapByteArray(all);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrapper);
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(6, msgs.size());
HttpPayload msg = msgs.get(0).getHttpResponse();
Assert.assertEquals(resp, msg);
HttpChunk chunk1 = msgs.get(1).getHttpChunk();
String first = chunk1.getBody().createStringFrom(0, chunk1.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals("Wiki", first);
HttpChunk chunk3 = msgs.get(3).getHttpChunk();
String third = chunk3.getBody().createStringFrom(0, chunk3.getBody().getReadableSize(), Charset.defaultCharset());
Assert.assertEquals(" in\r\n\r\nchunks.", third);
HttpPayload tailMsg = msgs.get(5);
Assert.assertEquals(resp400, tailMsg);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestReceiveHttp2 method testReceivePreface.
@Test
public void testReceivePreface() {
//All the 1's must be ignored and in leftover data
String preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n1111";
byte[] bytes = preface.getBytes(StandardCharsets.UTF_8);
DataWrapper moreData = dataGen.wrapByteArray(bytes);
Memento state = parser.prepareToParse();
state = parser.parse(state, moreData);
Assert.assertEquals(1, state.getParsedMessages().size());
Assert.assertEquals(4, state.getLeftOverData().getReadableSize());
HttpPayload httpPayload = state.getParsedMessages().get(0);
Assert.assertEquals(HttpMessageType.HTTP2_MARKER_MSG, httpPayload.getMessageType());
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestRequestBody method testPartialHeaders.
@Test
public void testPartialHeaders() {
HttpDummyRequest request = createPostRequestWithBody();
byte[] expected = request.getHttpData().getBody().createByteArray();
byte[] data = unwrap(request);
byte[] first = new byte[20];
byte[] second = new byte[10];
byte[] third = new byte[data.length - first.length - second.length];
System.arraycopy(data, 0, first, 0, first.length);
System.arraycopy(data, first.length, second, 0, second.length);
System.arraycopy(data, first.length + second.length, third, 0, third.length);
DataWrapper wrap1 = dataGen.wrapByteArray(first);
DataWrapper wrap2 = dataGen.wrapByteArray(second);
DataWrapper wrap3 = dataGen.wrapByteArray(third);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrap1);
Assert.assertEquals(ParsingState.HEADERS, memento.getUnParsedState().getCurrentlyParsing());
Assert.assertEquals(20, memento.getUnParsedState().getCurrentUnparsedSize());
memento = parser.parse(memento, wrap2);
Assert.assertEquals(ParsingState.HEADERS, memento.getUnParsedState().getCurrentlyParsing());
Assert.assertEquals(30, memento.getUnParsedState().getCurrentUnparsedSize());
Assert.assertEquals(0, memento.getParsedMessages().size());
memento = parser.parse(memento, wrap3);
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);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestRequestBody method createPostRequestWithBody.
private HttpDummyRequest createPostRequestWithBody() {
byte[] payload = new byte[30];
for (int i = 0; i < payload.length; i++) {
payload[i] = (byte) i;
}
HttpRequest request = TestRequestParsing.createPostRequest();
Header header = new Header();
header.setName(KnownHeaderName.CONTENT_LENGTH);
header.setValue("" + payload.length);
DataWrapper data = dataGen.wrapByteArray(payload);
request.addHeader(header);
HttpData httpData = new HttpData(data, true);
return new HttpDummyRequest(request, httpData);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestChunkedParsing 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());
Assert.assertEquals(11, memento.getUnParsedState().getCurrentUnparsedSize());
List<HttpPayload> msgs = memento.getParsedMessages();
Assert.assertEquals(1, msgs.size());
Assert.assertEquals(resp, msgs.get(0));
memento = parser.parse(memento, second);
List<HttpPayload> parsedMessages = memento.getParsedMessages();
Assert.assertEquals(4, parsedMessages.size());
Assert.assertEquals(resp400, parsedMessages.get(3));
}
Aggregations