|
| 1 | +package com.vaadin.base.devserver; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.HttpURLConnection; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CompletableFuture; |
| 10 | +import java.util.concurrent.atomic.AtomicReference; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +import javax.servlet.ServletOutputStream; |
| 14 | +import javax.servlet.http.HttpServletRequest; |
| 15 | +import javax.servlet.http.HttpServletResponse; |
| 16 | + |
| 17 | +import com.vaadin.base.devserver.startup.AbstractDevModeTest; |
| 18 | +import com.vaadin.flow.internal.DevModeHandler; |
| 19 | + |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | +import org.mockito.Mockito; |
| 23 | + |
| 24 | +public class AbstractDevServerRunnerTest extends AbstractDevModeTest { |
| 25 | + |
| 26 | + private class DummyRunner extends AbstractDevServerRunner { |
| 27 | + |
| 28 | + protected DummyRunner() { |
| 29 | + super(lookup, 0, npmFolder, |
| 30 | + CompletableFuture.completedFuture(null)); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected File getServerBinary() { |
| 35 | + return new File("dummy.bin"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected File getServerConfig() { |
| 40 | + return new File("dummy.config"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected String getServerName() { |
| 45 | + return "Dummy server"; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected List<String> getServerStartupCommand(String nodeExec) { |
| 50 | + List<String> commands = new ArrayList<>(); |
| 51 | + commands.add("echo"); |
| 52 | + return commands; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Pattern getServerSuccessPattern() { |
| 57 | + return Pattern.compile("Dummy success"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected Pattern getServerFailurePattern() { |
| 62 | + return Pattern.compile("Dummy fail"); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected boolean checkConnection() { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public HttpURLConnection prepareConnection(String path, String method) |
| 72 | + throws IOException { |
| 73 | + return Mockito.mock(HttpURLConnection.class); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldPassEncodedUrlToDevServer() throws Exception { |
| 80 | + handler = new DummyRunner(); |
| 81 | + DevModeHandler devServer = Mockito.spy(handler); |
| 82 | + |
| 83 | + HttpServletResponse response = Mockito.mock(HttpServletResponse.class); |
| 84 | + Mockito.when(response.getOutputStream()) |
| 85 | + .thenReturn(Mockito.mock(ServletOutputStream.class)); |
| 86 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 87 | + Mockito.when(request.getRequestURI()).thenReturn("/foo%20bar"); |
| 88 | + Mockito.when(request.getPathInfo()).thenReturn("foo bar"); |
| 89 | + Mockito.when(request.getHeaderNames()) |
| 90 | + .thenReturn(Collections.emptyEnumeration()); |
| 91 | + |
| 92 | + AtomicReference<String> requestedPath = new AtomicReference<>(); |
| 93 | + Mockito.when(devServer.prepareConnection(Mockito.any(), Mockito.any())) |
| 94 | + .then(invocation -> { |
| 95 | + requestedPath.set((String) invocation.getArguments()[0]); |
| 96 | + return Mockito.mock(HttpURLConnection.class); |
| 97 | + }); |
| 98 | + devServer.serveDevModeRequest(request, response); |
| 99 | + Assert.assertEquals("foo%20bar", requestedPath.get()); |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments