Search in sources :

Example 21 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class LegacyPropertyEnabledTestServlet method testValuesFromAnnotation.

@Test
public void testValuesFromAnnotation() throws ServletException {
    TestServlet servlet = new TestServlet();
    servlet.init(new MockServletConfig());
    DeploymentConfiguration configuration = servlet.getService().getDeploymentConfiguration();
    Assert.assertEquals(true, configuration.isProductionMode());
    Assert.assertEquals(true, configuration.isCloseIdleSessions());
    Assert.assertEquals(1234, configuration.getHeartbeatInterval());
    Class<? extends UI> uiClass = BootstrapHandler.getUIClass(new VaadinServletRequest(EasyMock.createMock(HttpServletRequest.class), servlet.getService()));
    Assert.assertEquals(MockUIContainingServlet.class, uiClass);
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 22 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class LegacyPropertyEnabledTestServlet method testValuesOverriddenForServlet.

@Test
public void testValuesOverriddenForServlet() throws ServletException {
    final boolean expectedBoolean = false;
    final int expectedInt = 1111;
    Properties servletInitParams = new Properties();
    servletInitParams.setProperty("sendUrlsAsParameters", Boolean.toString(expectedBoolean));
    servletInitParams.setProperty("heartbeatInterval", Integer.toString(expectedInt));
    TestServlet servlet = new TestServlet();
    servlet.init(new MockServletConfig(servletInitParams));
    DeploymentConfiguration configuration = servlet.getService().getDeploymentConfiguration();
    // Values from servlet init params take precedence
    Assert.assertEquals(expectedBoolean, configuration.isSendUrlsAsParameters());
    Assert.assertEquals(expectedInt, configuration.getHeartbeatInterval());
    // Other params are as defined in the annotation
    Assert.assertEquals(true, configuration.isCloseIdleSessions());
    Class<? extends UI> uiClass = BootstrapHandler.getUIClass(new VaadinServletRequest(EasyMock.createMock(HttpServletRequest.class), servlet.getService()));
    Assert.assertEquals(MockUIContainingServlet.class, uiClass);
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Properties(java.util.Properties) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Example 23 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class DefaultTemplateParserTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
    HttpSession httpSession = Mockito.mock(HttpSession.class);
    Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return service;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(service.getServlet()).thenReturn(servlet);
    Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
    Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
    Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
    Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
    Mockito.when(request.getServletPath()).thenReturn("");
    Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
    CurrentInstance.set(VaadinRequest.class, request);
    CurrentInstance.set(VaadinSession.class, session);
    CurrentInstance.set(VaadinService.class, service);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletException(javax.servlet.ServletException) VaadinSession(com.vaadin.flow.server.VaadinSession) ServiceException(com.vaadin.flow.server.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) Before(org.junit.Before)

Example 24 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class DefaultTemplateParserTest method defaultParser_servletPathIsNotEmpty_doubleSlashIsRemovedFromRequest.

@Test
public void defaultParser_servletPathIsNotEmpty_doubleSlashIsRemovedFromRequest() {
    VaadinServletRequest request = (VaadinServletRequest) CurrentInstance.get(VaadinRequest.class);
    Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("/./../bar.html");
    Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("/./../bar1.html");
    Mockito.when(request.getServletPath()).thenReturn("/run/");
    Mockito.when(servletContext.getResourceAsStream("/run/./../bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/run/./../bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Element element = DefaultTemplateParser.getInstance().getTemplateContent(ImportsInspectTemplate.class, "foo").getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    // The double slash should be ignored e.g. `/run//./..` should become
    // `/run/./..`
    Mockito.verify(servletContext).getResourceAsStream("/run/./../bar.html");
    Mockito.verify(servletContext).getResourceAsStream("/run/./../bar1.html");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.jsoup.nodes.Element) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Test(org.junit.Test)

Example 25 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest 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)

Aggregations

VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)40 Test (org.junit.Test)19 VaadinSession (com.vaadin.flow.server.VaadinSession)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 VaadinServletService (com.vaadin.flow.server.VaadinServletService)8 IOException (java.io.IOException)7 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)6 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinRequest (com.vaadin.flow.server.VaadinRequest)6 VaadinResponse (com.vaadin.flow.server.VaadinResponse)6 UI (com.vaadin.flow.component.UI)5 Element (org.jsoup.nodes.Element)5 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)3 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)3 VaadinService (com.vaadin.flow.server.VaadinService)3 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 JsonException (elemental.json.JsonException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3