Skip to content

Commit 792b15d

Browse files
committed
增加HttpProxy单元测试
1 parent 2b32bb9 commit 792b15d

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

WebApiClient/HttpProxy.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ namespace WebApiClient
99
/// </summary>
1010
public class HttpProxy : IWebProxy
1111
{
12+
/// <summary>
13+
/// 授权字段
14+
/// </summary>
15+
private ICredentials credentials;
16+
1217
/// <summary>
1318
/// 获取代理服务器域名或ip
1419
/// </summary>
@@ -32,7 +37,17 @@ public class HttpProxy : IWebProxy
3237
/// <summary>
3338
/// 获取或设置授权信息
3439
/// </summary>
35-
ICredentials IWebProxy.Credentials { get; set; }
40+
ICredentials IWebProxy.Credentials
41+
{
42+
get
43+
{
44+
return this.credentials;
45+
}
46+
set
47+
{
48+
this.SetCredentialsByInterface(value);
49+
}
50+
}
3651

3752
/// <summary>
3853
/// http代理信息
@@ -69,8 +84,9 @@ public HttpProxy(Uri proxyAddress)
6984
/// <param name="port">代理服务器端口</param>
7085
/// <exception cref="ArgumentNullException"></exception>
7186
public HttpProxy(string host, int port)
72-
: this(host, port, null, null)
7387
{
88+
this.Host = host ?? throw new ArgumentNullException(nameof(host));
89+
this.Port = port;
7490
}
7591

7692
/// <summary>
@@ -82,18 +98,37 @@ public HttpProxy(string host, int port)
8298
/// <param name="password">代理服务器密码</param>
8399
/// <exception cref="ArgumentNullException"></exception>
84100
public HttpProxy(string host, int port, string userName, string password)
101+
: this(host, port)
85102
{
86-
this.Host = host ?? throw new ArgumentNullException(nameof(host));
87-
this.Port = port;
88103
this.UserName = userName;
89104
this.Password = password;
90105

91106
if (string.IsNullOrEmpty(userName + password) == false)
92107
{
93-
((IWebProxy)this).Credentials = new NetworkCredential(userName, password);
108+
this.credentials = new NetworkCredential(userName, password);
94109
}
95110
}
96111

112+
/// <summary>
113+
/// 通过接口设置授权信息
114+
/// </summary>
115+
/// <param name="value"></param>
116+
private void SetCredentialsByInterface(ICredentials value)
117+
{
118+
var userName = default(string);
119+
var password = default(string);
120+
if (value != null)
121+
{
122+
var networkCredentialsd = value.GetCredential(null, null);
123+
userName = networkCredentialsd?.UserName;
124+
password = networkCredentialsd?.Password;
125+
}
126+
127+
this.UserName = userName;
128+
this.Password = password;
129+
this.credentials = value;
130+
}
131+
97132
/// <summary>
98133
/// 从IWebProxy实例转换获得
99134
/// </summary>
@@ -115,13 +150,8 @@ public static HttpProxy FromWebProxy(IWebProxy webProxy, Uri targetAddress)
115150

116151
var proxyAddress = webProxy.GetProxy(targetAddress);
117152
var httpProxy = new HttpProxy(proxyAddress);
153+
httpProxy.SetCredentialsByInterface(webProxy.Credentials);
118154

119-
if (webProxy.Credentials != null)
120-
{
121-
var credentials = webProxy.Credentials.GetCredential(null, null);
122-
httpProxy.UserName = credentials?.UserName;
123-
httpProxy.Password = credentials?.Password;
124-
}
125155
return httpProxy;
126156
}
127157

@@ -159,19 +189,19 @@ public string ToTunnelRequestString(Uri targetAddress)
159189
/// <summary>
160190
/// 获取代理服务器地址
161191
/// </summary>
162-
/// <param name="destination"></param>
192+
/// <param name="destination">目标地址</param>
163193
/// <returns></returns>
164-
Uri IWebProxy.GetProxy(Uri destination)
194+
public Uri GetProxy(Uri destination)
165195
{
166196
return new Uri($"http://{this.Host}:{this.Port}/");
167197
}
168198

169199
/// <summary>
170200
/// 是否忽略代理
171201
/// </summary>
172-
/// <param name="host"></param>
202+
/// <param name="host">目标地址</param>
173203
/// <returns></returns>
174-
bool IWebProxy.IsBypassed(Uri host)
204+
public bool IsBypassed(Uri host)
175205
{
176206
return false;
177207
}

WebApiClient/ProxyValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class ProxyValidator
2626
/// <param name="proxyPort">代理服务器端口</param>
2727
/// <exception cref="ArgumentNullException"></exception>
2828
public ProxyValidator(string proxyHost, int proxyPort)
29+
: this(new HttpProxy(proxyHost, proxyPort))
2930
{
30-
this.webProxy = new HttpProxy(proxyHost, proxyPort);
3131
}
3232

3333
/// <summary>

WebApiClientTest/HttpProxyTest.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Text;
5+
using WebApiClient;
6+
using Xunit;
7+
8+
namespace WebApiClientTest
9+
{
10+
public class HttpProxyTest
11+
{
12+
[Fact]
13+
public void CtorTest()
14+
{
15+
var target = new Uri("https://www.baidu.com/");
16+
var uri = "http://localhost:5533/";
17+
var proxy = new HttpProxy(uri);
18+
19+
Assert.True(proxy.Host == "localhost" && proxy.Port == 5533);
20+
Assert.True(proxy.GetProxy(target) == new Uri(uri));
21+
Assert.True(proxy.UserName == null);
22+
Assert.True(((IWebProxy)proxy).Credentials == null);
23+
24+
25+
proxy = new HttpProxy(new Uri(uri));
26+
Assert.True(proxy.Host == "localhost" && proxy.Port == 5533);
27+
Assert.True(proxy.GetProxy(target) == new Uri(uri));
28+
Assert.True(proxy.UserName == null);
29+
Assert.True(((IWebProxy)proxy).Credentials == null);
30+
31+
32+
proxy = new HttpProxy("localhost", 5533, "laojiu", "123456");
33+
Assert.True(proxy.Host == "localhost" && proxy.Port == 5533);
34+
Assert.True(proxy.GetProxy(target) == new Uri(uri));
35+
Assert.True(proxy.UserName == "laojiu");
36+
Assert.True(proxy.Password == "123456");
37+
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).UserName == "laojiu");
38+
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).Password == "123456");
39+
40+
41+
var p = new WebProxy("127.0.0.1", 5000) { Credentials = new NetworkCredential("abc", "123") };
42+
proxy = HttpProxy.FromWebProxy(p, target);
43+
Assert.True(proxy.Host == "127.0.0.1" && proxy.Port == 5000);
44+
Assert.True(proxy.GetProxy(target) == new Uri("http://127.0.0.1:5000"));
45+
Assert.True(proxy.UserName == "abc");
46+
Assert.True(proxy.Password == "123");
47+
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).UserName == "abc");
48+
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).Password == "123");
49+
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)