use of com.vaadin.flow.server.VaadinRequest 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());
}
use of com.vaadin.flow.server.VaadinRequest 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();
}
}
use of com.vaadin.flow.server.VaadinRequest in project flow by vaadin.
the class UITest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) {
try {
VaadinRequest request = Mockito.mock(VaadinRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
Properties initParams = new Properties();
initParams.setProperty(Constants.SERVLET_PARAMETER_USING_NEW_ROUTING, "false");
ServletConfig servletConfig = new MockServletConfig(initParams);
VaadinServlet servlet = new VaadinServlet();
servlet.init(servletConfig);
VaadinService service = servlet.getService();
service.setCurrentInstances(request, response);
service.getRouter().reconfigure(c -> {
});
MockVaadinSession session = new MockVaadinSession(service);
ui.getInternals().setSession(session);
ui.doInit(request, 0);
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
use of com.vaadin.flow.server.VaadinRequest in project flow by vaadin.
the class DefaultTemplateParserTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinSession session = Mockito.mock(VaadinSession.class);
factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
HttpSession httpSession = Mockito.mock(HttpSession.class);
Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
servlet = new VaadinServlet() {
@Override
protected VaadinServletService createServletService() throws ServletException, ServiceException {
return service;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
};
Mockito.when(service.getServlet()).thenReturn(servlet);
Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
Mockito.when(request.getServletPath()).thenReturn("");
Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
CurrentInstance.set(VaadinRequest.class, request);
CurrentInstance.set(VaadinSession.class, session);
CurrentInstance.set(VaadinService.class, service);
}
Aggregations