FrontendTools { protected List<ProxyConfig.Proxy> getProxies() { File projectNpmrc = new File(baseDir, ".npmrc"); File userNpmrc = new File(FileUtils.getUserDirectory(), ".npmrc"); List<ProxyConfig.Proxy> proxyList = new ArrayList<>(); proxyList.addAll(readProxySettingsFromSystemProperties()); proxyList.addAll( readProxySettingsFromNpmrcFile("user .npmrc", userNpmrc)); proxyList.addAll( readProxySettingsFromNpmrcFile("project .npmrc", projectNpmrc)); proxyList.addAll(readProxySettingsFromEnvironmentVariables()); return proxyList; } FrontendTools(String baseDir,
            Supplier<String> alternativeDirGetter);  FrontendTools(String baseDir,
                         Supplier<String> alternativeDirGetter,
                         String nodeVersion,
                         URI nodeDownloadRoot);   FrontendTools(String baseDir, Supplier<String> alternativeDirGetter,
            String nodeVersion, URI nodeDownloadRoot,
            boolean ignoreVersionChecks); String getNodeExecutable(); String forceAlternativeNodeExecutable(); List<String> getNpmExecutable(); List<String> getPnpmExecutable(); void ensurePnpm(); void validateNodeAndNpmVersion(); static final String DEFAULT_NODE_VERSION; static final String DEFAULT_PNPM_VERSION; static final String INSTALL_NODE_LOCALLY; static final int SUPPORTED_PNPM_MAJOR_VERSION; static final int SUPPORTED_PNPM_MINOR_VERSION; }
