Search in sources :

Example 31 with VaadinServletRequest

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

the class StreamReceiverHandlerTest method doHandleMultipartFileUpload_IOExceptionIsThrown_exceptionIsNotRethrown_exceptionIsNotHandlerByErrorHandler.

@Test
public void doHandleMultipartFileUpload_IOExceptionIsThrown_exceptionIsNotRethrown_exceptionIsNotHandlerByErrorHandler() throws IOException, ServletException {
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    Mockito.doThrow(IOException.class).when(request).getParts();
    handler.doHandleMultipartFileUpload(session, request, response, streamReceiver, stateNode);
    Mockito.verifyNoInteractions(errorHandler);
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Test(org.junit.Test)

Example 32 with VaadinServletRequest

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

the class ViewAccessCheckerTest method setupRequest.

private Result setupRequest(Class navigationTarget, User user, boolean productionMode) {
    CurrentInstance.clearAll();
    Principal principal;
    String[] roles;
    if (user == User.USER_NO_ROLES) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[0];
    } else if (user == User.NORMAL_USER) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "user" };
    } else if (user == User.ADMIN) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "admin" };
    } else {
        principal = null;
        roles = new String[0];
    }
    VaadinServletRequest vaadinServletRequest = Mockito.mock(VaadinServletRequest.class);
    HttpServletRequest httpServletRequest = AccessAnnotationCheckerTest.createRequest(principal, roles);
    Mockito.when(vaadinServletRequest.getHttpServletRequest()).thenReturn(httpServletRequest);
    CurrentInstance.set(VaadinRequest.class, vaadinServletRequest);
    Router router = Mockito.mock(Router.class);
    UI ui = Mockito.mock(UI.class);
    Page page = Mockito.mock(Page.class);
    Mockito.when(ui.getPage()).thenReturn(page);
    VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
    Mockito.when(ui.getSession()).thenReturn(vaadinSession);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(vaadinSession.getConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
    UIInternals uiInternals = Mockito.mock(UIInternals.class);
    Mockito.when(ui.getInternals()).thenReturn(uiInternals);
    Mockito.when(uiInternals.getRouter()).thenReturn(router);
    Mockito.when(router.getErrorNavigationTarget(Mockito.any())).thenAnswer(invocation -> {
        Class<?> exceptionClass = invocation.getArguments()[0].getClass();
        if (exceptionClass == NotFoundException.class) {
            return Optional.of(new ErrorTargetEntry(RouteNotFoundError.class, NotFoundException.class));
        } else {
            return Optional.empty();
        }
    });
    Location location = new Location(getRoute(navigationTarget));
    NavigationEvent navigationEvent = new NavigationEvent(router, location, ui, NavigationTrigger.ROUTER_LINK);
    BeforeEnterEvent event = new BeforeEnterEvent(navigationEvent, navigationTarget, new ArrayList<>());
    RouteRegistry routeRegistry = Mockito.mock(RouteRegistry.class);
    Mockito.when(router.getRegistry()).thenReturn(routeRegistry);
    Mockito.when(routeRegistry.getNavigationTarget(Mockito.anyString())).thenAnswer(invocation -> {
        String url = (String) invocation.getArguments()[0];
        if (location.getPath().equals(url)) {
            return Optional.of(navigationTarget);
        } else {
            return Optional.empty();
        }
    });
    HttpSession session = Mockito.mock(HttpSession.class);
    Map<String, Object> sessionAttributes = new HashMap<>();
    Mockito.when(httpServletRequest.getSession()).thenReturn(session);
    Mockito.doAnswer(invocation -> {
        String key = (String) invocation.getArguments()[0];
        Object value = invocation.getArguments()[1];
        sessionAttributes.put(key, value);
        return null;
    }).when(session).setAttribute(Mockito.anyString(), Mockito.any());
    Result info = new Result();
    info.event = event;
    info.sessionAttributes = sessionAttributes;
    Mockito.doAnswer(invocation -> {
        info.redirectUsingPageLocation = (String) invocation.getArguments()[0];
        return null;
    }).when(page).setLocation(Mockito.anyString());
    return info;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) RouteRegistry(com.vaadin.flow.server.RouteRegistry) HashMap(java.util.HashMap) NotFoundException(com.vaadin.flow.router.NotFoundException) Page(com.vaadin.flow.component.page.Page) HttpServletRequest(javax.servlet.http.HttpServletRequest) UI(com.vaadin.flow.component.UI) NavigationEvent(com.vaadin.flow.router.NavigationEvent) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Router(com.vaadin.flow.router.Router) UIInternals(com.vaadin.flow.component.internal.UIInternals) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) RouteNotFoundError(com.vaadin.flow.router.RouteNotFoundError) Principal(java.security.Principal) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Location(com.vaadin.flow.router.Location)

Example 33 with VaadinServletRequest

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

the class IndexHtmlRequestHandlerTest method serveNotFoundIndexHtml_requestWithRootPath_failsWithIOException.

@Test
public void serveNotFoundIndexHtml_requestWithRootPath_failsWithIOException() throws IOException {
    VaadinServletService vaadinService = Mockito.mock(VaadinServletService.class);
    Mockito.when(vaadinService.getDeploymentConfiguration()).thenReturn(deploymentConfiguration);
    Mockito.when(vaadinService.getContext()).thenReturn(context);
    final Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    URL resource = Mockito.mock(URL.class);
    Mockito.when(resourceProvider.getApplicationResource(VAADIN_WEBAPP_RESOURCES + INDEX_HTML)).thenReturn(resource);
    when(resource.openStream()).thenReturn(null);
    VaadinServletRequest vaadinRequest = Mockito.mock(VaadinServletRequest.class);
    Mockito.when(vaadinRequest.getService()).thenReturn(vaadinService);
    String path = DEFAULT_FRONTEND_DIR + "index.html";
    String expectedError = String.format("Failed to load content of '%1$s'. " + "It is required to have '%1$s' file when " + "using client side bootstrapping.", path);
    exceptionRule.expect(IOException.class);
    exceptionRule.expectMessage(expectedError);
    indexHtmlRequestHandler.synchronizedHandleRequest(session, vaadinRequest, response);
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Lookup(com.vaadin.flow.di.Lookup) URL(java.net.URL) Test(org.junit.Test)

Example 34 with VaadinServletRequest

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

the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noExportChunk.

@Test
public void writeBootstrapPage_noExportChunk() 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.setApplicationOrSystemProperty(SERVLET_PARAMETER_STATISTICS_JSON, VAADIN_SERVLET_RESOURCES + "config/stats_no_export.json");
    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);
    // no "export" chunk, expect "bundle" in result instead
    String result = stream.toString(StandardCharsets.UTF_8.name());
    Assert.assertTrue(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 35 with VaadinServletRequest

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

the class AccessAnnotationCheckerTest method hasClassAccessUsingCurrentRequest.

@Test
public void hasClassAccessUsingCurrentRequest() {
    try {
        CurrentInstance.set(VaadinRequest.class, new VaadinServletRequest(createRequest(USER_PRINCIPAL), null));
        Assert.assertTrue(accessAnnotationChecker.hasAccess(PermitAllClass.class));
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) PermitAllClass(com.vaadin.flow.server.auth.AccessControlTestClasses.PermitAllClass) 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