Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create proxy based on environment variables #197

Merged
merged 3 commits into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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