Search in sources :

Example 1 with PwaRegistry

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

the class PwaHandler method handleRequest.

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    PwaRegistry pwaRegistry = pwaRegistryGetter.get();
    boolean hasPwa = pwaRegistry != null && pwaRegistry.getPwaConfiguration().isEnabled();
    RequestHandler handler = null;
    session.lock();
    try {
        if (isInitialized && !hasPwa) {
            requestHandlerMap.clear();
        } else if (!isInitialized && hasPwa) {
            init(pwaRegistry);
        }
        if (hasPwa) {
            handler = requestHandlerMap.get(request.getPathInfo());
        }
    } finally {
        session.unlock();
    }
    if (handler == null) {
        return false;
    } else {
        return handler.handleRequest(session, request, response);
    }
}
Also used : RequestHandler(com.vaadin.flow.server.RequestHandler) PwaRegistry(com.vaadin.flow.server.PwaRegistry)

Example 2 with PwaRegistry

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

the class PwaHandlerTest method handleRequest_pwaRegistryConfigIsDisabled_returnsFalse.

@Test
public void handleRequest_pwaRegistryConfigIsDisabled_returnsFalse() throws IOException {
    PwaRegistry registry = Mockito.mock(PwaRegistry.class);
    PwaConfiguration configuration = Mockito.mock(PwaConfiguration.class);
    Mockito.when(registry.getPwaConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.isEnabled()).thenReturn(false);
    PwaHandler handler = new PwaHandler(() -> registry);
    Assert.assertFalse(handler.handleRequest(session, request, response));
}
Also used : PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) PwaRegistry(com.vaadin.flow.server.PwaRegistry) Test(org.junit.Test)

Example 3 with PwaRegistry

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

the class PwaHandlerTest method handleRequest_pwaRegistryConfigIsEnabled_pathIsPwaResource_returnsTrue.

@Test
public void handleRequest_pwaRegistryConfigIsEnabled_pathIsPwaResource_returnsTrue() throws IOException {
    PwaRegistry registry = Mockito.mock(PwaRegistry.class);
    PwaConfiguration configuration = Mockito.mock(PwaConfiguration.class);
    Mockito.when(registry.getPwaConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.isEnabled()).thenReturn(true);
    PwaHandler handler = new PwaHandler(() -> registry);
    Mockito.when(response.getWriter()).thenReturn(new PrintWriter(new StringWriter()));
    Mockito.when(registry.getRuntimeServiceWorkerJs()).thenReturn("");
    Mockito.when(request.getPathInfo()).thenReturn("/sw-runtime-resources-precache.js");
    Assert.assertTrue(handler.handleRequest(session, request, response));
}
Also used : PwaConfiguration(com.vaadin.flow.server.PwaConfiguration) StringWriter(java.io.StringWriter) PwaRegistry(com.vaadin.flow.server.PwaRegistry) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 4 with PwaRegistry

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

Aggregations

PwaRegistry (com.vaadin.flow.server.PwaRegistry)4 PwaConfiguration (com.vaadin.flow.server.PwaConfiguration)3 Test (org.junit.Test)3 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 PwaIcon (com.vaadin.flow.server.PwaIcon)1 RequestHandler (com.vaadin.flow.server.RequestHandler)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Element (org.jsoup.nodes.Element)1