Skip to content

Commit

Permalink
Create proxy based on environment variables (#197)
Browse files Browse the repository at this point in the history
Co-authored-by: Jurn van Mieghem <jurn.vanmieghem@ad.dfnld.nl>
  • Loading branch information
jgelon and Jurn van Mieghem committed Aug 15, 2022
1 parent a88cd6c commit e88d918
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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:

Expand Down
37 changes: 37 additions & 0 deletions WebDriverManager.Tests/BinaryServiceTests.cs
@@ -1,3 +1,4 @@
using System;
using System.IO;
using WebDriverManager.Helpers;
using WebDriverManager.Services.Impl;
Expand All @@ -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()
{
Expand Down
17 changes: 17 additions & 0 deletions WebDriverManager/Services/Impl/BinaryService.cs
Expand Up @@ -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})
Expand All @@ -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)
{
Expand Down

0 comments on commit e88d918

Please sign in to comment.