Skip to content

Commit

Permalink
Add ability to use manager with proxy (#94)
Browse files Browse the repository at this point in the history
Closes #88
  • Loading branch information
rosolko authored Sep 19, 2020
1 parent 5572df7 commit cc89cea
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ Only for Google Chrome so far, you can specify to automatically download a ```ch

new DriverManager().SetUpDriver(new TaobaoPhantomConfig());


### Using with proxy:
new DriverManager().WithProxy(previouslyInitializedProxy).SetUpDriver(new ChromeConfig());

## Thanks
Thanks to the following companies for generously providing their services/products to help improve this project:

Expand Down
23 changes: 23 additions & 0 deletions WebDriverManager.Tests/DriverManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Net;
using WebDriverManager.DriverConfigs.Impl;
using Xunit;

Expand All @@ -11,6 +13,27 @@ public void ChromeTest()
{
new DriverManager().SetUpDriver(new ChromeConfig());
}

[Fact]
public void DownloadZipFileWithProxy()
{
var webProxyStub = new WebProxyStub();
new DriverManager().WithProxy(webProxyStub).SetUpDriver(new ChromeConfig());
Assert.True(webProxyStub.IsBypassed(webProxyStub.RequestedUri));
}

private class WebProxyStub : IWebProxy
{
public Uri RequestedUri { get; private set; }
public ICredentials Credentials { get; set; }
public Uri GetProxy(Uri destination) => new Uri("http://localhost");

public bool IsBypassed(Uri host)
{
RequestedUri = host;
return true;
}
}
}

public class ParallelDriverManagerTests
Expand Down
10 changes: 8 additions & 2 deletions WebDriverManager/DriverManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Net;
using WebDriverManager.DriverConfigs;
using WebDriverManager.Helpers;
using WebDriverManager.Services;
Expand All @@ -10,7 +10,7 @@ public class DriverManager
{
private static readonly object Object = new object();

private readonly IBinaryService _binaryService;
private IBinaryService _binaryService;
private readonly IVariableService _variableService;

public DriverManager()
Expand All @@ -25,6 +25,12 @@ public DriverManager(IBinaryService binaryService, IVariableService variableServ
_variableService = variableService;
}

public DriverManager WithProxy(IWebProxy proxy)
{
_binaryService = new BinaryService {Proxy = proxy};
return this;
}

public void SetUpDriver(string url, string binaryPath, string binaryName)
{
var zipPath = FileHelper.GetZipDestination(url);
Expand Down
3 changes: 0 additions & 3 deletions WebDriverManager/Helpers/RegistryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Microsoft.Win32;

namespace WebDriverManager.Helpers
Expand Down
2 changes: 0 additions & 2 deletions WebDriverManager/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace WebDriverManager.Helpers
{
Expand Down
20 changes: 16 additions & 4 deletions WebDriverManager/Services/Impl/BinaryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace WebDriverManager.Services.Impl
{
public class BinaryService : IBinaryService
{
public IWebProxy Proxy { get; set; }

public string SetupBinary(string url, string zipDestination, string binDestination, string binaryName)
{
if (File.Exists(binDestination)) return binDestination;
Expand All @@ -36,19 +38,29 @@ public string SetupBinary(string url, string zipDestination, string binDestinati
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var chmod = Process.Start("chmod", $"+x {binDestination}");
chmod.WaitForExit();
chmod?.WaitForExit();
}

RemoveZip(zipDestination);
return binDestination;
}

protected string DownloadZip(string url, string destination)
public string DownloadZip(string url, string destination)
{
if (File.Exists(destination)) return destination;
using (var webClient = new WebClient())
if (Proxy != null)
{
webClient.DownloadFile(new Uri(url), destination);
using (var webClient = new WebClient() {Proxy = Proxy})
{
webClient.DownloadFile(new Uri(url), destination);
}
}
else
{
using (var webClient = new WebClient())
{
webClient.DownloadFile(new Uri(url), destination);
}
}

return destination;
Expand Down

0 comments on commit cc89cea

Please sign in to comment.