Search in sources :

Example 11 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class ProxyRequestStreamHandle method process.

@Override
public StreamRef process(Http2Request request, ResponseStreamHandle responseListener) {
    ProxyResponseStream proxyResponse = new ProxyResponseStream(responseListener, frontendSocket);
    Map<String, Object> context = Context.copyContext();
    // clear context so server uses a clean context
    Context.restoreContext(new HashMap<>());
    try {
        StreamRef streamRef = stream.incomingRequest(request, proxyResponse);
        return new MockProxyStreamRef(streamRef);
    } finally {
        // client still in this context
        Context.restoreContext(context);
    }
}
Also used : StreamRef(com.webpieces.http2.api.streaming.StreamRef)

Example 12 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef 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 13 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class ErrorTest method testNoMethod.

@Test
public void testNoMethod() {
    log.info("starting");
    String moduleFileContents = NoMethodRouterModules.class.getName();
    RouterService server = ErrorCommonTest.createServer(true, moduleFileContents);
    try {
        server.start();
        Assert.fail("Should have thrown exception on start since this is prod");
    } catch (RuntimeException e) {
        Assert.assertTrue(e.getMessage().contains("Cannot find 'public' method='thisMethodNotExist' on class="));
    }
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    // done and no exception SINCE we responded to client successfully
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response response = mockStream.getLastResponse();
    String body = mockStream.getResponseBody();
    Assert.assertEquals(StatusCode.HTTP_500_INTERNAL_SERVER_ERROR, response.getKnownStatusCode());
    Assert.assertTrue(body.contains("There was a bug in the developers application or webpieces server"));
}
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) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockStreamHandle(org.webpieces.router.api.error.MockStreamHandle) Test(org.junit.Test) ErrorCommonTest(org.webpieces.router.api.error.ErrorCommonTest)

Example 14 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestSimpleRoutes method testOneParamRoute.

@Test
public void testOneParamRoute() {
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.POST, "/meeting");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response resp = mockStream.getLastResponse();
    Assert.assertEquals("http://" + req.getAuthority() + "/meeting/888", resp.getSingleHeaderValue(Http2HeaderName.LOCATION));
}
Also used : 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) MockStreamHandle(org.webpieces.router.api.error.MockStreamHandle) Test(org.junit.Test)

Example 15 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestSimpleRoutes method testBasicRoute.

@Test
public void testBasicRoute() {
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response resp = mockStream.getLastResponse();
    Assert.assertEquals("http://" + req.getAuthority() + "/something", resp.getSingleHeaderValue(Http2HeaderName.LOCATION));
}
Also used : 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) MockStreamHandle(org.webpieces.router.api.error.MockStreamHandle) Test(org.junit.Test)

Aggregations

StreamRef (com.webpieces.http2.api.streaming.StreamRef)34 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)27 Test (org.junit.Test)22 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)17 XFuture (org.webpieces.util.futures.XFuture)12 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)8 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)6 DataWrapper (org.webpieces.data.api.DataWrapper)6 RouterService (org.webpieces.router.api.RouterService)6 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)6 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)5 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)5 Function (java.util.function.Function)5 RequestContext (org.webpieces.ctx.api.RequestContext)5 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)5 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)4 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)4