Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpEnvironmentProxyTest.cs « UnitTests « tests « System.Net.Http « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e567669bdfef79510cf192714f79b5d5f69f15d5 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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 System.Collections.Generic;
using System.Net.Http;
using Xunit;
using Xunit.Abstractions;
using System.Diagnostics;

namespace System.Net.Http.Tests
{
    public class HttpEnvironmentProxyTest : RemoteExecutorTestBase
    {
        private readonly ITestOutputHelper _output;
        private static readonly Uri fooHttp = new Uri("http://foo.com");
        private static readonly Uri fooHttps = new Uri("https://foo.com");

        // This will clean specific environmental variables
        // to be sure they do not interfere with the test.
        private void CleanEnv()
        {
            List<string>  vars = new List<string>() { "http_proxy", "HTTPS_PROXY", "https_proxy",
                                                      "all_proxy", "ALL_PROXY",
                                                      "NO_PROXY" };
            foreach (string v in vars)
            {
                Environment.SetEnvironmentVariable(v, null);
            }
        }

        public HttpEnvironmentProxyTest(ITestOutputHelper output)
        {
            _output = output;
            CleanEnv();
        }

        [Fact]
        public void HttpProxy_EnvironmentProxy_Loaded()
        {
            RemoteInvoke(() =>
            {

                IWebProxy p;
                Uri u;

                // It should not return object if there are no variables set.
                Assert.False(HttpEnvironmentProxy.TryCreate(out p));

                Environment.SetEnvironmentVariable("all_proxy", "http://1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.Null(p.Credentials);

                u = p.GetProxy(fooHttp);
                Assert.True(u != null && u.Host == "1.1.1.1");
                u = p.GetProxy(fooHttps);
                Assert.True(u != null && u.Host == "1.1.1.1");

                Environment.SetEnvironmentVariable("http_proxy", "http://1.1.1.2:3001");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                // Protocol specific variables should take precedence over all_
                // and https should still use all_proxy.
                u = p.GetProxy(fooHttp);
                Assert.True(u != null && u.Host == "1.1.1.2" && u.Port == 3001);
                u = p.GetProxy(fooHttps);
                Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

                // Set https to invalid strings and use only IP & port for http.
                Environment.SetEnvironmentVariable("http_proxy", "1.1.1.3:3003");
                Environment.SetEnvironmentVariable("https_proxy", "ab!cd");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                u = p.GetProxy(fooHttp);
                Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003);
                u = p.GetProxy(fooHttps);
                Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

                // Try valid URI with unsupported protocol. It will be ignored
                // to mimic curl behavior.
                Environment.SetEnvironmentVariable("https_proxy", "socks5://1.1.1.4:3004");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                u = p.GetProxy(fooHttps);
                Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000);

                // Set https to valid URI but different from http.
                Environment.SetEnvironmentVariable("https_proxy", "http://1.1.1.5:3005");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                u = p.GetProxy(fooHttp);
                Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003);
                u = p.GetProxy(fooHttps);
                Assert.True(u != null && u.Host == "1.1.1.5" && u.Port == 3005);

                return SuccessExitCode;
            }).Dispose();
        }

        [Theory]
        [InlineData("1.1.1.5", "1.1.1.5", "80", null, null)]
        [InlineData("http://1.1.1.5:3005", "1.1.1.5", "3005", null, null)]
        [InlineData("http://foo@1.1.1.5", "1.1.1.5", "80", "foo", "")]
        [InlineData("http://[::1]:80", "[::1]", "80", null, null)]
        [InlineData("foo:bar@[::1]:3128", "[::1]", "3128", "foo", "bar")]
        [InlineData("foo:Pass$!#\\.$@127.0.0.1:3128", "127.0.0.1", "3128", "foo", "Pass$!#\\.$")]
        [InlineData("[::1]", "[::1]", "80", null, null)]
        [InlineData("domain\\foo:bar@1.1.1.1", "1.1.1.1", "80", "foo", "bar")]
        [InlineData("domain%5Cfoo:bar@1.1.1.1", "1.1.1.1", "80", "foo", "bar")]
        [InlineData("HTTP://ABC.COM/", "abc.com", "80", null, null)]
        [InlineData("http://10.30.62.64:7890/", "10.30.62.64", "7890", null, null)]
        [InlineData("http://1.2.3.4:8888/foo", "1.2.3.4", "8888", null, null)]
        [SkipOnTargetFramework(TargetFrameworkMonikers.Mono)]
        public void HttpProxy_Uri_Parsing(string _input, string _host, string _port, string _user , string _password)
        {
            RemoteInvoke((input, host, port, user, password) =>
            {
                // Remote exec does not allow to pass null at this moment.
                if (user == "null")
                {
                    user = null;
                }
                if (password == "null")
                {
                    password = null;
                }

                Environment.SetEnvironmentVariable("all_proxy", input);
                IWebProxy p;
                Uri u;

                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                u = p.GetProxy(fooHttp);
                Assert.Equal(host, u.Host);
                Assert.Equal(Convert.ToInt32(port), u.Port);

                if (user != null)
                {
                    NetworkCredential nc = p.Credentials.GetCredential(u, "Basic");
                    Assert.NotNull(nc);
                    Assert.Equal(user, nc.UserName);
                    Assert.Equal(password, nc.Password);
                }

                return SuccessExitCode;
            }, _input, _host, _port, _user ?? "null" , _password ?? "null").Dispose();
        }

        [Fact]
        public void HttpProxy_CredentialParsing_Basic()
        {
            RemoteInvoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("all_proxy", "http://foo:bar@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use user only without password.
                Environment.SetEnvironmentVariable("all_proxy", "http://foo@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Assert.NotNull(p.Credentials);

                // Use different user for http and https
                Environment.SetEnvironmentVariable("https_proxy", "http://foo1:bar1@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);
                Uri u = p.GetProxy(fooHttp);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                u = p.GetProxy(fooHttps);
                Assert.NotNull(p.Credentials.GetCredential(u, "Basic"));
                // This should not match Proxy Uri
                Assert.Null(p.Credentials.GetCredential(fooHttp, "Basic"));
                Assert.Null(p.Credentials.GetCredential(null, null));

                return SuccessExitCode;
            }).Dispose();
        }

        [Fact]
        public void HttpProxy_Exceptions_Match()
        {
            RemoteInvoke(() =>
            {
                IWebProxy p;

                Environment.SetEnvironmentVariable("no_proxy", ".test.com,, foo.com");
                Environment.SetEnvironmentVariable("all_proxy", "http://foo:bar@1.1.1.1:3000");
                Assert.True(HttpEnvironmentProxy.TryCreate(out p));
                Assert.NotNull(p);

                Assert.True(p.IsBypassed(fooHttp));
                Assert.True(p.IsBypassed(fooHttps));
                Assert.True(p.IsBypassed(new Uri("http://test.com")));
                Assert.False(p.IsBypassed(new Uri("http://1test.com")));
                Assert.True(p.IsBypassed(new Uri("http://www.test.com")));

                return SuccessExitCode;
           }).Dispose();
        }
    }
}