use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class StreamReceiverHandlerTest method mockRequest.
private void mockRequest() throws IOException {
HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
when(servletRequest.getContentLength()).thenAnswer(invocationOnMock -> Integer.parseInt(contentLength));
request = new VaadinServletRequest(servletRequest, mockService) {
@Override
public String getParameter(String name) {
if ("restartApplication".equals(name) || "ignoreRestart".equals(name) || "closeApplication".equals(name)) {
return null;
}
return "1";
}
@Override
public String getPathInfo() {
return "/" + StreamRequestHandler.DYN_RES_PREFIX + uiId + "/" + nodeId + "/" + variableName + "/" + expectedSecurityKey;
}
@Override
public String getMethod() {
return "POST";
}
@Override
public ServletInputStream getInputStream() throws IOException {
return inputStream;
}
@Override
public String getHeader(String name) {
if ("content-length".equals(name.toLowerCase())) {
return contentLength;
}
return super.getHeader(name);
}
@Override
public String getContentType() {
return contentType;
}
@Override
public Collection<Part> getParts() throws IOException, ServletException {
return parts;
}
@Override
public long getContentLengthLong() {
isGetContentLengthLongCalled = true;
return 0;
}
};
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class UidlWriterTest method initializeUIForDependenciesTest.
private UI initializeUIForDependenciesTest(UI ui) throws Exception {
mocks = new MockServletServiceSessionSetup();
VaadinServletContext context = (VaadinServletContext) mocks.getService().getContext();
Lookup lookup = context.getAttribute(Lookup.class);
Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(new RoutePathProviderImpl());
VaadinSession session = mocks.getSession();
session.lock();
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
routeConfiguration.setAnnotatedRoute(BaseClass.class);
});
for (String type : new String[] { "html", "js", "css" }) {
mocks.getServlet().addServletContextResource("inline." + type, "inline." + type);
}
HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
ui.doInit(vaadinRequestMock, 1);
ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(vaadinRequestMock));
return ui;
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noPWA.
@Test
public void writeBootstrapPage_noPWA() throws IOException, ServiceException {
TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
PwaRegistry registry = Mockito.mock(PwaRegistry.class);
PwaConfiguration conf = Mockito.mock(PwaConfiguration.class);
Mockito.when(registry.getPwaConfiguration()).thenReturn(conf);
Mockito.when(conf.isEnabled()).thenReturn(true);
Mockito.when(conf.getManifestPath()).thenReturn("bar");
PwaIcon icon = Mockito.mock(PwaIcon.class);
Mockito.when(icon.asElement()).thenReturn(new Element("h1"));
Mockito.when(registry.getHeaderIcons()).thenReturn(Collections.singletonList(icon));
VaadinServletService service = new MockVaadinServletService() {
@Override
protected PwaRegistry getPwaRegistry() {
return registry;
}
};
initLookup(service);
VaadinSession session = new MockVaadinSession(service);
session.lock();
session.setConfiguration(service.getDeploymentConfiguration());
MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
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);
String result = stream.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("bar")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("h1")));
MatcherAssert.assertThat(result, CoreMatchers.not(CoreMatchers.containsString("baz")));
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_withExportChunk.
@Test
public void writeBootstrapPage_withExportChunk() 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.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);
String result = stream.toString(StandardCharsets.UTF_8.name());
Assert.assertTrue(result.contains("VAADIN/build/vaadin-export-2222.cache.js"));
Assert.assertFalse(result.contains("VAADIN/build/vaadin-bundle-1111.cache.js"));
}
use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_scriptGuadedAndGizmoDisabled.
@Test
public void writeBootstrapPage_scriptGuadedAndGizmoDisabled() 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.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);
String result = stream.toString(StandardCharsets.UTF_8.name());
int scriptIndex = result.indexOf("var hasScript = function(src)");
Assert.assertTrue(scriptIndex >= 0);
int guardIndex = result.indexOf("if (!hasScript(\"/VAADIN/build/vaadin-export-2222.cache.js\")) {");
Assert.assertTrue(guardIndex > scriptIndex);
int createScriptIndex = result.indexOf("document.createElement('script')");
Assert.assertTrue(createScriptIndex > guardIndex);
Assert.assertTrue(result.contains("\\\"devmodeGizmoEnabled\\\": false"));
}
Aggregations