use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testResolverBeforeSetRoute.
@Test
public void testResolverBeforeSetRoute() {
Router router = new Router();
AtomicReference<String> usedHandler = new AtomicReference<>();
router.reconfigure(configuration -> {
configuration.setResolver(resolveEvent -> Optional.of(handlerEvent -> {
usedHandler.set("resolver");
return HttpServletResponse.SC_OK;
}));
configuration.setRoute("*", e -> {
usedHandler.set("route");
return HttpServletResponse.SC_OK;
});
});
router.navigate(new RouterTestUI(), new Location(""), NavigationTrigger.PROGRAMMATIC);
Assert.assertEquals("resolver", usedHandler.get());
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testNavigateToEmptyLocation_triggersErrorView.
@Test
public void testNavigateToEmptyLocation_triggersErrorView() {
UI ui = new RouterTestUI();
Router router = new Router();
router.reconfigure(c -> {
c.setErrorView(ErrorView.class);
});
router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
Assert.assertEquals(new ErrorView().getText(), ui.getElement().getTextRecursively());
}
use of com.vaadin.flow.router.Location 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());
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testStatusCodeUpdates.
@Test
public void testStatusCodeUpdates() {
RouterTestUI ui = new RouterTestUI();
Router router = (Router) ui.getRouterInterface().get();
router.reconfigure(c -> {
c.setRoute("*", e -> 123);
});
VaadinResponse response = Mockito.mock(VaadinResponse.class);
try {
CurrentInstance.set(VaadinResponse.class, response);
VaadinRequest request = requestWithPathInfo(null);
router.initializeUI(ui, request);
// Response status should be set when initializing
Mockito.verify(response).setStatus(123);
router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
// Non-init navigation shouldn't set any status code
Mockito.verifyNoMoreInteractions(response);
} finally {
CurrentInstance.clearAll();
}
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class ViewRendererTest method routeParametersInEvent.
@Test
public void routeParametersInEvent() {
router.reconfigure(c -> c.setRoute("foo/{name}/*", TestView.class));
router.navigate(ui, new Location("foo/bar/baz/"), NavigationTrigger.PROGRAMMATIC);
TestView testView = (TestView) ui.getInternals().getActiveViewChain().get(0);
Assert.assertEquals("bar", testView.namePlaceholderValue);
Assert.assertEquals("baz/", testView.wildcardValue);
}
Aggregations