Search in sources :

Example 1 with MockServletConfig

use of com.vaadin.flow.server.MockServletConfig in project flow by vaadin.

the class RouterLinkTest method invalidRouteWhenConstructing.

@Test(expected = IllegalArgumentException.class)
public void invalidRouteWhenConstructing() throws ServletException {
    VaadinServlet servlet = new VaadinServlet();
    Properties initParams = new Properties();
    initParams.setProperty(Constants.SERVLET_PARAMETER_USING_NEW_ROUTING, "false");
    servlet.init(new MockServletConfig(initParams));
    try {
        VaadinService.setCurrent(servlet.getService());
        servlet.getService().getRouter().reconfigure(c -> c.setRoute("show/{bar}", TestView.class));
        new RouterLink("Show something", TestView.class);
    } finally {
        VaadinService.setCurrent(null);
    }
}
Also used : TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Properties(java.util.Properties) Test(org.junit.Test)

Example 2 with MockServletConfig

use of com.vaadin.flow.server.MockServletConfig in project flow by vaadin.

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    Router router = new Router();
    router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)2 MockServletConfig (com.vaadin.flow.server.MockServletConfig)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NavigationHandler (com.vaadin.flow.router.NavigationHandler)1 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)1 RouterInterface (com.vaadin.flow.router.RouterInterface)1 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)1 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)1 Resolver (com.vaadin.flow.router.legacy.Resolver)1 Router (com.vaadin.flow.router.legacy.Router)1 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)1 ErrorView (com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView)1 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1