use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class InternalErrorView method showCriticalNotification.
protected void showCriticalNotification(String caption, String message, String details, String url) {
VaadinService service = VaadinService.getCurrent();
VaadinResponse response = VaadinService.getCurrentResponse();
try {
service.writeUncachedStringResponse(response, JsonConstants.JSON_CONTENT_TYPE, VaadinService.createCriticalNotificationJSON(caption, message, details, url));
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class CsrfIndexHtmlRequestListenerTest method setup.
@Before
public void setup() {
csrfIndexHtmlRequestListener = Mockito.spy(new CsrfIndexHtmlRequestListener());
vaadinRequest = Mockito.mock(VaadinRequest.class);
Mockito.doReturn(TEST_CONTEXT_PATH).when(vaadinRequest).getContextPath();
Mockito.doReturn(true).when(vaadinRequest).isSecure();
VaadinResponse vaadinResponse = Mockito.mock(VaadinResponse.class);
Document document = Mockito.mock(Document.class);
indexHtmlResponse = new IndexHtmlResponse(vaadinRequest, vaadinResponse, document);
}
use of com.vaadin.flow.server.VaadinResponse 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.VaadinResponse 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.VaadinResponse in project flow by vaadin.
the class Router method initializeUI.
@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
Location location = getLocationForRequest(initRequest.getPathInfo(), initRequest.getParameterMap());
ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger()));
int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
VaadinResponse response = VaadinService.getCurrentResponse();
if (response != null) {
response.setStatus(statusCode);
}
}
Aggregations