Search in sources :

Example 16 with MockResponseListener

use of org.webpieces.http2client.mock.MockResponseListener in project webpieces by deanhiller.

the class TestBackpressure method testBasicBackpressureChunked.

@Test
public void testBasicBackpressureChunked() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = httpSocket.openStream();
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    Http2Request request = Requests.createRequest();
    StreamRef streamRef = handle.process(request, listener);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    Assert.assertTrue(writer.isDone());
    Assert.assertEquals(request, mockChannel.getFrameAndClear());
    List<ByteBuffer> buffers = create4BuffersWith3Messags();
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // consume since not enough data for client
    Assert.assertTrue(fut1.isDone());
    XFuture<StreamWriter> future = new XFuture<StreamWriter>();
    listener.addReturnValueIncomingResponse(future);
    XFuture<Void> fut2 = dataListener.incomingData(mockChannel, buffers.get(1));
    // not resolved yet since client only has part of the data
    Assert.assertFalse(fut2.isDone());
    MockStreamWriter mockWriter = new MockStreamWriter();
    // This releases the response msg acking 10 bytes
    future.complete(mockWriter);
    fut2.get(2, TimeUnit.SECONDS);
    // feed the rest of first chunk in and feed part of last chunk
    XFuture<Void> firstChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(firstChunkAck);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    // ack the http chunk packet
    firstChunkAck.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
    XFuture<Void> lastChunkAck = new XFuture<Void>();
    mockWriter.addProcessResponse(lastChunkAck);
    XFuture<Void> fut4 = dataListener.incomingData(mockChannel, buffers.get(3));
    Assert.assertFalse(fut4.isDone());
    lastChunkAck.complete(null);
    fut4.get(2, TimeUnit.SECONDS);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ByteBuffer(java.nio.ByteBuffer) RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) Test(org.junit.Test) AbstractTest(org.webpieces.http2client.AbstractTest)

Example 17 with MockResponseListener

use of org.webpieces.http2client.mock.MockResponseListener in project webpieces by deanhiller.

the class TestBasicHttp2Client method testBasicSendRespond.

@Test
public void testBasicSendRespond() {
    FullRequest request1 = Requests.createHttp2Request();
    MockResponseListener respListener1 = new MockResponseListener();
    respListener1.setIncomingRespDefault(XFuture.completedFuture(null));
    XFuture<FullResponse> future = httpSocket.send(request1);
    Assert.assertFalse(future.isDone());
}
Also used : FullResponse(org.webpieces.http2client.api.dto.FullResponse) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) FullRequest(org.webpieces.http2client.api.dto.FullRequest) Test(org.junit.Test)

Example 18 with MockResponseListener

use of org.webpieces.http2client.mock.MockResponseListener in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceiveValidFramesAfterSendRstStreamFrame.

/**
 * If this state is reached as a result of sending a RST_STREAM frame, the
 * peer that receives the RST_STREAM might have already sent — or enqueued for
 * sending — frames on the stream that cannot be withdrawn. An endpoint MUST ignore
 * frames that it receives on closed streams after it has sent a RST_STREAM frame. An
 * endpoint MAY choose to limit the period over which it ignores frames and
 * treat frames that arrive after this time as being in error.
 * @throws TimeoutException
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void testSection5_1ReceiveValidFramesAfterSendRstStreamFrame() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
    Http2Request request1 = Requests.createRequest();
    RequestStreamHandle stream = httpSocket.openStream();
    StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef.getWriter();
    @SuppressWarnings("unused") StreamWriter writer = future.get(2, TimeUnit.SECONDS);
    Http2Msg req = mockChannel.getFrameAndClear();
    Assert.assertEquals(request1, req);
    RstStreamFrame rst = new RstStreamFrame(request1.getStreamId(), Http2ErrorCode.CANCEL);
    XFuture<Void> cancel = streamRef.cancel(rst);
    cancel.get(2, TimeUnit.SECONDS);
    Http2Msg svrRst = mockChannel.getFrameAndClear();
    Assert.assertEquals(rst, svrRst);
    // simulate server responding before receiving the cancel
    Http2Response resp1 = Requests.createEosResponse(request1.getStreamId());
    // endOfStream=true
    mockChannel.write(resp1);
// Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
// Assert.assertFalse(mockChannel.isClosed());
// 
// Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
Also used : RequestStreamHandle(com.webpieces.http2.api.streaming.RequestStreamHandle) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) Test(org.junit.Test)

Example 19 with MockResponseListener

use of org.webpieces.http2client.mock.MockResponseListener in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceivePriorityAfterReceiveRstStreamFrame.

/**
 * An endpoint MUST NOT send frames other than ----PRIORITY---- on a closed stream. An endpoint
 * that receives any frame other than ----PRIORITY---- after receiving a ----RST_STREAM---- MUST
 * treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an
 * endpoint that receives any frames after receiving a frame with the
 * END_STREAM flag set MUST treat that as a connection error (Section 5.4.1) of
 * type STREAM_CLOSED, unless the frame is permitted as described below.
 */
