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);
}
}
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);
}
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());
}
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());
}
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);
}
Aggregations