Search in sources :

Example 26 with Http2Response

use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.

the class TestC5x1StreamStates method sendEosResponseFromServer.

private void sendEosResponseFromServer(MockResponseListener listener1, Http2Request request) {
    Http2Response resp1 = Requests.createEosResponse(request.getStreamId());
    // endOfStream=true
    mockChannel.write(resp1);
    Http2Response response1 = (Http2Response) listener1.getSingleReturnValueIncomingResponse();
    Assert.assertEquals(resp1, response1);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response)

Example 27 with Http2Response

use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.

the class ErrorCommonTest method testRedirectRouteNotEnoughArgumentsBUTwithKeepAlive.

@Test
public void testRedirectRouteNotEnoughArgumentsBUTwithKeepAlive() {
    // say method is something(int arg, String this)
    // we verify redirects MUST match type and number of method arguments every time
    // then when we form url, we put the stuff in the path OR put it as query params so it works on the way back in again too
    String moduleFileContents = CommonRoutesModules.class.getName();
    RouterService server = createServer(isProdTest, moduleFileContents);
    server.start();
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/user/5553");
    // ADD a keep alive to test keeping alive
    req.addHeader(new Http2Header(Http2HeaderName.CONNECTION, "keep-alive"));
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response response = mockStream.getLastResponse();
    String contents = mockStream.getResponseBody();
    Assert.assertEquals(response.getSingleHeaderValue(Http2HeaderName.STATUS), "500");
    if (isProdTest) {
        Assert.assertTrue(contents.contains("There was a bug in the developers application or webpieces server."));
    } else {
        // use DevServer template
        Assert.assertTrue(contents.contains("NullTemplateApi.java is running for templatePath=org.webpieces.devrouter.impl.internalError_html"));
    }
    // We did send a keep alive so it should close
    Assert.assertFalse(mockStream.isWasClosed());
}
Also used : RouterService(org.webpieces.router.api.RouterService) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Test(org.junit.Test)

Example 28 with Http2Response

use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.

the class TestHttp1Backpressure method testBasicBackpressure.

@Test
public void testBasicBackpressure() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.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.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 250);
    HttpData response2 = Requests.createHttpData(250);
    List<ByteBuffer> buffers = create3BuffersWithTwoMessags(response1, response2);
    DataListener dataListener = mockChannel.getConnectedListener();
    XFuture<StreamWriter> requestFuture = new XFuture<StreamWriter>();
    listener.addProcessResponse(requestFuture);
    XFuture<Void> fut1 = dataListener.incomingData(mockChannel, buffers.get(0));
    // resolved since data is cached in parser (client doesn't receive this data)
    Assert.assertTrue(fut1.isDone());
    // This next one is confusing BUT in http1.1 parsing terms, data is data for content length so this results
    // in a full HttpData packet actually...
    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();
    XFuture<Void> streamWriterFuture = new XFuture<Void>();
    mockWriter.addProcessResponse(streamWriterFuture);
    // This releases BOTH packets above to be processed!!!(not just the one)
    requestFuture.complete(mockWriter);
    Assert.assertFalse(fut2.isDone());
    streamWriterFuture.complete(null);
    // NOW it's resolved
    fut2.get(2, TimeUnit.SECONDS);
    // feed the last buffer in
    XFuture<Void> future3 = new XFuture<Void>();
    mockWriter.addProcessResponse(future3);
    XFuture<Void> fut3 = dataListener.incomingData(mockChannel, buffers.get(2));
    Assert.assertFalse(fut3.isDone());
    future3.complete(null);
    fut3.get(2, TimeUnit.SECONDS);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.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) HttpData(org.webpieces.httpparser.api.dto.HttpData) DataListener(org.webpieces.nio.api.handlers.DataListener) MockStreamWriter(org.webpieces.httpclient.api.mocks.MockStreamWriter) Test(org.junit.Test)

Example 29 with Http2Response

use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.

the class Requests method createResponse.

public static Http2Response createResponse(int id, int contentLength) {
    List<Http2Header> headers = new ArrayList<>();
    headers.add(new Http2Header(Http2HeaderName.STATUS, "200"));
    headers.add(new Http2Header(Http2HeaderName.SERVER, id + ""));
    headers.add(new Http2Header(Http2HeaderName.CONTENT_LENGTH, contentLength + ""));
    Http2Response response = new Http2Response(headers);
    if (contentLength == 0)
        response.setEndOfStream(true);
    return response;
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) ArrayList(java.util.ArrayList)

Example 30 with Http2Response

use of com.webpieces.http2.api.dto.highlevel.Http2Response in project webpieces by deanhiller.

the class TestWriteReads method testBasicReadWrite.

@Test
public void testBasicReadWrite() throws InterruptedException, ExecutionException, TimeoutException {
    MockResponseListener listener = new MockResponseListener();
    RequestStreamHandle handle = socket.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.getLastWriteParam());
    MockResponseListener listener2 = new MockResponseListener();
    request.getHeaderLookupStruct().getHeader("serverid").setValue("2");
    mockChannel.addWriteResponse(XFuture.completedFuture(null));
    StreamRef streamRef1 = handle.process(request, listener2);
    XFuture<StreamWriter> writer2 = streamRef1.getWriter();
    Assert.assertTrue(writer2.isDone());
    Assert.assertEquals(request, mockChannel.getLastWriteParam());
    Http2Response response1 = Requests.createResponse(1, 0);
    listener.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut1 = mockChannel.writeResponse(response1);
    // throws if exception occurred and ensures future resolved
    fut1.get(2, TimeUnit.SECONDS);
    Http2Response msg = listener.getIncomingMsg();
    Assert.assertEquals(response1, msg);
    Http2Response response2 = Requests.createResponse(2, 0);
    listener2.addProcessResponse(XFuture.completedFuture(null));
    XFuture<Void> fut2 = mockChannel.writeResponse(response2);
    // throws if exception occurred and ensures future resolved
    fut2.get(2, TimeUnit.SECONDS);
    Http2Response msg2 = listener2.getIncomingMsg();
    Assert.assertEquals(response2, msg2);
}
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) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockResponseListener(org.webpieces.httpclient.api.mocks.MockResponseListener) Test(org.junit.Test)

Aggregations

Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)66 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)32 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)30 Test (org.junit.Test)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)24 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)16 StreamRef (com.webpieces.http2.api.streaming.StreamRef)15 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)14 DataWrapper (org.webpieces.data.api.DataWrapper)13 Header (org.webpieces.httpparser.api.common.Header)13 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)12 ArrayList (java.util.ArrayList)10 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)10 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 HttpPayload (org.webpieces.httpparser.api.dto.HttpPayload)9 HttpData (org.webpieces.httpparser.api.dto.HttpData)8 XFuture (org.webpieces.util.futures.XFuture)8 RouterService (org.webpieces.router.api.RouterService)5 Http2Headers (com.webpieces.http2.api.dto.highlevel.Http2Headers)4 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)4