Search in sources :

Example 16 with VaadinResponse

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);
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse)

Example 17 with VaadinResponse

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());
    }
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 18 with VaadinResponse

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());
    }
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 19 with VaadinResponse

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"));
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) Element(org.jsoup.nodes.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 20 with VaadinResponse

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")));
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) Element(org.jsoup.nodes.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

VaadinResponse (com.vaadin.flow.server.VaadinResponse)24 Test (org.junit.Test)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 VaadinRequest (com.vaadin.flow.server.VaadinRequest)8 VaadinSession (com.vaadin.flow.server.VaadinSession)8 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)7 Document (org.jsoup.nodes.Document)7 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinService (com.vaadin.flow.server.VaadinService)6 Element (org.jsoup.nodes.Element)6 UI (com.vaadin.flow.component.UI)3 Location (com.vaadin.flow.router.Location)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Optional (java.util.Optional)3 AbstractDevModeTest (com.vaadin.base.devserver.startup.AbstractDevModeTest)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)2