use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Errors method testCloseAfter2ndRequest.
@Test
public void testCloseAfter2ndRequest() throws InterruptedException, ExecutionException, TimeoutException {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpRequest req2 = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
XFuture<StreamWriter> futA = XFuture.completedFuture(null);
MockStreamRef mockStreamRefA = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStreamRefA);
mockChannel.sendToSvr(req);
PassedIn in1 = mockListener.getSingleRequest();
XFuture<StreamWriter> futB = XFuture.completedFuture(null);
MockStreamRef mockStreamRefB = new MockStreamRef(futB);
mockListener.addMockStreamToReturn(mockStreamRefB);
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);
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertFalse(mockStreamRefB.isCancelled());
mockChannel.simulateClose();
// this request is done, nothing to cancel
Assert.assertFalse(mockStreamRefA.isCancelled());
Assert.assertTrue(mockStreamRefB.isCancelled());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Errors method testRemoteClientClosesSocket.
@Test
public void testRemoteClientClosesSocket() {
XFuture<StreamWriter> fut = XFuture.completedFuture(null);
MockStreamRef mockStreamRef = new MockStreamRef(fut);
mockListener.addMockStreamToReturn(mockStreamRef);
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);
Assert.assertFalse(mockStreamRef.isCancelled());
mockChannel.simulateClose();
Assert.assertTrue(mockStreamRef.isCancelled());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestHttp11Basic method testUploadWithBody.
@Test
public void testUploadWithBody() throws InterruptedException, ExecutionException, TimeoutException {
String bodyStr = "hi there, how are you";
DataWrapper dataWrapper = DATA_GEN.wrapByteArray(bodyStr.getBytes(StandardCharsets.UTF_8));
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/xxxx");
HttpData body = new HttpData(dataWrapper, true);
req.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, "" + dataWrapper.getReadableSize()));
mockChannel.sendToSvr(req);
mockChannel.sendToSvr(body);
PassedIn in1 = mockListener.getSingleRequest();
HttpRequest req1 = Http2ToHttp11.translateRequest(in1.request);
Assert.assertEquals(req, req1);
DataFrame frame = (DataFrame) mockStreamWriter.getSingleFrame();
DataWrapper data = frame.getData();
Assert.assertEquals(bodyStr, data.createStringFromUtf8(0, data.getReadableSize()));
Assert.assertTrue(frame.isEndOfStream());
HttpResponse resp = Requests.createNobodyResponse();
Http2Response http2Resp = Http11ToHttp2.responseToHeaders(resp);
XFuture<StreamWriter> fut = in1.stream.process(http2Resp);
fut.get(2, TimeUnit.SECONDS);
HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
Assert.assertEquals(resp, respToClient);
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestS5x1StreamStates method testSection5_1_1TooLowStreamIdAfterHighStreamId.
/**
* The identifier of a newly established stream MUST be numerically
* greater than all streams that the initiating endpoint has opened
* or reserved. This governs streams that are opened using a HEADERS
* frame and streams that are reserved using PUSH_PROMISE. An endpoint
* that receives an unexpected stream identifier MUST respond with
* a connection error (Section 5.4.1) of type PROTOCOL_ERROR.
*
* This is in conflict with another part of the spec!!!!! and so we pretend
* the stream is closed(as in all likely hood, the stream was closed)!!!
* and do not shutdown the whole connection for a case like this.
*
* The part it is in conflict with is closed state and receiving messages
* in closed state. The only way to resolve conflict would be to KEEP around
* state that a connection is closed. SORRY, the connection is closed so we
* clean up all memory!!!
*/
@Test
public void testSection5_1_1TooLowStreamIdAfterHighStreamId() {
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
MockStreamRef mockStream = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStream);
Http2Request request1 = Http2Requests.createRequest(5, true);
mockChannel.send(request1);
mockListener.getSingleRequest();
Http2Request request = Http2Requests.createRequest(3, true);
mockChannel.send(request);
// WE DO NOT DO THIS which spec wants(or another test we have starts failing)
// we leave this here in case you want to comment back in and debug that.
// //no request comes in
// Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
// //cancel the first stream since whole connection is going down.
// Assert.assertEquals(1, mockListener.getNumCancelsThatCameIn());
//
// //remote receives goAway
// GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
// Assert.assertEquals(Http2ErrorCode.PROTOCOL_ERROR, goAway.getKnownErrorCode());
// DataWrapper debugData = goAway.getDebugData();
// String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
// Assert.assertTrue(msg.contains("Bad stream id. Event stream ids not allowed in requests to a server frame="));
// Assert.assertTrue(mockChannel.isClosed());
// no request comes in
Assert.assertEquals(0, mockListener.getNumRequestsThatCameIn());
// we do not close the channel
Assert.assertFalse(mockListener.isClosed());
// our existing streams stays valid and open
Assert.assertFalse(mockStream.isCancelled());
// remote receives goAway
RstStreamFrame frame = (RstStreamFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, frame.getKnownErrorCode());
Assert.assertTrue(!mockChannel.isClosed());
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class TestSMaxConcurrentSetting method testSend2ndPushHeadersOnlyOnCompletionOfFirst.
@Test
public void testSend2ndPushHeadersOnlyOnCompletionOfFirst() throws InterruptedException, ExecutionException, TimeoutException {
WriterHolder sent = sendTwoRequests();
DataFrame data1 = Http2Requests.createData1(sent.getResp1().getStreamId(), true);
// ending this promise stream starts the next
sent.getWriter1().processPiece(data1);
List<Http2Msg> frames = mockChannel.getFramesAndClear();
Assert.assertEquals(2, frames.size());
Assert.assertEquals(sent.getResp2(), frames.get(0));
DataFrame dataRecv1 = (DataFrame) frames.get(1);
Assert.assertEquals(sent.getResp1().getStreamId(), dataRecv1.getStreamId());
StreamWriter writer2 = sent.getFuture2().get(2, TimeUnit.SECONDS);
DataFrame data2 = Http2Requests.createData1(sent.getResp2().getStreamId(), true);
writer2.processPiece(data2);
DataFrame dataRecv2 = (DataFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(sent.getResp2().getStreamId(), dataRecv2.getStreamId());
}
Aggregations