@Test
public void testSection5_1ReceivePriorityAfterReceiveRstStreamFrame() {
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(null));
    Http2Request request = sendRequestToServer(listener1);
    sendResetFromServer(listener1, request);
    PriorityDetails details = new PriorityDetails();
    details.setStreamDependency(3);
    PriorityFrame dataFrame = new PriorityFrame(request.getStreamId(), details);
    mockChannel.write(dataFrame);
    // priority is ignored
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
    Assert.assertFalse(mockChannel.isClosed());
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
}
Also used : Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) PriorityDetails(com.webpieces.http2.api.dto.lowlevel.lib.PriorityDetails) PriorityFrame(com.webpieces.http2.api.dto.lowlevel.PriorityFrame) Test(org.junit.Test)

Example 20 with MockResponseListener

use of org.webpieces.http2client.mock.MockResponseListener in project webpieces by deanhiller.

the class TestC5x1StreamStates method testSection5_1ReceiveBadFrameAfterReceiveRstStreamFrame.

/**
 * An endpoint MUST NOT send frames other than PRIORITY on a closed stream. An endpoint
 * that receives any frame other than PRIORITY after receiving a ----RST_STREAM---- MUST
 * treat that as a stream error (Section 5.4.2) of type STREAM_CLOSED. Similarly, an
 * endpoint that receives any frames after receiving a frame with the
 * END_STREAM flag set MUST treat that as a connection error (Section 5.4.1) of
 * type STREAM_CLOSED, unless the frame is permitted as described below.
 */
@Test
public void testSection5_1ReceiveBadFrameAfterReceiveRstStreamFrame() {
    MockStreamWriter mockWriter = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.<StreamWriter>completedFuture(mockWriter));
    Http2Request request = sendRequestToServer(listener1);
    sendResetFromServer(listener1, request);
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    mockChannel.write(dataFrame);
    // remote receives goAway
    GoAwayFrame goAway = (GoAwayFrame) mockChannel.getFrameAndClear();
    Assert.assertEquals(Http2ErrorCode.STREAM_CLOSED, goAway.getKnownErrorCode());
    DataWrapper debugData = goAway.getDebugData();
    String msg = debugData.createStringFromUtf8(0, debugData.getReadableSize());
    Assert.assertEquals("ConnectionException: MockHttp2Channel1:stream1:(CLOSED_STREAM) " + "Stream must have been closed as it no longer exists.  high mark=1  " + "your frame=DataFrame{streamId=1, endStream=false, data.len=0, padding=0}", msg);
    Assert.assertTrue(mockChannel.isClosed());
    Assert.assertEquals(0, listener1.getReturnValuesIncomingResponse().size());
    // send new request on closed connection
    Http2Request request1 = Requests.createRequest();
    StreamRef streamRef = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef.getWriter();
    ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
    Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) ConnectionClosedException(com.webpieces.http2engine.api.error.ConnectionClosedException) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) GoAwayFrame(com.webpieces.http2.api.dto.lowlevel.GoAwayFrame) Test(org.junit.Test)

Aggregations

MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)22 Test (org.junit.Test)21 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)15 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)14 DataWrapper (org.webpieces.data.api.DataWrapper)11 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)9 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)8 StreamRef (com.webpieces.http2.api.streaming.StreamRef)8 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)7 Http2Request (com.webpieces.hpack.api.dto.Http2Request)6 ShutdownStream (com.webpieces.http2engine.api.error.ShutdownStream)5 StreamWriter (com.webpieces.http2engine.api.StreamWriter)4 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)4 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)4 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)4 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)3 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)2 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)2 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)2 MockPushListener (org.webpieces.http2client.mock.MockPushListener)2