Search in sources :

Example 16 with StreamWriter

use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.

the class TestBasicHttp2Server method testBasicIntegration.

@Test
public void testBasicIntegration() throws InterruptedException, ExecutionException {
    MockStreamWriter mockSw = new MockStreamWriter();
    mockListener.addMockStreamToReturn(mockSw);
    MockStreamWriter mockSw2 = new MockStreamWriter();
    mockListener.addMockStreamToReturn(mockSw2);
    Http2Request request1 = Http2Requests.createRequest(1, true);
    Http2Request request2 = Http2Requests.createRequest(3, true);
    mockChannel.send(request1);
    PassedIn requestAndStream1 = mockListener.getSingleRequest();
    mockChannel.send(request2);
    PassedIn requestAndStream2 = mockListener.getSingleRequest();
    //each stream given to webapp is a unique one....
    Assert.assertTrue(requestAndStream1.stream != requestAndStream2.stream);
    Assert.assertEquals(request1, requestAndStream1.request);
    Assert.assertEquals(request2, requestAndStream2.request);
    Assert.assertEquals(1, request1.getStreamId());
    Assert.assertEquals(3, request2.getStreamId());
    Http2Response resp2 = Http2Requests.createResponse(request2.getStreamId());
    CompletableFuture<StreamWriter> future = requestAndStream2.stream.sendResponse(resp2);
    Assert.assertTrue(future.isDone());
    Http2Response frame2 = (Http2Response) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp2, frame2);
    Http2Response resp1 = Http2Requests.createResponse(request1.getStreamId());
    CompletableFuture<StreamWriter> future1 = requestAndStream1.stream.sendResponse(resp1);
    Assert.assertTrue(future1.isDone());
    Http2Response frame1 = (Http2Response) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp1, frame1);
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) Http2Request(com.webpieces.hpack.api.dto.Http2Request) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) StreamWriter(com.webpieces.http2engine.api.StreamWriter) MockStreamWriter(org.webpieces.httpfrontend2.api.mock2.MockStreamWriter) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 17 with StreamWriter

use of com.webpieces.http2engine.api.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 = dataGen.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.write(req);
    mockChannel.write(body);
    PassedIn in1 = mockListener.getSingleRequest();
    HttpRequest req1 = Http2Translations.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 = Http2Translations.responseToHeaders(resp);
    CompletableFuture<StreamWriter> fut = in1.stream.sendResponse(http2Resp);
    fut.get(2, TimeUnit.SECONDS);
    HttpResponse respToClient = (HttpResponse) mockChannel.getFrameAndClear();
    Assert.assertEquals(resp, respToClient);
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) HttpRequest(org.webpieces.httpparser.api.dto.HttpRequest) Http2Response(com.webpieces.hpack.api.dto.Http2Response) Header(org.webpieces.httpparser.api.common.Header) HttpData(org.webpieces.httpparser.api.dto.HttpData) StreamWriter(com.webpieces.http2engine.api.StreamWriter) HttpResponse(org.webpieces.httpparser.api.dto.HttpResponse) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) Test(org.junit.Test)

Example 18 with StreamWriter

