use of org.webpieces.data.api.DataWrapper 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);
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestRequestParsing method test2AndHalfHttpMessages.
@Test
public void test2AndHalfHttpMessages() {
HttpRequest request = createPostRequest();
byte[] payload = unwrap(parser.marshalToByteBuffer(state, request));
byte[] first = new byte[2 * payload.length + 20];
byte[] second = new byte[payload.length - 20];
System.arraycopy(payload, 0, first, 0, payload.length);
System.arraycopy(payload, 0, first, payload.length, payload.length);
System.arraycopy(payload, 0, first, 2 * payload.length, 20);
System.arraycopy(payload, 20, second, 0, second.length);
DataWrapper data1 = dataGen.wrapByteArray(first);
DataWrapper data2 = dataGen.wrapByteArray(second);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, data1);
Assert.assertEquals(2, memento.getParsedMessages().size());
memento = parser.parse(memento, data2);
Assert.assertEquals(1, memento.getParsedMessages().size());
}
use of org.webpieces.data.api.DataWrapper in project webpieces by deanhiller.
the class TestRequestParsing method testHalfThenTwoHalvesNext.
/**
* Send in 1/2 first http message and then send in
* next 1/2 AND 1/2 of second message TOGETHER in 2nd
* payload of bytes to make sure it is handled correctly
* and then finally last 1/2
*/
@Test
public void testHalfThenTwoHalvesNext() {
HttpRequest request = createPostRequest();
byte[] payload = unwrap(parser.marshalToByteBuffer(state, request));
byte[] first = new byte[20];
byte[] second = new byte[payload.length];
byte[] third = new byte[payload.length - first.length];
System.arraycopy(payload, 0, first, 0, first.length);
System.arraycopy(payload, first.length, second, 0, payload.length - first.length);
System.arraycopy(payload, 0, second, payload.length - first.length, first.length);
System.arraycopy(payload, first.length, third, 0, third.length);
DataWrapper data1 = dataGen.wrapByteArray(first);
DataWrapper data2 = dataGen.wrapByteArray(second);
DataWrapper data3 = dataGen.wrapByteArray(third);
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());
memento = parser.parse(memento, data3);
Assert.assertEquals(1, memento.getParsedMessages().size());
}
use of org.webpieces.data.api.DataWrapper 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.data.api.DataWrapper in project webpieces by deanhiller.
the class StaticFileReader method sendHttpChunk.
private CompletableFuture<StreamWriter> sendHttpChunk(StreamWriter writer, BufferPool pool, ByteBuffer buf, Path file, boolean isEos) {
DataWrapper data = wrapperFactory.wrapByteBuffer(buf);
DataFrame frame = new DataFrame();
frame.setEndOfStream(isEos);
frame.setData(data);
return writer.processPiece(frame).thenApply(w -> {
buf.position(buf.limit());
pool.releaseBuffer(buf);
return w;
});
}
Aggregations