Skip to content

Commit cc89cea

Browse files
authored
Add ability to use manager with proxy (#94)
Closes #88
1 parent 5572df7 commit cc89cea

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ Only for Google Chrome so far, you can specify to automatically download a ```ch
201201

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

204+
205+
### Using with proxy:
206+
new DriverManager().WithProxy(previouslyInitializedProxy).SetUpDriver(new ChromeConfig());
207+
204208
## Thanks
205209
Thanks to the following companies for generously providing their services/products to help improve this project:
206210

WebDriverManager.Tests/DriverManagerTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Net;
13
using WebDriverManager.DriverConfigs.Impl;
24
using Xunit;
35

@@ -11,6 +13,27 @@ public void ChromeTest()
1113
{
1214
new DriverManager().SetUpDriver(new ChromeConfig());
1315
}
16+
17+
[Fact]
18+
public void DownloadZipFileWithProxy()
19+
{
20+
var webProxyStub = new WebProxyStub();
21+
new DriverManager().WithProxy(webProxyStub).SetUpDriver(new ChromeConfig());
22+
Assert.True(webProxyStub.IsBypassed(webProxyStub.RequestedUri));
23+
}
24+
25+
private class WebProxyStub : IWebProxy
26+
{
27+
public Uri RequestedUri { get; private set; }
28+
public ICredentials Credentials { get; set; }
29+
public Uri GetProxy(Uri destination) => new Uri("http://localhost");
30+
31+
public bool IsBypassed(Uri host)
32+
{
33+
RequestedUri = host;
34+
return true;
35+
}
36+
}
1437
}
1538

1639
public class ParallelDriverManagerTests

WebDriverManager/DriverManager.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System.Net;
22
using WebDriverManager.DriverConfigs;
33
using WebDriverManager.Helpers;
44
using WebDriverManager.Services;
@@ -10,7 +10,7 @@ public class DriverManager
1010
{
1111
private static readonly object Object = new object();
1212

13-
private readonly IBinaryService _binaryService;
13+
private IBinaryService _binaryService;
1414
private readonly IVariableService _variableService;
1515

1616
public DriverManager()
@@ -25,6 +25,12 @@ public DriverManager(IBinaryService binaryService, IVariableService variableServ
2525
_variableService = variableService;
2626
}
2727

28+
public DriverManager WithProxy(IWebProxy proxy)
29+
{
30+
_binaryService = new BinaryService {Proxy = proxy};
31+
return this;
32+
}
33+
2834
public void SetUpDriver(string url, string binaryPath, string binaryName)
2935
{
3036
var zipPath = FileHelper.GetZipDestination(url);

WebDriverManager/Helpers/RegistryHelper.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Diagnostics;
4-
using System.Text;
52
using Microsoft.Win32;
63

74
namespace WebDriverManager.Helpers

WebDriverManager/Helpers/VersionHelper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
42

53
namespace WebDriverManager.Helpers
64
{

WebDriverManager/Services/Impl/BinaryService.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace WebDriverManager.Services.Impl
1212
{
1313
public class BinaryService : IBinaryService
1414
{
15+
public IWebProxy Proxy { get; set; }
16+
1517
public string SetupBinary(string url, string zipDestination, string binDestination, string binaryName)
1618
{
1719
if (File.Exists(binDestination)) return binDestination;
@@ -36,19 +38,29 @@ public string SetupBinary(string url, string zipDestination, string binDestinati
3638
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
3739
{
3840
var chmod = Process.Start("chmod", $"+x {binDestination}");
39-
chmod.WaitForExit();
41+
chmod?.WaitForExit();
4042
}
4143

4244
RemoveZip(zipDestination);
4345
return binDestination;
4446
}
4547

46-
protected string DownloadZip(string url, string destination)
48+
public string DownloadZip(string url, string destination)
4749
{
4850
if (File.Exists(destination)) return destination;
49-
using (var webClient = new WebClient())
51+
if (Proxy != null)
5052
{
51-
webClient.DownloadFile(new Uri(url), destination);
53+
using (var webClient = new WebClient() {Proxy = Proxy})
54+
{
55+
webClient.DownloadFile(new Uri(url), destination);
56+
}
57+
}
58+
else
59+
{
60+
using (var webClient = new WebClient())
61+
{
62+
webClient.DownloadFile(new Uri(url), destination);
63+
}
5264
}
5365

5466
return destination;

0 commit comments

Comments
 (0)