Search in sources :

Example 6 with VaadinContext

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

the class PushAtmosphereHandlerTest method setup.

@Before
public void setup() throws IOException {
    request = Mockito.mock(AtmosphereRequest.class);
    response = Mockito.mock(AtmosphereResponse.class);
    printWriter = Mockito.mock(PrintWriter.class);
    Mockito.when(response.getWriter()).thenReturn(printWriter);
    resource = Mockito.mock(AtmosphereResource.class);
    Mockito.when(resource.getRequest()).thenReturn(request);
    Mockito.when(resource.getResponse()).thenReturn(response);
    VaadinContext context = new MockVaadinContext();
    ApplicationConfiguration config = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(config.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(config.getContext()).thenReturn(context);
    VaadinServletService service = new VaadinServletService(null, new DefaultDeploymentConfiguration(config, getClass(), new Properties()));
    PushHandler handler = new PushHandler(service);
    atmosphereHandler = new PushAtmosphereHandler();
    atmosphereHandler.setPushHandler(handler);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) PrintWriter(java.io.PrintWriter) Before(org.junit.Before)

Example 7 with VaadinContext

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

the class IndexHtmlRequestHandler method synchronizedHandleRequest.

@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    if (writeErrorCodeIfRequestLocationIsInvalid(request, response)) {
        return true;
    }
    DeploymentConfiguration config = session.getConfiguration();
    IndexHtmlResponse indexHtmlResponse;
    Document indexDocument = config.isProductionMode() ? getCachedIndexHtmlDocument(request.getService()) : getIndexHtmlDocument(request.getService());
    prependBaseHref(request, indexDocument);
    JsonObject initialJson = Json.createObject();
    if (request.getService().getBootstrapInitialPredicate().includeInitialUidl(request)) {
        includeInitialUidl(initialJson, session, request, response);
        indexHtmlResponse = new IndexHtmlResponse(request, response, indexDocument, UI.getCurrent());
        // App might be using classic server-routing, which is true
        // unless we detect a call to JavaScriptBootstrapUI.connectClient
        session.setAttribute(SERVER_ROUTING, Boolean.TRUE);
    } else {
        indexHtmlResponse = new IndexHtmlResponse(request, response, indexDocument);
    }
    addInitialFlow(initialJson, indexDocument, request);
    configureErrorDialogStyles(indexDocument);
    showWebpackErrors(session.getService(), indexDocument);
    response.setContentType(CONTENT_TYPE_TEXT_HTML_UTF_8);
    VaadinContext context = session.getService().getContext();
    AppShellRegistry registry = AppShellRegistry.getInstance(context);
    if (!config.isProductionMode()) {
        UsageStatisticsExporter.exportUsageStatisticsToDocument(indexDocument);
    }
    // modify the page based on the @PWA annotation
    setupPwa(indexDocument, session.getService());
    // modify the page based on the @Meta, @ViewPort, @BodySize and @Inline
    // annotations
    // and on the AppShellConfigurator
    registry.modifyIndexHtml(indexDocument, request);
    // the bootstrap page title could be used as a fallback title to
    // a server-side route that doesn't have a title
    storeAppShellTitleToUI(indexDocument);
    // modify the page based on registered IndexHtmlRequestListener:s
    request.getService().modifyIndexHtmlResponse(indexHtmlResponse);
    if (config.isDevModeGizmoEnabled()) {
        addDevmodeGizmo(indexDocument, config, session, request);
        catchErrorsInDevMode(indexDocument);
    }
    try {
        response.getOutputStream().write(indexDocument.html().getBytes(UTF_8));
    } catch (IOException e) {
        getLogger().error("Error writing 'index.html' to response", e);
        return false;
    }
    return true;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) JsonObject(elemental.json.JsonObject) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.jsoup.nodes.Document) AppShellRegistry(com.vaadin.flow.server.AppShellRegistry) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 8 with VaadinContext

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

