|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
2 | 4 | using System.Net; |
3 | 5 | using System.Net.Http; |
4 | 6 | using System.Threading.Tasks; |
5 | 7 | using Microsoft.AspNetCore.Http; |
6 | 8 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 9 | +using Titanium.Web.Proxy.Models; |
7 | 10 |
|
8 | 11 | namespace Titanium.Web.Proxy.IntegrationTests |
9 | 12 | { |
@@ -73,5 +76,98 @@ public async Task Smoke_Test_Nested_Proxy_UserData() |
73 | 76 | Assert.AreEqual("I am server. I received your greetings.", body); |
74 | 77 | } |
75 | 78 |
|
| 79 | + //Try reproduce bug reported so that we can fix it. |
| 80 | + //https://github.com/justcoding121/titanium-web-proxy/issues/826 |
| 81 | + [TestMethod] |
| 82 | + public async Task Nested_Proxy_Farm_Should_Not_Hang() |
| 83 | + { |
| 84 | + var rnd = new Random(); |
| 85 | + |
| 86 | + var testSuite = new TestSuite(); |
| 87 | + |
| 88 | + var server = testSuite.GetServer(); |
| 89 | + server.HandleRequest((context) => |
| 90 | + { |
| 91 | + return context.Response.WriteAsync("I am server. I received your greetings."); |
| 92 | + }); |
| 93 | + |
| 94 | + var proxies2 = new List<ProxyServer>(); |
| 95 | + |
| 96 | + //create a level 2 upstream proxy farm |
| 97 | + for (int i = 0; i < 10; i++) |
| 98 | + { |
| 99 | + var proxy = testSuite.GetProxy(); |
| 100 | + proxy.ProxyBasicAuthenticateFunc += (_, _, _) => |
| 101 | + { |
| 102 | + return Task.FromResult(true); |
| 103 | + }; |
| 104 | + |
| 105 | + proxies2.Add(proxy); |
| 106 | + } |
| 107 | + |
| 108 | + var proxies1 = new List<ProxyServer>(); |
| 109 | + |
| 110 | + //create a level 1 upstream proxy farm |
| 111 | + for (int i = 0; i < 10; i++) |
| 112 | + { |
| 113 | + var proxy1 = testSuite.GetProxy(); |
| 114 | + var proxy2 = proxies2[rnd.Next() % proxies2.Count]; |
| 115 | + |
| 116 | + var explicitEndpoint = proxy1.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().First(); |
| 117 | + explicitEndpoint.BeforeTunnelConnectRequest += (_, e) => |
| 118 | + { |
| 119 | + e.CustomUpStreamProxy = new ExternalProxy() |
| 120 | + { |
| 121 | + HostName = "localhost", |
| 122 | + Port = proxy2.ProxyEndPoints[0].Port, |
| 123 | + ProxyType = ExternalProxyType.Http, |
| 124 | + UserName = "test_user", |
| 125 | + Password = "test_password" |
| 126 | + }; |
| 127 | + |
| 128 | + return Task.CompletedTask; |
| 129 | + }; |
| 130 | + |
| 131 | + proxy1.BeforeRequest += (_, e) => |
| 132 | + { |
| 133 | + e.CustomUpStreamProxy = new ExternalProxy() |
| 134 | + { |
| 135 | + HostName = "localhost", |
| 136 | + Port = proxy2.ProxyEndPoints[0].Port, |
| 137 | + ProxyType = ExternalProxyType.Http, |
| 138 | + UserName = "test_user", |
| 139 | + Password = "test_password" |
| 140 | + }; |
| 141 | + |
| 142 | + return Task.CompletedTask; |
| 143 | + }; |
| 144 | + |
| 145 | + proxies1.Add(proxy1); |
| 146 | + } |
| 147 | + |
| 148 | + var tasks = new List<Task>(); |
| 149 | + |
| 150 | + //send multiple concurrent requests from client => proxy farm 1 => proxy farm 2 => server |
| 151 | + for (int j = 0; j < 1000; j++) |
| 152 | + { |
| 153 | + var task = Task.Run(async () => |
| 154 | + { |
| 155 | + var proxy = proxies1[rnd.Next() % proxies1.Count]; |
| 156 | + using var client = testSuite.GetClient(proxy); |
| 157 | + |
| 158 | + var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl), |
| 159 | + new StringContent("hello server. I am a client.")); |
| 160 | + |
| 161 | + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); |
| 162 | + var body = await response.Content.ReadAsStringAsync(); |
| 163 | + |
| 164 | + Assert.AreEqual("I am server. I received your greetings.", body); |
| 165 | + }); |
| 166 | + |
| 167 | + tasks.Add(task); |
| 168 | + } |
| 169 | + |
| 170 | + await Task.WhenAll(tasks); |
| 171 | + } |
76 | 172 | } |
77 | 173 | } |
0 commit comments