From e88d91823f210ac762146c5f7b33e93d38abacdc Mon Sep 17 00:00:00 2001 From: Jurn van Mieghem Date: Mon, 15 Aug 2022 09:41:58 +0200 Subject: [PATCH] Create proxy based on environment variables (#197) Co-authored-by: Jurn van Mieghem --- README.md | 3 ++ WebDriverManager.Tests/BinaryServiceTests.cs | 37 +++++++++++++++++++ .../Services/Impl/BinaryService.cs | 17 +++++++++ 3 files changed, 57 insertions(+) diff --git a/README.md b/README.md index b6715e5..29446f1 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,9 @@ new DriverManager().SetUpDriver(new TaobaoPhantomConfig()); new DriverManager().WithProxy(previouslyInitializedProxy).SetUpDriver(new ChromeConfig()); ``` +You can also set the environment variables `HTTP_PROXY` and/or `HTTPS_PROXY` to use a proxy when retrieving the drivers. +This does not require any changes in the setup of DriverManager in C#. + ## Thanks Thanks to the following companies for generously providing their services/products to help improve this project: diff --git a/WebDriverManager.Tests/BinaryServiceTests.cs b/WebDriverManager.Tests/BinaryServiceTests.cs index 6e6d654..3e0dcc2 100644 --- a/WebDriverManager.Tests/BinaryServiceTests.cs +++ b/WebDriverManager.Tests/BinaryServiceTests.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using WebDriverManager.Helpers; using WebDriverManager.Services.Impl; @@ -7,6 +8,42 @@ namespace WebDriverManager.Tests { public class BinaryServiceTests : BinaryService { + [Fact] + public void ProxySetBySystemVariableHttp() + { + const string url = "https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip"; + const string httpName = "HTTP_PROXY"; + const string proxyUrl = "http://myproxy:8080/"; + Environment.SetEnvironmentVariable(httpName, proxyUrl); + CheckProxySystemVariables(); + // Remove to make sure it is not saved in later runs + Environment.SetEnvironmentVariable(httpName, null); + Assert.NotNull(Proxy); + Assert.Equal(proxyUrl, Proxy.GetProxy(new Uri(url)).AbsoluteUri); + } + + [Fact] + public void ProxySetBySystemVariableHttps() + { + const string url = "https://chromedriver.storage.googleapis.com/2.27/chromedriver_win32.zip"; + const string httpName = "HTTPS_PROXY"; + const string proxyUrl = "https://myproxy:8080/"; + Environment.SetEnvironmentVariable(httpName, proxyUrl); + CheckProxySystemVariables(); + // Remove to make sure it is not saved in later runs + Environment.SetEnvironmentVariable(httpName, null); + Assert.NotNull(Proxy); + Assert.Equal(proxyUrl, Proxy.GetProxy(new Uri(url)).AbsoluteUri); + } + + + [Fact] + public void NoProxyBySystemVariable() + { + CheckProxySystemVariables(); + Assert.Null(Proxy); + } + [Fact] public void DownloadZipResultNotEmpty() { diff --git a/WebDriverManager/Services/Impl/BinaryService.cs b/WebDriverManager/Services/Impl/BinaryService.cs index e157913..68fadf4 100644 --- a/WebDriverManager/Services/Impl/BinaryService.cs +++ b/WebDriverManager/Services/Impl/BinaryService.cs @@ -138,6 +138,8 @@ public string SetupBinary(string url, string zipPath, string binaryPath) public string DownloadZip(string url, string destination) { if (File.Exists(destination)) return destination; + if (Proxy == null) CheckProxySystemVariables(); + if (Proxy != null) { using (var webClient = new WebClient() {Proxy = Proxy}) @@ -155,6 +157,21 @@ public string DownloadZip(string url, string destination) return destination; } + protected void CheckProxySystemVariables() + { + const string nameHttp = "HTTP_PROXY"; + const string nameHttps = "HTTPS_PROXY"; + var httpProxyVariable = Environment.GetEnvironmentVariable(nameHttp, EnvironmentVariableTarget.Process); + var httpsProxyVariable = Environment.GetEnvironmentVariable(nameHttps, EnvironmentVariableTarget.Process); + if (!string.IsNullOrEmpty(httpProxyVariable)) + { + Proxy = new WebProxy(httpProxyVariable); + } + else if (!string.IsNullOrEmpty(httpsProxyVariable)) + { + Proxy = new WebProxy(httpsProxyVariable); + } + } protected string UnZip(string path, string destination, string name) {