@Test public void getProxies_noNpmrc_shouldReturnEmptyList() { File npmrc = new File(baseDir + "/.npmrc"); if (npmrc.exists()) npmrc.delete(); List<ProxyConfig.Proxy> proxyList = tools.getProxies(); Assert.assertTrue(proxyList.isEmpty()); }
@Test public synchronized void getProxies_systemPropertiesAndNpmrcWithProxySetting_shouldReturnAllProxies() throws IOException { File npmrc = new File(tmpDirWithNpmrc.newFolder("test2"), ".npmrc"); FrontendTools tools = new FrontendTools(npmrc.getParent(), null); Properties properties = new Properties(); properties.put(FrontendTools.NPMRC_PROXY_PROPERTY_KEY, "http: properties.put(FrontendTools.NPMRC_HTTPS_PROXY_PROPERTY_KEY, "http: properties.put(FrontendTools.NPMRC_NOPROXY_PROPERTY_KEY, "192.168.1.1,vaadin.com,mycompany.com"); try (FileOutputStream fileOutputStream = new FileOutputStream(npmrc)) { properties.store(fileOutputStream, null); } List<ProxyConfig.Proxy> proxyList = null; try { System.setProperty(FrontendUtils.SYSTEM_NOPROXY_PROPERTY_KEY, "somethingelse,someotherip,75.41.41.33"); System.setProperty(FrontendUtils.SYSTEM_HTTP_PROXY_PROPERTY_KEY, "http: System.setProperty(FrontendUtils.SYSTEM_HTTPS_PROXY_PROPERTY_KEY, "http: proxyList = tools.getProxies(); } finally { System.clearProperty(FrontendUtils.SYSTEM_NOPROXY_PROPERTY_KEY); System.clearProperty(FrontendUtils.SYSTEM_HTTP_PROXY_PROPERTY_KEY); System.clearProperty(FrontendUtils.SYSTEM_HTTPS_PROXY_PROPERTY_KEY); } Assert.assertEquals(4, proxyList.size()); ProxyConfig.Proxy systemHttpsProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(0) : proxyList.get(1); ProxyConfig.Proxy systemProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(1) : proxyList.get(0); ProxyConfig.Proxy npmrcHttpsProxy = proxyList.get(2).id.startsWith( "https-proxy") ? proxyList.get(2) : proxyList.get(3); ProxyConfig.Proxy npmrcProxy = proxyList.get(2).id.startsWith( "https-proxy") ? proxyList.get(3) : proxyList.get(2); Assert.assertEquals("http", systemProxy.protocol); Assert.assertEquals("anotheruser", systemProxy.username); Assert.assertEquals("anotherpassword", systemProxy.password); Assert.assertEquals("aanotherhost", systemProxy.host); Assert.assertEquals(9090, systemProxy.port); Assert.assertEquals("somethingelse|someotherip|75.41.41.33", systemProxy.nonProxyHosts); Assert.assertEquals("http", systemHttpsProxy.protocol); Assert.assertEquals("anotherusers", systemHttpsProxy.username); Assert.assertEquals("anotherpasswords", systemHttpsProxy.password); Assert.assertEquals("aanotherhosts", systemHttpsProxy.host); Assert.assertEquals(9091, systemHttpsProxy.port); Assert.assertEquals("somethingelse|someotherip|75.41.41.33", systemHttpsProxy.nonProxyHosts); Assert.assertEquals("http", npmrcHttpsProxy.protocol); Assert.assertEquals("httpsuser", npmrcHttpsProxy.username); Assert.assertEquals("httpspassword", npmrcHttpsProxy.password); Assert.assertEquals("httpshost", npmrcHttpsProxy.host); Assert.assertEquals(8081, npmrcHttpsProxy.port); Assert.assertEquals("192.168.1.1|vaadin.com|mycompany.com", npmrcHttpsProxy.nonProxyHosts); Assert.assertEquals("http", npmrcProxy.protocol); Assert.assertEquals("httpuser", npmrcProxy.username); Assert.assertEquals("httppassword", npmrcProxy.password); Assert.assertEquals("httphost", npmrcProxy.host); Assert.assertEquals(8080, npmrcProxy.port); Assert.assertEquals("192.168.1.1|vaadin.com|mycompany.com", npmrcProxy.nonProxyHosts); }
@Test public synchronized void getProxies_npmrcWithProxySettingNoNoproxy_shouldReturnNullNoproxy() throws IOException { File npmrc = new File(tmpDirWithNpmrc.newFolder("test1"), ".npmrc"); Properties properties = new Properties(); properties.put(FrontendTools.NPMRC_PROXY_PROPERTY_KEY, "http: properties.put(FrontendTools.NPMRC_HTTPS_PROXY_PROPERTY_KEY, "http: try (FileOutputStream fileOutputStream = new FileOutputStream(npmrc)) { properties.store(fileOutputStream, null); } FrontendTools tools = new FrontendTools(npmrc.getParent(), null); List<ProxyConfig.Proxy> proxyList = tools.getProxies(); Assert.assertEquals(2, proxyList.size()); ProxyConfig.Proxy httpsProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(0) : proxyList.get(1); ProxyConfig.Proxy httpProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(1) : proxyList.get(0); Assert.assertEquals("http", httpProxy.protocol); Assert.assertEquals("httpuser", httpProxy.username); Assert.assertEquals("httppassword", httpProxy.password); Assert.assertEquals("httphost", httpProxy.host); Assert.assertEquals(8080, httpProxy.port); Assert.assertNull(httpProxy.nonProxyHosts); Assert.assertEquals("http", httpsProxy.protocol); Assert.assertEquals("httpsuser", httpsProxy.username); Assert.assertEquals("httpspassword", httpsProxy.password); Assert.assertEquals("httpshost", httpsProxy.host); Assert.assertEquals(8081, httpsProxy.port); Assert.assertNull(httpsProxy.nonProxyHosts); }
@Test public synchronized void getProxies_npmrcWithProxySetting_shouldReturnProxiesList() throws IOException { File npmrc = new File(tmpDirWithNpmrc.newFolder("test1"), ".npmrc"); Properties properties = new Properties(); properties.put(FrontendTools.NPMRC_PROXY_PROPERTY_KEY, "http: properties.put(FrontendTools.NPMRC_HTTPS_PROXY_PROPERTY_KEY, "http: properties.put(FrontendTools.NPMRC_NOPROXY_PROPERTY_KEY, "192.168.1.1,vaadin.com,mycompany.com"); try (FileOutputStream fileOutputStream = new FileOutputStream(npmrc)) { properties.store(fileOutputStream, null); } FrontendTools tools = new FrontendTools(npmrc.getParent(), null); List<ProxyConfig.Proxy> proxyList = tools.getProxies(); Assert.assertEquals(2, proxyList.size()); ProxyConfig.Proxy httpsProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(0) : proxyList.get(1); ProxyConfig.Proxy httpProxy = proxyList.get(0).id.startsWith( "https-proxy") ? proxyList.get(1) : proxyList.get(0); Assert.assertEquals("http", httpProxy.protocol); Assert.assertEquals("httpuser", httpProxy.username); Assert.assertEquals("httppassword", httpProxy.password); Assert.assertEquals("httphost", httpProxy.host); Assert.assertEquals(8080, httpProxy.port); Assert.assertEquals("192.168.1.1|vaadin.com|mycompany.com", httpProxy.nonProxyHosts); Assert.assertEquals("http", httpsProxy.protocol); Assert.assertEquals("httpsuser", httpsProxy.username); Assert.assertEquals("httpspassword", httpsProxy.password); Assert.assertEquals("httpshost", httpsProxy.host); Assert.assertEquals(8081, httpsProxy.port); Assert.assertEquals("192.168.1.1|vaadin.com|mycompany.com", httpsProxy.nonProxyHosts); }