use of org.webpieces.httpfrontend2.api.mock2.MockStreamRef 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 org.webpieces.httpfrontend2.api.mock2.MockStreamRef 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 org.webpieces.httpfrontend2.api.mock2.MockStreamRef 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 org.webpieces.httpfrontend2.api.mock2.MockStreamRef in project webpieces by deanhiller.
the class TestS4FrameSizeAndHeaders method testSection4_2FrameTooLarge.
/**
* An endpoint MUST send an error code of FRAME_SIZE_ERROR if a frame
* exceeds the size defined in SETTINGS_MAX_FRAME_SIZE, exceeds any
* limit defined for the frame type, or is too small to contain
* mandatory frame data. A frame size error in a frame that could alter
* the state of the entire connection MUST be treated as a connection
* error (Section 5.4.1); this includes any frame carrying a header
* block (Section 4.3) (that is, HEADERS, PUSH_PROMISE, and
* CONTINUATION), SETTINGS, and any frame with a stream identifier of 0.
* @throws TimeoutException
* @throws ExecutionException
* @throws InterruptedException
*/
@Test
public void testSection4_2FrameTooLarge() throws InterruptedException, ExecutionException, TimeoutException {
MockStreamWriter mockWriter = new MockStreamWriter();
XFuture<StreamWriter> futA = XFuture.completedFuture(mockWriter);
MockStreamRef mockStream = new MockStreamRef(futA);
mockListener.addMockStreamToReturn(mockStream);
int streamId = 1;
PassedIn info = sendRequestToServer(streamId, false);
ResponseStream stream = info.stream;
Http2Request request = info.request;
Assert.assertFalse(mockStream.isCancelled());
// send data that goes with request
DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
byte[] buf = new byte[localSettings.getMaxFrameSize() + 4];
dataFrame.setData(DATA_GEN.wrapByteArray(buf));
// endOfStream=false
mockChannel.send(dataFrame);
// remote receives goAway
GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
Assert.assertEquals(Http2ErrorCode.FRAME_SIZE_ERROR, goAway.getKnownErrorCode());
DataWrapper debugData = goAway.getDebugData();
String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
Assert.assertEquals("ConnectionException: stream1:(EXCEEDED_MAX_FRAME_SIZE) Frame size=16389 was greater than max=16385", msg);
Assert.assertTrue(mockChannel.isClosed());
Assert.assertTrue(mockListener.isClosed());
Assert.assertTrue(mockStream.isCancelled());
CancelReason failResp = mockStream.getCancelInfo();
ShutdownStream reset = (ShutdownStream) failResp;
Assert.assertEquals(CancelReasonCode.EXCEEDED_MAX_FRAME_SIZE, reset.getCause().getReasonCode());
// send response with request not complete but failed as well anyways
Http2Response response = Http2Requests.createResponse(request.getStreamId());
XFuture<StreamWriter> future = stream.process(response);
ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Aggregations