use of com.vaadin.flow.server.VaadinResponse 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 location
* the location object of the route
*/
public void initializeUI(UI ui, Location location) {
ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger(), e.getState().orElse(null)));
int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
VaadinResponse response = VaadinService.getCurrentResponse();
if (response != null) {
response.setStatus(statusCode);
}
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class UITest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(RootNavigationTarget.class, FooBarNavigationTarget.class, Parameterized.class, FooBarParamNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class InvalidUrlTest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException, ServiceException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(UITest.RootNavigationTarget.class, UITest.FooBarNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_scriptSrcHasNoDoubleQuotes_attributeIsTransferred.
@Test
public void writeBootstrapPage_scriptSrcHasNoDoubleQuotes_attributeIsTransferred() throws IOException {
WebComponentBootstrapHandler handler = new WebComponentBootstrapHandler();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Element head = new Document("").normalise().head();
Element script = head.ownerDocument().createElement("script");
head.appendChild(script);
script.attr("src", "foo'bar%20%27?baz%22");
VaadinResponse response = getMockResponse(stream);
handler.writeBootstrapPage("", response, head, "");
String resultingScript = stream.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(resultingScript, CoreMatchers.containsString("foo'bar%20%27?baz%22"));
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_skipMetaAndStyleHeaderElements.
@Test
public void writeBootstrapPage_skipMetaAndStyleHeaderElements() throws IOException {
WebComponentBootstrapHandler handler = new WebComponentBootstrapHandler();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Element head = new Document("").normalise().head();
Element meta = head.ownerDocument().createElement("meta");
head.appendChild(meta);
meta.attr("http-equiv", "Content-Type");
Element style = head.ownerDocument().createElement("style");
head.appendChild(style);
style.attr("type", "text/css");
style.text("body {height:100vh;width:100vw;margin:0;}");
Element script = head.ownerDocument().createElement("script");
head.appendChild(script);
script.text("var i=1;");
VaadinResponse response = getMockResponse(stream);
handler.writeBootstrapPage("", response, head, "");
String resultingScript = stream.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(resultingScript, CoreMatchers.containsString("var i=1;"));
MatcherAssert.assertThat(resultingScript, CoreMatchers.not(CoreMatchers.containsString("body {height:100vh;width:100vw;margin:0;}")));
MatcherAssert.assertThat(resultingScript, CoreMatchers.not(CoreMatchers.containsString("http-equiv")));
}
Aggregations