use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestRequestBody method testPartialBodyThenHalfBodyWithNextHttpMsg.
@Test
public void testPartialBodyThenHalfBodyWithNextHttpMsg() {
HttpDummyRequest request = createPostRequestWithBody();
byte[] expected = request.getHttpData().getBody().createByteArray();
byte[] data = unwrap(request);
HttpRequest request2 = TestRequestParsing.createPostRequest();
byte[] payload = unwrap(parser.marshalToByteBuffer(state, request2));
byte[] first = new byte[data.length - 20];
byte[] second = new byte[data.length - first.length + payload.length];
System.arraycopy(data, 0, first, 0, first.length);
int lenOfLastPiece = data.length - first.length;
System.arraycopy(data, first.length, second, 0, lenOfLastPiece);
System.arraycopy(payload, 0, second, lenOfLastPiece, payload.length);
DataWrapper wrap1 = dataGen.wrapByteArray(first);
DataWrapper wrap2 = dataGen.wrapByteArray(second);
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);
memento = parser.parse(memento, wrap2);
Assert.assertEquals(2, memento.getParsedMessages().size());
HttpData data2 = (HttpData) memento.getParsedMessages().get(0);
DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
byte[] bodyBytesActual = body.createByteArray();
Assert.assertArrayEquals(expected, bodyBytesActual);
HttpPayload msg2 = memento.getParsedMessages().get(1);
Assert.assertEquals(request2, msg2);
}
use of org.webpieces.httpparser.api.dto.HttpData 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.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class HttpParserImpl method parseData.
private byte[] parseData(MarshalStateImpl state, HttpPayload payload) {
if (!(payload instanceof HttpData))
throw new IllegalStateException("You passed in a request or response earlier with Content-Length so must pass in all the HttpData next not this=" + payload);
HttpData data = payload.getHttpData();
DataWrapper body = data.getBodyNonNull();
state.addMoreBytes(body.getReadableSize());
if (state.getTotalRead() > state.getParsingDataSize())
throw new IllegalStateException("Content-Length was " + state.getParsingDataSize() + " but you have so far passed in " + state.getTotalRead() + " bytes");
else if (state.getTotalRead() == state.getParsingDataSize()) {
state.resetDataReading();
}
return body.createByteArray();
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestRequestBody method testPartialBody.
@Test
public void testPartialBody() {
HttpDummyRequest request = createPostRequestWithBody();
byte[] expected = request.getHttpData().getBody().createByteArray();
byte[] data = unwrap(request);
byte[] first = new byte[data.length - 20];
byte[] second = new byte[data.length - first.length];
System.arraycopy(data, 0, first, 0, first.length);
System.arraycopy(data, first.length, second, 0, second.length);
DataWrapper wrap1 = dataGen.wrapByteArray(first);
DataWrapper wrap2 = dataGen.wrapByteArray(second);
Memento memento = parser.prepareToParse();
memento = parser.parse(memento, wrap1);
Assert.assertEquals(ParsingState.BODY, memento.getUnParsedState().getCurrentlyParsing());
Assert.assertEquals(0, memento.getUnParsedState().getCurrentUnparsedSize());
HttpRequest httpRequest = memento.getParsedMessages().get(0).getHttpRequest();
Assert.assertEquals(request.getRequest(), httpRequest);
HttpData data1 = memento.getParsedMessages().get(1).getHttpData();
Assert.assertEquals(2, memento.getParsedMessages().size());
memento = parser.parse(memento, wrap2);
Assert.assertEquals(1, memento.getParsedMessages().size());
HttpData data2 = memento.getParsedMessages().get(0).getHttpData();
DataWrapper body = dataGen.chainDataWrappers(data1.getBody(), data2.getBody());
byte[] bodyBytesActual = body.createByteArray();
Assert.assertArrayEquals(expected, bodyBytesActual);
}
use of org.webpieces.httpparser.api.dto.HttpData in project webpieces by deanhiller.
the class TestHttp11Basic method testSendTwoRequestsStreamFirstResponse.
@Test
public void testSendTwoRequestsStreamFirstResponse() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.write(req);
PassedIn in1 = mockListener.getSingleRequest();
mockChannel.write(req2);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
Http2Response headers1 = Http2Translations.responseToHeaders(resp1);
CompletableFuture<StreamWriter> future = in1.stream.sendResponse(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
byte[] buf = new byte[10];
DataWrapper dataWrapper = dataGen.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, true);
DataFrame data = (DataFrame) Http2Translations.translateData(data1);
writer.processPiece(data);
HttpData d = (HttpData) mockChannel.getFrameAndClear();
Assert.assertEquals(10, d.getBody().getReadableSize());
PassedIn in2 = mockListener.getSingleRequest();
HttpResponse resp2 = Requests.createResponse(2);
resp2.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "0"));
Http2Response headers2 = Http2Translations.responseToHeaders(resp2);
in2.stream.sendResponse(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
Aggregations