blob: a96914586698c87df991092104b4cb2b34465cef (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.Net.Tests
{
public class WebRequestTest
{
static WebRequestTest()
{
// Capture the value of DefaultWebProxy before any tests run.
// This lets us test the default value without imposing test
// ordering constraints which aren't natively supported by xunit.
initialDefaultWebProxy = WebRequest.DefaultWebProxy;
initialDefaultWebProxyCredentials = initialDefaultWebProxy.Credentials;
}
private readonly NetworkCredential _explicitCredentials = new NetworkCredential("user", "password", "domain");
private static IWebProxy initialDefaultWebProxy;
private static ICredentials initialDefaultWebProxyCredentials;
[Fact]
public void DefaultWebProxy_VerifyDefaults_Success()
{
Assert.NotNull(initialDefaultWebProxy);
Assert.Null(initialDefaultWebProxyCredentials);
}
[Fact]
public void DefaultWebProxy_SetCredentialsToNullThenGet_ValuesMatch()
{
IWebProxy proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = null;
Assert.Equal(null, proxy.Credentials);
}
[Fact]
public void DefaultWebProxy_SetCredentialsToDefaultCredentialsThenGet_ValuesMatch()
{
IWebProxy proxy = WebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;
Assert.Equal(CredentialCache.DefaultCredentials, proxy.Credentials);
}
[Fact]
public void DefaultWebProxy_SetCredentialsToExplicitCredentialsThenGet_ValuesMatch()
{
IWebProxy proxy = WebRequest.DefaultWebProxy;
ICredentials oldCreds = proxy.Credentials;
try
{
proxy.Credentials = _explicitCredentials;
Assert.Equal(_explicitCredentials, proxy.Credentials);
}
finally
{
// Reset the credentials so as not to interfere with any subsequent tests,
// e.g. DefaultWebProxy_VerifyDefaults_Success
proxy.Credentials = oldCreds;
}
}
[Theory]
[InlineData("http")]
[InlineData("https")]
public void Create_ValidWebRequestUriScheme_Success(string scheme)
{
var uri = new Uri($"{scheme}://example.com/folder/resource.txt");
WebRequest request = WebRequest.Create(uri);
}
[Theory]
[InlineData("ws")]
[InlineData("wss")]
[InlineData("file")]
[InlineData("ftp")]
[InlineData("custom")]
public void Create_InvalidWebRequestUriScheme_Throws(string scheme)
{
var uri = new Uri($"{scheme}://example.com/folder/resource.txt");
Assert.Throws<NotSupportedException>(() => WebRequest.Create(uri));
}
}
}
|