the class PushHandlerTest method mockBrowserLiveReloadImpl.

public static BrowserLiveReload mockBrowserLiveReloadImpl(VaadinContext context) {
    BrowserLiveReload liveReload = Mockito.mock(BrowserLiveReload.class);
    Lookup lookup = Lookup.of(new BrowserLiveReloadAccessor() {

        @Override
        public BrowserLiveReload getLiveReload(VaadinContext context) {
            return liveReload;
        }
    }, BrowserLiveReloadAccessor.class);
    context.setAttribute(Lookup.class, lookup);
    return liveReload;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) BrowserLiveReload(com.vaadin.flow.internal.BrowserLiveReload) Lookup(com.vaadin.flow.di.Lookup) BrowserLiveReloadAccessor(com.vaadin.flow.internal.BrowserLiveReloadAccessor)

Example 9 with VaadinContext

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

the class PushHandlerTest method onMessage_devMode_websocket_refreshConnection_callWithUIIsNotCalled.

@Test
public void onMessage_devMode_websocket_refreshConnection_callWithUIIsNotCalled() throws ServiceException {
    MockVaadinServletService service = Mockito.spy(MockVaadinServletService.class);
    MockDeploymentConfiguration deploymentConfiguration = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
    deploymentConfiguration.setProductionMode(false);
    deploymentConfiguration.setDevModeLiveReloadEnabled(true);
    deploymentConfiguration.setDevModeGizmoEnabled(true);
    VaadinContext context = service.getContext();
    mockBrowserLiveReloadImpl(context);
    AtomicReference<AtmosphereResource> res = new AtomicReference<>();
    runTest(service, (handler, resource) -> {
        AtmosphereRequest request = resource.getRequest();
        try {
            Mockito.when(request.getReader()).thenReturn(new BufferedReader(new StringReader("")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Mockito.when(request.getParameter(ApplicationConstants.DEBUG_WINDOW_CONNECTION)).thenReturn("");
        Mockito.when(resource.transport()).thenReturn(TRANSPORT.WEBSOCKET);
        try {
            Mockito.when(request.getReader()).thenReturn(new BufferedReader(new StringReader("{}")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        handler.onMessage(resource);
        res.set(resource);
    });
    Mockito.verify(service, Mockito.times(0)).requestStart(Mockito.any(), Mockito.any());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with VaadinContext

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

the class UidlRequestHandlerTest method writeSessionExpired.

@Test
public void writeSessionExpired() throws Exception {
    ApplicationConfiguration config = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(config.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(config.getBuildFolder()).thenReturn(".");
    VaadinContext context = new MockVaadinContext();
    Mockito.when(config.getContext()).thenReturn(context);
    VaadinService service = new VaadinServletService(null, new DefaultDeploymentConfiguration(config, getClass(), new Properties()));
    when(request.getService()).thenReturn(service);
    when(request.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER)).thenReturn(RequestType.UIDL.getIdentifier());
    boolean result = handler.handleSessionExpired(request, response);
    Assert.assertTrue("Result should be true", result);
    String responseContent = CommunicationUtil.getStringWhenWriteBytesOffsetLength(outputStream);
    // response shouldn't contain async
    Assert.assertEquals("Invalid response", "for(;;);[{\"meta\":{\"sessionExpired\":true}}]", responseContent);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Aggregations

VaadinContext (com.vaadin.flow.server.VaadinContext)42 Test (org.junit.Test)16 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)15 VaadinService (com.vaadin.flow.server.VaadinService)11 Lookup (com.vaadin.flow.di.Lookup)8 ResourceProvider (com.vaadin.flow.di.ResourceProvider)8 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)5 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)5 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 Properties (java.util.Properties)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)4 File (java.io.File)4 Before (org.junit.Before)4 VaadinSession (com.vaadin.flow.server.VaadinSession)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 AppShellRegistry (com.vaadin.flow.server.AppShellRegistry)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)2