From b35b5723aeb7c125e1d7d9e0e26112ce1956c097 Mon Sep 17 00:00:00 2001 From: haijian Date: Tue, 24 Nov 2020 10:58:57 +0200 Subject: [PATCH 1/2] fix: remove instance field from singleton IndexHtmlRequestHandler --- .../IndexHtmlRequestHandler.java | 6 +--- .../IndexHtmlRequestHandlerTest.java | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java index d3939329184..df796eaa61d 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java @@ -59,12 +59,11 @@ */ public class IndexHtmlRequestHandler extends JavaScriptBootstrapHandler { - private transient IndexHtmlResponse indexHtmlResponse; - @Override public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { DeploymentConfiguration config = session.getConfiguration(); + IndexHtmlResponse indexHtmlResponse = null; Document indexDocument = config.isProductionMode() ? getCachedIndexHtmlDocument(request.getService()) @@ -271,7 +270,4 @@ private static Logger getLogger() { return LoggerFactory.getLogger(IndexHtmlRequestHandler.class); } - protected IndexHtmlResponse getIndexHtmlResponse() { - return this.indexHtmlResponse; - } } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java index db92a5661ab..fffce55a0d8 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java @@ -37,6 +37,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import com.vaadin.flow.component.UI; @@ -45,6 +46,7 @@ import com.vaadin.flow.server.AppShellRegistry; import com.vaadin.flow.server.DevModeHandler; import com.vaadin.flow.server.MockServletServiceSessionSetup; +import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinResponse; import com.vaadin.flow.server.VaadinServletRequest; import com.vaadin.flow.server.VaadinServletService; @@ -61,6 +63,7 @@ import static com.vaadin.flow.server.frontend.FrontendUtils.DEFAULT_FRONTEND_DIR; import static com.vaadin.flow.server.frontend.NodeUpdateTestUtil.createStubWebpackServer; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; public class IndexHtmlRequestHandlerTest { private MockServletServiceSessionSetup mocks; @@ -344,21 +347,34 @@ public void should_getter_UI_return_not_empty_when_includeInitialBootstrapUidl() throws IOException { deploymentConfiguration.setEagerServerLoad(true); - indexHtmlRequestHandler.synchronizedHandleRequest(session, - createVaadinRequest("/"), response); + VaadinRequest request = createVaadinRequest("/"); - Assert.assertNotNull( - indexHtmlRequestHandler.getIndexHtmlResponse().getUI()); + indexHtmlRequestHandler.synchronizedHandleRequest(session, + + request, response); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(IndexHtmlResponse.class); + + verify(request.getService()).modifyIndexHtmlResponse(captor.capture()); + + Assert.assertNotNull(captor.getValue().getUI()); } @Test public void should_getter_UI_return_empty_when_not_includeInitialBootstrapUidl() throws IOException { - indexHtmlRequestHandler.synchronizedHandleRequest(session, - createVaadinRequest("/"), response); + VaadinRequest request = createVaadinRequest("/"); - Assert.assertEquals(Optional.empty(), - indexHtmlRequestHandler.getIndexHtmlResponse().getUI()); + indexHtmlRequestHandler.synchronizedHandleRequest(session, + request, response); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(IndexHtmlResponse.class); + + verify(request.getService()).modifyIndexHtmlResponse(captor.capture()); + + Assert.assertEquals(Optional.empty(), captor.getValue().getUI()); } @Test @@ -632,7 +648,7 @@ public void tearDown() throws Exception { private VaadinServletRequest createVaadinRequest(String pathInfo) { HttpServletRequest request = createRequest(pathInfo); - return new VaadinServletRequest(request, service); + return new VaadinServletRequest(request, Mockito.spy(service)); } private HttpServletRequest createRequest(String pathInfo) { From cd8d1ff9250d8f870baae66245125a3856e6c671 Mon Sep 17 00:00:00 2001 From: haijian Date: Tue, 24 Nov 2020 17:24:58 +0200 Subject: [PATCH 2/2] apply code review suggestions --- .../flow/server/communication/IndexHtmlRequestHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java index df796eaa61d..852ccedfc5e 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java @@ -63,7 +63,7 @@ public class IndexHtmlRequestHandler extends JavaScriptBootstrapHandler { public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { DeploymentConfiguration config = session.getConfiguration(); - IndexHtmlResponse indexHtmlResponse = null; + IndexHtmlResponse indexHtmlResponse; Document indexDocument = config.isProductionMode() ? getCachedIndexHtmlDocument(request.getService())