1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using System . Net ;
5
6
using System . Net . Http ;
6
7
using Moq ;
@@ -39,8 +40,8 @@ public void HttpClientFactory_TryCreateProxy_NoProxy_ReturnsFalseOutNull()
39
40
{
40
41
const string repoPath = "/tmp/repos/foo" ;
41
42
const string repoRemote = "https://remote.example.com/foo.git" ;
42
- var repoRemoteUri = new Uri ( repoRemote ) ;
43
43
44
+ var repoRemoteUri = new Uri ( repoRemote ) ;
44
45
var settings = new TestSettings
45
46
{
46
47
RemoteUri = repoRemoteUri ,
@@ -57,59 +58,96 @@ public void HttpClientFactory_TryCreateProxy_NoProxy_ReturnsFalseOutNull()
57
58
[ Fact ]
58
59
public void HttpClientFactory_TryCreateProxy_ProxyNoCredentials_ReturnsTrueOutProxyWithUrlDefaultCredentials ( )
59
60
{
61
+ const string proxyUrl = "https://proxy.example.com/git" ;
60
62
const string repoPath = "/tmp/repos/foo" ;
61
63
const string repoRemote = "https://remote.example.com/foo.git" ;
62
- var repoRemoteUri = new Uri ( repoRemote ) ;
63
64
64
- string proxyConfigString = "https://proxy.example.com/git" ;
65
- string expectedProxyUrl = proxyConfigString ;
65
+ var repoRemoteUri = new Uri ( repoRemote ) ;
66
+ var proxyConfig = new ProxyConfiguration ( new Uri ( proxyUrl ) ) ;
66
67
67
68
var settings = new TestSettings
68
69
{
69
70
RemoteUri = repoRemoteUri ,
70
71
RepositoryPath = repoPath ,
71
- ProxyConfiguration = new Uri ( proxyConfigString )
72
+ ProxyConfiguration = proxyConfig
72
73
} ;
73
74
var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
74
75
75
76
bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
76
77
77
78
Assert . True ( result ) ;
78
79
Assert . NotNull ( proxy ) ;
79
- var configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
80
- Assert . Equal ( expectedProxyUrl , configuredProxyUrl . ToString ( ) ) ;
80
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
81
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
81
82
82
83
AssertDefaultCredentials ( proxy . Credentials ) ;
83
84
}
84
85
86
+ [ Fact ]
87
+ public void HttpClientFactory_TryCreateProxy_ProxyWithBypass_ReturnsTrueOutProxyWithBypassedHosts ( )
88
+ {
89
+ const string proxyUrl = "https://proxy.example.com/git" ;
90
+ const string repoPath = "/tmp/repos/foo" ;
91
+ const string repoRemote = "https://remote.example.com/foo.git" ;
92
+
93
+ var bypassList = new List < string > { "https://contoso.com" , ".*fabrikam\\ .com" } ;
94
+ var repoRemoteUri = new Uri ( repoRemote ) ;
95
+ var proxyConfig = new ProxyConfiguration (
96
+ new Uri ( proxyUrl ) ,
97
+ userName : null ,
98
+ password : null ,
99
+ bypassHosts : bypassList ) ;
100
+
101
+ var settings = new TestSettings
102
+ {
103
+ RemoteUri = repoRemoteUri ,
104
+ RepositoryPath = repoPath ,
105
+ ProxyConfiguration = proxyConfig
106
+ } ;
107
+ var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
108
+
109
+ bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
110
+
111
+ Assert . True ( result ) ;
112
+ Assert . NotNull ( proxy ) ;
113
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
114
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
115
+
116
+ Assert . True ( proxy . IsBypassed ( new Uri ( "https://contoso.com" ) ) ) ;
117
+ Assert . True ( proxy . IsBypassed ( new Uri ( "http://fabrikam.com" ) ) ) ;
118
+ Assert . True ( proxy . IsBypassed ( new Uri ( "https://subdomain.fabrikam.com" ) ) ) ;
119
+ Assert . False ( proxy . IsBypassed ( repoRemoteUri ) ) ;
120
+ }
121
+
85
122
[ Fact ]
86
123
public void HttpClientFactory_TryCreateProxy_ProxyWithCredentials_ReturnsTrueOutProxyWithUrlConfiguredCredentials ( )
87
124
{
88
- const string proxyScheme = "https" ;
89
125
const string proxyUser = "john.doe" ;
90
126
const string proxyPass = "letmein" ;
91
- const string proxyHost = "proxy.example.com/git" ;
127
+ const string proxyUrl = "https:// proxy.example.com/git" ;
92
128
const string repoPath = "/tmp/repos/foo" ;
93
129
const string repoRemote = "https://remote.example.com/foo.git" ;
94
130
95
- string proxyConfigString = $ "{ proxyScheme } ://{ proxyUser } :{ proxyPass } @{ proxyHost } ";
96
- string expectedProxyUrl = $ "{ proxyScheme } ://{ proxyHost } ";
97
131
var repoRemoteUri = new Uri ( repoRemote ) ;
132
+ var proxyConfig = new ProxyConfiguration (
133
+ new Uri ( proxyUrl ) ,
134
+ proxyUser ,
135
+ proxyPass ) ;
98
136
99
137
var settings = new TestSettings
100
138
{
101
139
RemoteUri = repoRemoteUri ,
102
140
RepositoryPath = repoPath ,
103
- ProxyConfiguration = new Uri ( proxyConfigString )
141
+ ProxyConfiguration = proxyConfig
104
142
} ;
105
143
var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
106
144
107
145
bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
108
146
109
147
Assert . True ( result ) ;
110
148
Assert . NotNull ( proxy ) ;
111
- var configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
112
- Assert . Equal ( expectedProxyUrl , configuredProxyUrl . ToString ( ) ) ;
149
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
150
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
113
151
114
152
Assert . NotNull ( proxy . Credentials ) ;
115
153
Assert . IsType < NetworkCredential > ( proxy . Credentials ) ;
@@ -121,30 +159,31 @@ public void HttpClientFactory_TryCreateProxy_ProxyWithCredentials_ReturnsTrueOut
121
159
[ Fact ]
122
160
public void HttpClientFactory_TryCreateProxy_ProxyWithNonEmptyUserAndEmptyPass_ReturnsTrueOutProxyWithUrlConfiguredCredentials ( )
123
161
{
124
- const string proxyScheme = "https" ;
162
+ const string proxyUrl = "https://proxy.example.com/git " ;
125
163
const string proxyUser = "john.doe" ;
126
- const string proxyHost = "proxy.example.com/git" ;
127
164
const string repoPath = "/tmp/repos/foo" ;
128
165
const string repoRemote = "https://remote.example.com/foo.git" ;
129
166
130
- string proxyConfigString = $ "{ proxyScheme } ://{ proxyUser } :@{ proxyHost } ";
131
- string expectedProxyUrl = $ "{ proxyScheme } ://{ proxyHost } ";
132
167
var repoRemoteUri = new Uri ( repoRemote ) ;
168
+ var proxyConfig = new ProxyConfiguration (
169
+ new Uri ( proxyUrl ) ,
170
+ proxyUser ,
171
+ password : null ) ;
133
172
134
173
var settings = new TestSettings
135
174
{
136
175
RemoteUri = repoRemoteUri ,
137
176
RepositoryPath = repoPath ,
138
- ProxyConfiguration = new Uri ( proxyConfigString )
177
+ ProxyConfiguration = proxyConfig
139
178
} ;
140
179
var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
141
180
142
181
bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
143
182
144
183
Assert . True ( result ) ;
145
184
Assert . NotNull ( proxy ) ;
146
- var configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
147
- Assert . Equal ( expectedProxyUrl , configuredProxyUrl . ToString ( ) ) ;
185
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
186
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
148
187
149
188
Assert . NotNull ( proxy . Credentials ) ;
150
189
Assert . IsType < NetworkCredential > ( proxy . Credentials ) ;
@@ -156,30 +195,31 @@ public void HttpClientFactory_TryCreateProxy_ProxyWithNonEmptyUserAndEmptyPass_R
156
195
[ Fact ]
157
196
public void HttpClientFactory_TryCreateProxy_ProxyWithEmptyUserAndNonEmptyPass_ReturnsTrueOutProxyWithUrlConfiguredCredentials ( )
158
197
{
159
- const string proxyScheme = "https" ;
198
+ const string proxyUrl = "https://proxy.example.com/git " ;
160
199
const string proxyPass = "letmein" ;
161
- const string proxyHost = "proxy.example.com/git" ;
162
200
const string repoPath = "/tmp/repos/foo" ;
163
201
const string repoRemote = "https://remote.example.com/foo.git" ;
164
202
165
- string proxyConfigString = $ "{ proxyScheme } ://:{ proxyPass } @{ proxyHost } ";
166
- string expectedProxyUrl = $ "{ proxyScheme } ://{ proxyHost } ";
167
203
var repoRemoteUri = new Uri ( repoRemote ) ;
204
+ var proxyConfig = new ProxyConfiguration (
205
+ new Uri ( proxyUrl ) ,
206
+ userName : null ,
207
+ password : proxyPass ) ;
168
208
169
209
var settings = new TestSettings
170
210
{
171
211
RemoteUri = repoRemoteUri ,
172
212
RepositoryPath = repoPath ,
173
- ProxyConfiguration = new Uri ( proxyConfigString )
213
+ ProxyConfiguration = proxyConfig
174
214
} ;
175
215
var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
176
216
177
217
bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
178
218
179
219
Assert . True ( result ) ;
180
220
Assert . NotNull ( proxy ) ;
181
- var configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
182
- Assert . Equal ( expectedProxyUrl , configuredProxyUrl . ToString ( ) ) ;
221
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
222
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
183
223
184
224
Assert . NotNull ( proxy . Credentials ) ;
185
225
Assert . IsType < NetworkCredential > ( proxy . Credentials ) ;
@@ -191,27 +231,30 @@ public void HttpClientFactory_TryCreateProxy_ProxyWithEmptyUserAndNonEmptyPass_R
191
231
[ Fact ]
192
232
public void HttpClientFactory_TryCreateProxy_ProxyEmptyUserAndEmptyPass_ReturnsTrueOutProxyWithUrlDefaultCredentials ( )
193
233
{
234
+ const string proxyUrl = "https://proxy.example.com/git" ;
194
235
const string repoPath = "/tmp/repos/foo" ;
195
236
const string repoRemote = "https://remote.example.com/foo.git" ;
196
237
var repoRemoteUri = new Uri ( repoRemote ) ;
197
238
198
- string proxyConfigString = "https://:@proxy.example.com/git" ;
199
- string expectedProxyUrl = "https://proxy.example.com/git" ;
239
+ var proxyConfig = new ProxyConfiguration (
240
+ new Uri ( proxyUrl ) ,
241
+ userName : string . Empty ,
242
+ password : string . Empty ) ;
200
243
201
244
var settings = new TestSettings
202
245
{
203
246
RemoteUri = repoRemoteUri ,
204
247
RepositoryPath = repoPath ,
205
- ProxyConfiguration = new Uri ( proxyConfigString )
248
+ ProxyConfiguration = proxyConfig
206
249
} ;
207
250
var httpFactory = new HttpClientFactory ( Mock . Of < ITrace > ( ) , settings , Mock . Of < IStandardStreams > ( ) ) ;
208
251
209
252
bool result = httpFactory . TryCreateProxy ( out IWebProxy proxy ) ;
210
253
211
254
Assert . True ( result ) ;
212
255
Assert . NotNull ( proxy ) ;
213
- var configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
214
- Assert . Equal ( expectedProxyUrl , configuredProxyUrl . ToString ( ) ) ;
256
+ Uri configuredProxyUrl = proxy . GetProxy ( repoRemoteUri ) ;
257
+ Assert . Equal ( proxyUrl , configuredProxyUrl ? . ToString ( ) ) ;
215
258
216
259
AssertDefaultCredentials ( proxy . Credentials ) ;
217
260
}
0 commit comments