Skip to content

Commit

Permalink
fix: remove instance field from singleton IndexHtmlRequestHandler (#9475
Browse files Browse the repository at this point in the history
)

* fix: remove instance field from singleton IndexHtmlRequestHandler

* apply code review suggestions
  • Loading branch information
haijian-vaadin committed Nov 26, 2020
1 parent cd45478 commit 8332afa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Document indexDocument = config.isProductionMode()
? getCachedIndexHtmlDocument(request.getService())
Expand Down Expand Up @@ -271,7 +270,4 @@ private static Logger getLogger() {
return LoggerFactory.getLogger(IndexHtmlRequestHandler.class);
}

protected IndexHtmlResponse getIndexHtmlResponse() {
return this.indexHtmlResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<IndexHtmlResponse> 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<IndexHtmlResponse> captor =
ArgumentCaptor.forClass(IndexHtmlResponse.class);

verify(request.getService()).modifyIndexHtmlResponse(captor.capture());

Assert.assertEquals(Optional.empty(), captor.getValue().getUI());
}

@Test
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8332afa

Please sign in to comment.