use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class LegacyPropertyEnabledTestServlet method testValuesFromAnnotation.
@Test
public void testValuesFromAnnotation() throws ServletException {
TestServlet servlet = new TestServlet();
servlet.init(new MockServletConfig());
DeploymentConfiguration configuration = servlet.getService().getDeploymentConfiguration();
Assert.assertEquals(true, configuration.isProductionMode());
Assert.assertEquals(true, configuration.isCloseIdleSessions());
Assert.assertEquals(1234, configuration.getHeartbeatInterval());
Class<? extends UI> uiClass = BootstrapHandler.getUIClass(new VaadinServletRequest(EasyMock.createMock(HttpServletRequest.class), servlet.getService()));
Assert.assertEquals(MockUIContainingServlet.class, uiClass);
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class LegacyPropertyEnabledTestServlet method testValuesOverriddenForServlet.
@Test
public void testValuesOverriddenForServlet() throws ServletException {
final boolean expectedBoolean = false;
final int expectedInt = 1111;
Properties servletInitParams = new Properties();
servletInitParams.setProperty("sendUrlsAsParameters", Boolean.toString(expectedBoolean));
servletInitParams.setProperty("heartbeatInterval", Integer.toString(expectedInt));
TestServlet servlet = new TestServlet();
servlet.init(new MockServletConfig(servletInitParams));
DeploymentConfiguration configuration = servlet.getService().getDeploymentConfiguration();
// Values from servlet init params take precedence
Assert.assertEquals(expectedBoolean, configuration.isSendUrlsAsParameters());
Assert.assertEquals(expectedInt, configuration.getHeartbeatInterval());
// Other params are as defined in the annotation
Assert.assertEquals(true, configuration.isCloseIdleSessions());
Class<? extends UI> uiClass = BootstrapHandler.getUIClass(new VaadinServletRequest(EasyMock.createMock(HttpServletRequest.class), servlet.getService()));
Assert.assertEquals(MockUIContainingServlet.class, uiClass);
}
use of com.vaadin.flow.server.VaadinServletRequest 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);
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class DefaultTemplateParserTest method defaultParser_servletPathIsNotEmpty_doubleSlashIsRemovedFromRequest.
@Test
public void defaultParser_servletPathIsNotEmpty_doubleSlashIsRemovedFromRequest() {
VaadinServletRequest request = (VaadinServletRequest) CurrentInstance.get(VaadinRequest.class);
Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("/./../bar.html");
Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("/./../bar1.html");
Mockito.when(request.getServletPath()).thenReturn("/run/");
Mockito.when(servletContext.getResourceAsStream("/run/./../bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Mockito.when(servletContext.getResourceAsStream("/run/./../bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Element element = DefaultTemplateParser.getInstance().getTemplateContent(ImportsInspectTemplate.class, "foo").getTemplateElement();
Assert.assertTrue(element.getElementById("foo") != null);
// The double slash should be ignored e.g. `/run//./..` should become
// `/run/./..`
Mockito.verify(servletContext).getResourceAsStream("/run/./../bar.html");
Mockito.verify(servletContext).getResourceAsStream("/run/./../bar1.html");
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class UITest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.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);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(RootNavigationTarget.class, FooBarNavigationTarget.class, Parameterized.class, FooBarParamNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
Aggregations