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);
}
}
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));
}
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));
}
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")));
}
Aggregations