Search in sources :

Example 6 with QueryParameters

use of com.vaadin.flow.router.QueryParameters in project flow by vaadin.

the class Router method initializeUI.

/**
 * Enables navigation for a new UI instance. This initializes the UI content
 * based on the location used for loading the UI and sets up the UI to be
 * updated when the user navigates to some other location.
 *
 * @param ui
 *            the UI that navigation should be set up for
 * @param initRequest
 *            the Vaadin request that bootstraps the provided UI
 */
@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
    assert getConfiguration().isConfigured();
    String pathInfo = initRequest.getPathInfo();
    final String path;
    if (pathInfo == null) {
        path = "";
    } else {
        assert pathInfo.startsWith("/");
        path = pathInfo.substring(1);
    }
    final QueryParameters queryParameters = QueryParameters.full(initRequest.getParameterMap());
    ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger()));
    Location location = new Location(path, queryParameters);
    int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
    VaadinResponse response = VaadinService.getCurrentResponse();
    if (response != null) {
        response.setStatus(statusCode);
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location)

Example 7 with QueryParameters

use of com.vaadin.flow.router.QueryParameters in project flow by vaadin.

the class UITest method navigateWithParameters.

@Test
public void navigateWithParameters() {
    requestHandled = false;
    final String route = "params";
    UI ui = createAndInitTestUI(route);
    QueryParameters params = QueryParameters.simple(Collections.singletonMap("test", "indeed"));
    ui.getRouterInterface().get().reconfigure(c -> c.setRoute(route, event -> {
        assertEquals(params.getParameters(), event.getLocation().getQueryParameters().getParameters());
        requestHandled = true;
        return HttpServletResponse.SC_OK;
    }));
    ui.navigate(route, params);
    assertEquals(route, ui.getInternals().getActiveViewLocation().getPath());
    assertTrue("Request with QueryParameters was not handled.", requestHandled);
}
Also used : ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) NodeVisitor(com.vaadin.flow.dom.NodeVisitor) ArrayList(java.util.ArrayList) History(com.vaadin.flow.component.page.History) ArgumentCaptor(org.mockito.ArgumentCaptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Element(com.vaadin.flow.dom.Element) Location(com.vaadin.flow.router.Location) Constants(com.vaadin.flow.server.Constants) AbstractTextElementStateProvider(com.vaadin.flow.dom.impl.AbstractTextElementStateProvider) Node(com.vaadin.flow.dom.Node) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) QueryParameters(com.vaadin.flow.router.QueryParameters) Properties(java.util.Properties) ServletConfig(javax.servlet.ServletConfig) StateNode(com.vaadin.flow.internal.StateNode) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Mockito(org.mockito.Mockito) List(java.util.List) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) QueryParameters(com.vaadin.flow.router.QueryParameters) Test(org.junit.Test)

Example 8 with QueryParameters

use of com.vaadin.flow.router.QueryParameters in project flow by vaadin.

the class UITest method navigateWithParameters_delegateToRouter.

@Test
public void navigateWithParameters_delegateToRouter() {
    final String route = "params";
    Router router = Mockito.mock(Router.class);
    UI ui = new MockUI(router);
    QueryParameters params = QueryParameters.simple(Collections.singletonMap("test", "indeed"));
    ArgumentCaptor<Location> location = ArgumentCaptor.forClass(Location.class);
    ui.navigate(route, params);
    Mockito.verify(router).navigate(ArgumentMatchers.eq(ui), location.capture(), ArgumentMatchers.eq(NavigationTrigger.UI_NAVIGATE));
    Location value = location.getValue();
    Assert.assertEquals(route, value.getPath());
    Assert.assertEquals(params, value.getQueryParameters());
}
Also used : MockUI(com.vaadin.tests.util.MockUI) MockUI(com.vaadin.tests.util.MockUI) Router(com.vaadin.flow.router.Router) QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 9 with QueryParameters

use of com.vaadin.flow.router.QueryParameters in project flow by vaadin.

the class NavigationStateRendererTest method handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved.

@Test
public void handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    // the path is the same, location params will be different
    String path = "foo";
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a NavigationStateRenderer mapping to PreservedView
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(PreservedView.class));
    // given the session has a cache of PreservedView at this location
    final PreservedView view = new PreservedView();
    MockUI ui = new MockUI(session);
    ui.add(view);
    AbstractNavigationStateRenderer.setPreservedChain(session, "ROOT.123", new Location(path, new QueryParameters(Collections.singletonMap("a", Collections.emptyList()))), new ArrayList<>(Arrays.asList(view)));
    ExtendedClientDetails details = Mockito.mock(ExtendedClientDetails.class);
    Mockito.when(details.getWindowName()).thenReturn("ROOT.123");
    ui.getInternals().setExtendedClientDetails(details);
    AtomicInteger count = new AtomicInteger();
    view.addDetachListener(event -> count.getAndIncrement());
    NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(path, new QueryParameters(Collections.singletonMap("b", Collections.emptyList()))), ui, NavigationTrigger.ROUTER_LINK, Json.createObject(), false);
    renderer.handle(event);
    Assert.assertFalse(ui.isClosing());
    Assert.assertEquals(0, count.get());
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) QueryParameters(com.vaadin.flow.router.QueryParameters) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtendedClientDetails(com.vaadin.flow.component.page.ExtendedClientDetails) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 10 with QueryParameters

use of com.vaadin.flow.router.QueryParameters in project flow by vaadin.

the class LocationTest method locationWithParametersPath_withoutTrailingSlash.

@Test
public void locationWithParametersPath_withoutTrailingSlash() {
    String initialPath = "foo/bar";
    QueryParameters queryParams = getQueryParameters();
    Location location = new Location(initialPath, queryParams);
    String pathWithParameters = location.getPathWithQueryParameters();
    assertEquals(initialPath + '?' + queryParams.getQueryString(), pathWithParameters);
}
Also used : QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

QueryParameters (com.vaadin.flow.router.QueryParameters)17 Test (org.junit.Test)15 Location (com.vaadin.flow.router.Location)7 List (java.util.List)4 Router (com.vaadin.flow.router.Router)2 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)2 VaadinResponse (com.vaadin.flow.server.VaadinResponse)2 MockUI (com.vaadin.tests.util.MockUI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)1 History (com.vaadin.flow.component.page.History)1 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)1 Element (com.vaadin.flow.dom.Element)1 Node (com.vaadin.flow.dom.Node)1 NodeVisitor (com.vaadin.flow.dom.NodeVisitor)1 AbstractTextElementStateProvider (com.vaadin.flow.dom.impl.AbstractTextElementStateProvider)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 StateNode (com.vaadin.flow.internal.StateNode)1