Search in sources :

Example 11 with VaadinResponse

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

the class InternalErrorView method showCriticalNotification.

protected void showCriticalNotification(String caption, String message, String details, String url) {
    VaadinService service = VaadinService.getCurrent();
    VaadinResponse response = VaadinService.getCurrentResponse();
    try {
        service.writeUncachedStringResponse(response, JsonConstants.JSON_CONTENT_TYPE, VaadinService.createCriticalNotificationJSON(caption, message, details, url));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinService(com.vaadin.flow.server.VaadinService) IOException(java.io.IOException)

Example 12 with VaadinResponse

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

the class CsrfIndexHtmlRequestListenerTest method setup.

@Before
public void setup() {
    csrfIndexHtmlRequestListener = Mockito.spy(new CsrfIndexHtmlRequestListener());
    vaadinRequest = Mockito.mock(VaadinRequest.class);
    Mockito.doReturn(TEST_CONTEXT_PATH).when(vaadinRequest).getContextPath();
    Mockito.doReturn(true).when(vaadinRequest).isSecure();
    VaadinResponse vaadinResponse = Mockito.mock(VaadinResponse.class);
    Document document = Mockito.mock(Document.class);
    indexHtmlResponse = new IndexHtmlResponse(vaadinRequest, vaadinResponse, document);
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) IndexHtmlResponse(com.vaadin.flow.server.communication.IndexHtmlResponse) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Document(org.jsoup.nodes.Document) Before(org.junit.Before)

Example 13 with VaadinResponse

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

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    Router router = new Router();
    router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 14 with VaadinResponse

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

the class RouterTest method testStatusCodeUpdates.

@Test
public void testStatusCodeUpdates() {
    RouterTestUI ui = new RouterTestUI();
    Router router = (Router) ui.getRouterInterface().get();
    router.reconfigure(c -> {
        c.setRoute("*", e -> 123);
    });
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    try {
        CurrentInstance.set(VaadinResponse.class, response);
        VaadinRequest request = requestWithPathInfo(null);
        router.initializeUI(ui, request);
        // Response status should be set when initializing
        Mockito.verify(response).setStatus(123);
        router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
        // Non-init navigation shouldn't set any status code
        Mockito.verifyNoMoreInteractions(response);
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) Router(com.vaadin.flow.router.legacy.Router) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 15 with VaadinResponse

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

the class Router method initializeUI.

@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
    Location location = getLocationForRequest(initRequest.getPathInfo(), initRequest.getParameterMap());
    ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger()));
    int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
    VaadinResponse response = VaadinService.getCurrentResponse();
    if (response != null) {
        response.setStatus(statusCode);
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse)

Aggregations

VaadinResponse (com.vaadin.flow.server.VaadinResponse)24 Test (org.junit.Test)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 VaadinRequest (com.vaadin.flow.server.VaadinRequest)8 VaadinSession (com.vaadin.flow.server.VaadinSession)8 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)7 Document (org.jsoup.nodes.Document)7 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinService (com.vaadin.flow.server.VaadinService)6 Element (org.jsoup.nodes.Element)6 UI (com.vaadin.flow.component.UI)3 Location (com.vaadin.flow.router.Location)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Optional (java.util.Optional)3 AbstractDevModeTest (com.vaadin.base.devserver.startup.AbstractDevModeTest)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)2