Search in sources :

Example 1 with MockResponseStream

use of org.webpieces.router.api.mocks.MockResponseStream 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="));
    }
    RouterRequest req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockResponseStream mockResponseStream = new MockResponseStream();
    server.incomingCompleteRequest(req, mockResponseStream);
    Exception e = mockResponseStream.getOnlyException();
    Assert.assertEquals(IllegalStateException.class, e.getClass());
    Assert.assertTrue(e.getMessage().contains("start was not called by client or start threw"));
}
Also used : RouterService(org.webpieces.router.api.RouterService) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) ErrorCommonTest(org.webpieces.router.api.error.ErrorCommonTest) Test(org.junit.Test)

Example 2 with MockResponseStream

use of org.webpieces.router.api.mocks.MockResponseStream in project webpieces by deanhiller.

the class TestSimpleRoutes method testOneParamRoute.

@Test
public void testOneParamRoute() {
    RouterRequest req = createHttpRequest(HttpMethod.POST, "/meeting");
    MockResponseStream mockResponseStream = new MockResponseStream();
    Current.setContext(new RequestContext(new ValidationImpl(null), new FlashImpl(null), new SessionImpl(null), req));
    server.incomingCompleteRequest(req, mockResponseStream);
    List<RedirectResponse> responses = mockResponseStream.getSendRedirectCalledList();
    Assert.assertEquals(1, responses.size());
    RedirectResponse response = responses.get(0);
    Assert.assertEquals(req.domain, response.domain);
    Assert.assertFalse(response.isHttps);
    Assert.assertEquals("/meeting/888", response.redirectToPath);
}
Also used : RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) RequestContext(org.webpieces.ctx.api.RequestContext) FlashImpl(org.webpieces.router.impl.ctx.FlashImpl) SessionImpl(org.webpieces.router.impl.ctx.SessionImpl) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) ValidationImpl(org.webpieces.router.impl.ctx.ValidationImpl) Test(org.junit.Test)

Example 3 with MockResponseStream

use of org.webpieces.router.api.mocks.MockResponseStream in project webpieces by deanhiller.

the class TestSimpleRoutes method testBasicRoute.

@Test
public void testBasicRoute() {
    RouterRequest req = createHttpRequest(HttpMethod.GET, "/something");
    MockResponseStream mockResponseStream = new MockResponseStream();
    Current.setContext(new RequestContext(new ValidationImpl(null), new FlashImpl(null), new SessionImpl(null), req));
    server.incomingCompleteRequest(req, mockResponseStream);
    List<RedirectResponse> responses = mockResponseStream.getSendRedirectCalledList();
    Assert.assertEquals(1, responses.size());
    RedirectResponse response = responses.get(0);
    Assert.assertEquals(req.domain, response.domain);
    Assert.assertFalse(response.isHttps);
    Assert.assertEquals("/something", response.redirectToPath);
}
Also used : RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) RequestContext(org.webpieces.ctx.api.RequestContext) FlashImpl(org.webpieces.router.impl.ctx.FlashImpl) SessionImpl(org.webpieces.router.impl.ctx.SessionImpl) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) ValidationImpl(org.webpieces.router.impl.ctx.ValidationImpl) Test(org.junit.Test)

Example 4 with MockResponseStream

use of org.webpieces.router.api.mocks.MockResponseStream 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(false, moduleFileContents);
    //this should definitely not throw since we lazy load everything in dev...
    server.start();
    RouterRequest req = RequestCreation.createHttpRequest(HttpMethod.GET, "/something");
    MockResponseStream mockResponseStream = new MockResponseStream();
    server.incomingCompleteRequest(req, mockResponseStream);
    List<RenderResponse> renders = mockResponseStream.getSendRenderHtmlList();
    Assert.assertEquals(1, renders.size());
    RenderResponse renderResponse = renders.get(0);
    Assert.assertEquals(RouteType.INTERNAL_SERVER_ERROR, renderResponse.routeType);
}
Also used : RouterService(org.webpieces.router.api.RouterService) RenderResponse(org.webpieces.router.api.dto.RenderResponse) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) Test(org.junit.Test) ErrorCommonTest(org.webpieces.router.api.error.ErrorCommonTest)

Example 5 with MockResponseStream

use of org.webpieces.router.api.mocks.MockResponseStream 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() {
    RouterRequest req = createHttpRequest(HttpMethod.GET, "/async");
    //setup returning a response
    CompletableFuture<Integer> future = new CompletableFuture<Integer>();
    overrides.mockService.addToReturn(future);
    MockResponseStream mockResponseStream = new MockResponseStream();
    server.incomingCompleteRequest(req, mockResponseStream);
    //no response yet...
    List<RedirectResponse> responses = mockResponseStream.getSendRedirectCalledList();
    Assert.assertEquals(0, responses.size());
    int id = 78888;
    future.complete(id);
    responses = mockResponseStream.getSendRedirectCalledList();
    Assert.assertEquals(1, responses.size());
    RedirectResponse response = responses.get(0);
    Assert.assertEquals(req.domain, response.domain);
    Assert.assertFalse(response.isHttps);
    Assert.assertEquals("/meeting/" + id, response.redirectToPath);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RedirectResponse(org.webpieces.router.api.dto.RedirectResponse) RouterRequest(org.webpieces.ctx.api.RouterRequest) MockResponseStream(org.webpieces.router.api.mocks.MockResponseStream) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 RouterRequest (org.webpieces.ctx.api.RouterRequest)8 MockResponseStream (org.webpieces.router.api.mocks.MockResponseStream)8 RequestContext (org.webpieces.ctx.api.RequestContext)5 RouterService (org.webpieces.router.api.RouterService)5 FlashImpl (org.webpieces.router.impl.ctx.FlashImpl)5 SessionImpl (org.webpieces.router.impl.ctx.SessionImpl)5 ValidationImpl (org.webpieces.router.impl.ctx.ValidationImpl)5 RedirectResponse (org.webpieces.router.api.dto.RedirectResponse)3 ErrorCommonTest (org.webpieces.router.api.error.ErrorCommonTest)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 RenderResponse (org.webpieces.router.api.dto.RenderResponse)1