Search in sources :

Example 11 with VaadinServletRequest

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

the class StreamReceiverHandlerTest method mockRequest.

private void mockRequest() throws IOException {
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    when(servletRequest.getContentLength()).thenAnswer(invocationOnMock -> Integer.parseInt(contentLength));
    request = new VaadinServletRequest(servletRequest, mockService) {

        @Override
        public String getParameter(String name) {
            if ("restartApplication".equals(name) || "ignoreRestart".equals(name) || "closeApplication".equals(name)) {
                return null;
            }
            return "1";
        }

        @Override
        public String getPathInfo() {
            return "/" + StreamRequestHandler.DYN_RES_PREFIX + uiId + "/" + nodeId + "/" + variableName + "/" + expectedSecurityKey;
        }

        @Override
        public String getMethod() {
            return "POST";
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            return inputStream;
        }

        @Override
        public String getHeader(String name) {
            if ("content-length".equals(name.toLowerCase())) {
                return contentLength;
            }
            return super.getHeader(name);
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public Collection<Part> getParts() throws IOException, ServletException {
            return parts;
        }

        @Override
        public long getContentLengthLong() {
            isGetContentLengthLongCalled = true;
            return 0;
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Collection(java.util.Collection) IOException(java.io.IOException)

Example 12 with VaadinServletRequest

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

the class UidlWriterTest method initializeUIForDependenciesTest.

private UI initializeUIForDependenciesTest(UI ui) throws Exception {
    mocks = new MockServletServiceSessionSetup();
    VaadinServletContext context = (VaadinServletContext) mocks.getService().getContext();
    Lookup lookup = context.getAttribute(Lookup.class);
    Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(new RoutePathProviderImpl());
    VaadinSession session = mocks.getSession();
    session.lock();
    ui.getInternals().setSession(session);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        routeConfiguration.setAnnotatedRoute(BaseClass.class);
    });
    for (String type : new String[] { "html", "js", "css" }) {
        mocks.getServlet().addServletContextResource("inline." + type, "inline." + type);
    }
    HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
    VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
    when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
    ui.doInit(vaadinRequestMock, 1);
    ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(vaadinRequestMock));
    return ui;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinSession(com.vaadin.flow.server.VaadinSession) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) RoutePathProviderImpl(com.vaadin.flow.server.MockVaadinContext.RoutePathProviderImpl) Lookup(com.vaadin.flow.di.Lookup) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup)

Example 13 with VaadinServletRequest

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noPWA.

@Test
public void writeBootstrapPage_noPWA() throws IOException, ServiceException {
    TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
    PwaRegistry registry = Mockito.mock(PwaRegistry.class);
    PwaConfiguration conf = Mockito.mock(PwaConfiguration.class);
    Mockito.when(registry.getPwaConfiguration()).thenReturn(conf);
    Mockito.when(conf.isEnabled()).thenReturn(true);
    Mockito.when(conf.getManifestPath()).thenReturn("bar");
    PwaIcon icon = Mockito.mock(PwaIcon.class);
    Mockito.when(icon.asElement()).thenReturn(new Element("h1"));
    Mockito.when(registry.getHeaderIcons()).thenReturn(Collections.singletonList(icon));
    VaadinServletService service = new MockVaadinServletService() {

        @Override
        protected PwaRegistry getPwaRegistry() {
            return registry;
        }
    };
    initLookup(service);
    VaadinSession session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(service.getDeploymentConfiguration());
    MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    config.setEnableDevServer(false);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(request.getServletPath()).thenReturn("/");
    VaadinResponse response = getMockResponse(null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.synchronizedHandleRequest(session, request, response);
    String result = stream.toString(StandardCharsets.UTF_8.name());
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("bar")));
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("h1")));
    MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("baz")));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Element(org.jsoup.nodes.Element) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PwaIcon(com.vaadin.flow.server.PwaIcon) VaadinResponse(com.vaadin.flow.server.VaadinResponse) PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) PwaRegistry(com.vaadin.flow.server.PwaRegistry) Test(org.junit.Test)

Example 14 with VaadinServletRequest

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_withExportChunk.

@Test
public void writeBootstrapPage_withExportChunk() throws IOException, ServiceException {
    TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
    VaadinServletService service = new MockVaadinServletService();
    initLookup(service);
    VaadinSession session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(service.getDeploymentConfiguration());
    MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    config.setEnableDevServer(false);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(request.getServletPath()).thenReturn("/");
    VaadinResponse response = getMockResponse(null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.synchronizedHandleRequest(session, request, response);
    String result = stream.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(result.contains("VAADIN/build/vaadin-export-2222.cache.js"));
    Assert.assertFalse(result.contains("VAADIN/build/vaadin-bundle-1111.cache.js"));
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 15 with VaadinServletRequest

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_scriptGuadedAndGizmoDisabled.

@Test
public void writeBootstrapPage_scriptGuadedAndGizmoDisabled() throws IOException, ServiceException {
    TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
    VaadinServletService service = new MockVaadinServletService();
    initLookup(service);
    VaadinSession session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(service.getDeploymentConfiguration());
    MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    config.setEnableDevServer(false);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(request.getServletPath()).thenReturn("/");
    VaadinResponse response = getMockResponse(null);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.synchronizedHandleRequest(session, request, response);
    String result = stream.toString(StandardCharsets.UTF_8.name());
    int scriptIndex = result.indexOf("var hasScript = function(src)");
    Assert.assertTrue(scriptIndex >= 0);
    int guardIndex = result.indexOf("if (!hasScript(\"/VAADIN/build/vaadin-export-2222.cache.js\")) {");
    Assert.assertTrue(guardIndex > scriptIndex);
    int createScriptIndex = result.indexOf("document.createElement('script')");
    Assert.assertTrue(createScriptIndex > guardIndex);
    Assert.assertTrue(result.contains("\\\"devmodeGizmoEnabled\\\": false"));
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

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