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