use of com.webpieces.http2engine.api.StreamWriter 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 {
    int streamId = 1;
    PassedIn info = sendRequestToServer(streamId, false);
    ResponseStream stream = info.stream;
    Http2Request request = info.request;
    //send data that goes with request
    DataFrame dataFrame = new DataFrame(request.getStreamId(), false);
    byte[] buf = new byte[localSettings.getMaxFrameSize() + 4];
    dataFrame.setData(dataGen.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());
    Cancel failResp = mockListener.getCancelInfo();
    ShutdownStream reset = (ShutdownStream) failResp.reset;
    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());
    CompletableFuture<StreamWriter> future = stream.sendResponse(response);
    ConnectionClosedException intercept = (ConnectionClosedException) TestAssert.intercept(future);
    Assert.assertTrue(intercept.getMessage().contains("Connection closed or closing"));
    Assert.assertEquals(0, mockChannel.getFramesAndClear().size());
}
Also used : Http2Response(com.webpieces.hpack.api.dto.Http2Response) Cancel(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.Cancel) StreamWriter(com.webpieces.http2engine.api.StreamWriter) ConnectionClosedException(com.webpieces.http2engine.api.ConnectionClosedException) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) PassedIn(org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn) ResponseStream(org.webpieces.frontend2.api.ResponseStream) GoAwayFrame(com.webpieces.http2parser.api.dto.GoAwayFrame) DataWrapper(org.webpieces.data.api.DataWrapper) ShutdownStream(com.webpieces.http2engine.api.error.ShutdownStream) Http2Request(com.webpieces.hpack.api.dto.Http2Request) Test(org.junit.Test)

Example 19 with StreamWriter

use of com.webpieces.http2engine.api.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());
}
Also used : StreamWriter(com.webpieces.http2engine.api.StreamWriter) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Http2Msg(com.webpieces.http2parser.api.dto.lib.Http2Msg) Test(org.junit.Test)

Example 20 with StreamWriter

use of com.webpieces.http2engine.api.StreamWriter in project webpieces by deanhiller.

the class TestSMaxConcurrentSetting method testSend2ndPushHeadersOnlyOnAfterSettingsFrameMaxConcurrentBigger.

@Test
public void testSend2ndPushHeadersOnlyOnAfterSettingsFrameMaxConcurrentBigger() throws InterruptedException, ExecutionException, TimeoutException {
    WriterHolder sent = sendTwoRequests();
    //client increases max concurrent
    HeaderSettings settings = new HeaderSettings();
    settings.setMaxConcurrentStreams(2L);
    mockChannel.send(HeaderSettings.createSettingsFrame(settings));
    List<Http2Msg> frames = mockChannel.getFramesAndClear();
    Assert.assertEquals(2, frames.size());
    Assert.assertEquals(sent.getResp2(), frames.get(0));
    SettingsFrame dataRecv1 = (SettingsFrame) frames.get(1);
    Assert.assertTrue(dataRecv1.isAck());
    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());
}
Also used : SettingsFrame(com.webpieces.http2parser.api.dto.SettingsFrame) HeaderSettings(com.webpieces.http2engine.impl.shared.data.HeaderSettings) StreamWriter(com.webpieces.http2engine.api.StreamWriter) DataFrame(com.webpieces.http2parser.api.dto.DataFrame) Http2Msg(com.webpieces.http2parser.api.dto.lib.Http2Msg) Test(org.junit.Test)

Aggregations

StreamWriter (com.webpieces.http2engine.api.StreamWriter)26 DataFrame (com.webpieces.http2parser.api.dto.DataFrame)17 Test (org.junit.Test)16 Http2Response (com.webpieces.hpack.api.dto.Http2Response)13 Http2Request (com.webpieces.hpack.api.dto.Http2Request)12 DataWrapper (org.webpieces.data.api.DataWrapper)9 Http2Msg (com.webpieces.http2parser.api.dto.lib.Http2Msg)8 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)8 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)8 ConnectionClosedException (com.webpieces.http2engine.api.ConnectionClosedException)5 GoAwayFrame (com.webpieces.http2parser.api.dto.GoAwayFrame)5 Header (org.webpieces.httpparser.api.common.Header)4 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)4 Http2Header (com.webpieces.http2parser.api.dto.lib.Http2Header)3 Http2HeaderName (com.webpieces.http2parser.api.dto.lib.Http2HeaderName)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ResponseStream (org.webpieces.frontend2.api.ResponseStream)3 MockStreamWriter (org.webpieces.httpfrontend2.api.mock2.MockStreamWriter)3 HttpData (org.webpieces.httpparser.api.dto.HttpData)3