Search in sources :

Example 1 with MockStreamHandle

use of org.webpieces.router.api.error.MockStreamHandle 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 2 with MockStreamHandle

use of org.webpieces.router.api.error.MockStreamHandle 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 3 with MockStreamHandle

use of org.webpieces.router.api.error.MockStreamHandle 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)

Example 4 with MockStreamHandle

use of org.webpieces.router.api.error.MockStreamHandle in project webpieces by deanhiller.

the class TestProdRouter method testAsyncRouteAndMocking.

/**
 * This test won't work in DevRoute right now as we need to do the following
 * 1. create CompileOnDemand very early on
 * 2. do a Thread.current().setContextClassLoader(compileOnDemand.getLatestClassloader())
 *
 * and this all needs to be done BEFORE TestModule is created and more importantly before
 * the bind(SomeService.class) as SomeService will be loaded from one classloader and then
 * when DEVrouter creates the controller, the compileOnDemand classloader is used resulting
 * in a mismatch.
 */
@Test
public void testAsyncRouteAndMocking() {
    Http2Request req = RequestCreation.createHttpRequest(HttpMethod.GET, "/async");
    // setup returning a response
    XFuture<Integer> future1 = new XFuture<Integer>();
    overrides.mockService.addToReturn(future1);
    MockStreamHandle mockStream = new MockStreamHandle();
    StreamRef ref = server.incomingRequest(req, mockStream);
    XFuture<StreamWriter> future = ref.getWriter();
    Assert.assertFalse(future.isDone());
    // no response yet...
    Assert.assertNull(mockStream.getLastResponse());
    // release controlleer
    int id = 78888;
    future1.complete(id);
    Assert.assertTrue(future.isDone() && !future.isCompletedExceptionally());
    Http2Response resp = mockStream.getLastResponse();
    Assert.assertNull(resp.getSingleHeaderValue(Http2HeaderName.AUTHORITY));
    Assert.assertEquals("http://" + req.getAuthority() + "/meeting/" + id, resp.getSingleHeaderValue(Http2HeaderName.LOCATION));
    // We did not send a keep alive so it should close
    Assert.assertTrue(mockStream.isWasClosed());
}
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) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) MockStreamHandle(org.webpieces.router.api.error.MockStreamHandle) Test(org.junit.Test)

Aggregations

Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)4 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)4 StreamRef (com.webpieces.http2.api.streaming.StreamRef)4 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)4 Test (org.junit.Test)4 MockStreamHandle (org.webpieces.router.api.error.MockStreamHandle)4 RouterService (org.webpieces.router.api.RouterService)1 ErrorCommonTest (org.webpieces.router.api.error.ErrorCommonTest)1 XFuture (org.webpieces.util.futures.XFuture)1