use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.
the class TestStreamingRaw method testAsyncJsonGet.
@Test
public void testAsyncJsonGet() throws InterruptedException, ExecutionException {
MockChannel channel = new MockChannel();
XFuture<DataListener> future = mgr.simulateHttpConnect(channel);
DataListener dataListener = future.get();
HttpFullRequest fullRequest = Requests.createJsonRequest(KnownHttpMethod.GET, "/json/streaming");
ByteBuffer buffer = parser.marshalToByteBuffer(fullRequest.getRequest());
DataWrapper data = fullRequest.getData();
byte[] headers = new byte[buffer.remaining()];
buffer.get(headers);
data.getReadableSize();
byte[] part1 = data.readBytesAt(0, 10);
String str = new String(part1);
byte[] part2 = data.readBytesAt(10, data.getReadableSize() - 10);
String str2 = new String(part2);
ByteBuffer firstPacket = ByteBuffer.allocate(headers.length + part1.length);
firstPacket.put(headers);
firstPacket.put(part1);
firstPacket.flip();
XFuture<Boolean> authFuture = new XFuture<Boolean>();
mockAuth.addValueToReturn(authFuture);
Assert.assertEquals(0, Context.getContext().size());
// Feed in request with content-length AND part of the body as well...
XFuture<Void> fut1 = dataListener.incomingData(channel, firstPacket);
Assert.assertEquals(0, Context.getContext().size());
// Feed in more BEFORE authFuture is complete(this was the bug, ie. race condition)
ByteBuffer buf2 = ByteBuffer.allocate(part2.length);
buf2.put(part2);
buf2.flip();
XFuture<Void> fut2 = dataListener.incomingData(channel, buf2);
Assert.assertEquals(0, Context.getContext().size());
XFuture<StreamWriter> streamWriterFuture = new XFuture<StreamWriter>();
mockStreamClient.addStreamWriter(streamWriterFuture);
// complete it
authFuture.complete(true);
Assert.assertFalse(fut1.isDone());
Assert.assertFalse(fut2.isDone());
MockStreamWriter2 mockStreamWriter = new MockStreamWriter2();
streamWriterFuture.complete(mockStreamWriter);
Assert.assertEquals(0, Context.getContext().size());
Assert.assertTrue(fut1.isDone());
Assert.assertTrue(fut2.isDone());
List<StreamMsg> frames = mockStreamWriter.getFrames();
Assert.assertEquals(2, frames.size());
DataFrame f1 = (DataFrame) frames.get(0);
DataFrame f2 = (DataFrame) frames.get(1);
String s1 = f1.getData().createStringFromUtf8(0, f1.getData().getReadableSize());
Assert.assertEquals(str, s1);
String s2 = f2.getData().createStringFromUtf8(0, f2.getData().getReadableSize());
Assert.assertEquals(str2, s2);
Assert.assertEquals(0, Context.getContext().size());
}
use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.
the class TestHttp11Basic method testPostWithChunking.
@Test
public void testPostWithChunking() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
req.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpChunk chunk = new HttpChunk();
String bodyStr = "hi here and there";
DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
chunk.setBody(data);
mockChannel.sendToSvr(chunk);
DataFrame singleFrame = (DataFrame) mockStreamWriter.getSingleFrame();
Assert.assertTrue(!singleFrame.isEndOfStream());
DataWrapper data2 = singleFrame.getData();
String result = data2.createStringFromUtf8(0, data2.getReadableSize());
Assert.assertEquals(bodyStr, result);
HttpLastChunk last = new HttpLastChunk();
mockChannel.sendToSvr(last);
DataFrame lastEmpty = (DataFrame) mockStreamWriter.getSingleFrame();
Assert.assertTrue(lastEmpty.isEndOfStream());
Assert.assertEquals(0, lastEmpty.getData().getReadableSize());
}
use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.
the class TestHttp11Basic method testFileDownloadWithChunking.
@Test
public void testFileDownloadWithChunking() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
Assert.assertEquals(req, req1);
HttpResponse resp = Requests.createResponse();
resp.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
Http2Response headers = Http11ToHttp2.responseToHeaders(resp);
XFuture<StreamWriter> future = in1.stream.process(headers);
HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
Assert.assertEquals(resp, respToClient);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
DataFrame dataFrame = new DataFrame();
dataFrame.setEndOfStream(true);
String bodyStr = "hi here and there";
DataWrapper data = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
dataFrame.setData(data);
writer.processPiece(dataFrame);
List<HttpPayload> frames = mockChannel.getFramesAndClear();
Assert.assertEquals(2, frames.size());
HttpData chunk = (HttpData) frames.get(0);
DataWrapper body = chunk.getBodyNonNull();
String result = body.createStringFromUtf8(0, body.getReadableSize());
Assert.assertEquals(bodyStr, result);
HttpLastData last = (HttpLastData) frames.get(1);
Assert.assertEquals(0, last.getBodyNonNull().getReadableSize());
}
use of com.webpieces.http2.api.dto.lowlevel.DataFrame 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.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
XFuture<Void> fut1 = mockChannel.sendToSvrAsync(req2);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
HttpResponse resp1 = Requests.createResponse(1);
resp1.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "10"));
Http2Response headers1 = Http11ToHttp2.responseToHeaders(resp1);
XFuture<StreamWriter> future = in1.stream.process(headers1);
HttpPayload payload = mockChannel.getFrameAndClear();
Assert.assertEquals(resp1, payload);
StreamWriter writer = future.get(2, TimeUnit.SECONDS);
Assert.assertFalse(fut1.isDone());
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
byte[] buf = new byte[10];
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(buf);
HttpData data1 = new HttpData(dataWrapper, true);
DataFrame data = (DataFrame) Http11ToHttp2.translateData(data1);
writer.processPiece(data);
fut1.get(2, TimeUnit.SECONDS);
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 = Http11ToHttp2.responseToHeaders(resp2);
in2.stream.process(headers2);
HttpPayload payload2 = mockChannel.getFrameAndClear();
Assert.assertEquals(resp2, payload2);
}
use of com.webpieces.http2.api.dto.lowlevel.DataFrame in project webpieces by deanhiller.
the class Layer2Http11Handler method processCorrectly.
private XFuture<Void> processCorrectly(FrontendSocketImpl socket, HttpPayload payload) {
try {
MDC.put("svrSocket", socket.getChannel().getChannelId());
Http2Msg msg = Http11ToHttp2.translate(payload, socket.isForServingHttpsPages());
if (payload instanceof HttpRequest) {
return processInitialPieceOfRequest(socket, (HttpRequest) payload, (Http2Request) msg);
} else if (msg instanceof DataFrame) {
return processData(socket, (DataFrame) msg);
} else {
throw new IllegalArgumentException("payload not supported=" + payload);
}
} finally {
MDC.put("svrSocket", "");
}
}
Aggregations