From 73e99a937751ba466ab949272e146d7cec706a02 Mon Sep 17 00:00:00 2001 From: tqk2811 Date: Fri, 22 Dec 2023 17:37:14 +0700 Subject: [PATCH] rework unit test --- src/TestProxy/BaseBindTest.cs | 12 ++ src/TestProxy/BaseClassTest.cs | 41 +++++ ...lProxySourceTest.cs => BaseConnectTest.cs} | 62 ++------ src/TestProxy/BaseUdpTest.cs | 7 + .../HttpProxySourceIpV6Test.cs | 146 ------------------ .../HttpProxySourceTest.cs | 146 ------------------ .../LocalProxySourceIpV6Test.cs | 126 --------------- .../LocalProxySourceTest.cs | 133 ---------------- .../HttpProxySourceIpV6Test.cs | 35 +++++ .../HttpProxySourceTest.cs | 35 +++++ .../ServerTest/HttpProxyServerIpV6Test.cs | 44 ++++++ .../ServerTest/HttpProxyServerTest.cs | 44 ++++++ .../ServerTest/Socks4ProxyServerTest.cs | 38 +++++ .../ServerTest/Socks5ProxyServerTest.cs | 38 +++++ src/TestProxy/Singleton.cs | 22 --- .../LocalProxySourceTest.cs | 121 --------------- src/TestProxy/TestProxy.csproj | 1 - 17 files changed, 305 insertions(+), 746 deletions(-) create mode 100644 src/TestProxy/BaseBindTest.cs create mode 100644 src/TestProxy/BaseClassTest.cs rename src/TestProxy/{Socks4ProxyServerTest/LocalProxySourceTest.cs => BaseConnectTest.cs} (60%) create mode 100644 src/TestProxy/BaseUdpTest.cs delete mode 100644 src/TestProxy/HttpProxyServerTest/HttpProxySourceIpV6Test.cs delete mode 100644 src/TestProxy/HttpProxyServerTest/HttpProxySourceTest.cs delete mode 100644 src/TestProxy/HttpProxyServerTest/LocalProxySourceIpV6Test.cs delete mode 100644 src/TestProxy/HttpProxyServerTest/LocalProxySourceTest.cs create mode 100644 src/TestProxy/HttpProxySourceTest/HttpProxySourceIpV6Test.cs create mode 100644 src/TestProxy/HttpProxySourceTest/HttpProxySourceTest.cs create mode 100644 src/TestProxy/ServerTest/HttpProxyServerIpV6Test.cs create mode 100644 src/TestProxy/ServerTest/HttpProxyServerTest.cs create mode 100644 src/TestProxy/ServerTest/Socks4ProxyServerTest.cs create mode 100644 src/TestProxy/ServerTest/Socks5ProxyServerTest.cs delete mode 100644 src/TestProxy/Singleton.cs delete mode 100644 src/TestProxy/Socks5ProxyServerTest/LocalProxySourceTest.cs diff --git a/src/TestProxy/BaseBindTest.cs b/src/TestProxy/BaseBindTest.cs new file mode 100644 index 0000000..ffb61d3 --- /dev/null +++ b/src/TestProxy/BaseBindTest.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TestProxy +{ + public abstract class BaseBindTest : BaseConnectTest + { + } +} diff --git a/src/TestProxy/BaseClassTest.cs b/src/TestProxy/BaseClassTest.cs new file mode 100644 index 0000000..2268529 --- /dev/null +++ b/src/TestProxy/BaseClassTest.cs @@ -0,0 +1,41 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxyServers; + +namespace TestProxy +{ + public abstract class BaseClassTest : IDisposable + { + protected readonly HttpClient _httpClient; + readonly BaseProxyServer _proxyServer; + + protected BaseClassTest() + { + _proxyServer = CreateServer(GetProxySource()); + _proxyServer.StartListen(); + _httpClient = new HttpClient(CreateHttpMessageHandler(_proxyServer), true); + } + ~BaseClassTest() + { + Dispose(false); + } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + protected virtual void Dispose(bool isDisposing) + { + _httpClient.Dispose(); + _proxyServer.Dispose(); + } + protected abstract IProxySource GetProxySource(); + protected abstract BaseProxyServer CreateServer(IProxySource proxySource); + protected abstract HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer); + } +} diff --git a/src/TestProxy/Socks4ProxyServerTest/LocalProxySourceTest.cs b/src/TestProxy/BaseConnectTest.cs similarity index 60% rename from src/TestProxy/Socks4ProxyServerTest/LocalProxySourceTest.cs rename to src/TestProxy/BaseConnectTest.cs index 7c46e03..8d60a52 100644 --- a/src/TestProxy/Socks4ProxyServerTest/LocalProxySourceTest.cs +++ b/src/TestProxy/BaseConnectTest.cs @@ -1,48 +1,20 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; -using System.Net; +using System.Net.Http; using System.Text; using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxySources; -using TqkLibrary.Proxy.ProxyServers; -using Newtonsoft.Json; -namespace TestProxy.Socks4ProxyServerTest +namespace TestProxy { - [TestClass] - public class LocalProxySourceTest + public abstract class BaseConnectTest : BaseClassTest { - static readonly IProxySource localProxySource; - - static readonly SocketsHttpHandler httpClientHandler; - static readonly HttpClient httpClient; - static LocalProxySourceTest() - { - localProxySource = new LocalProxySource(); - //.Net6 support socks4 and socks5 - //https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#socks-proxy-support - httpClientHandler = new SocketsHttpHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"socks4://{Singleton.Address0}"), - }, - UseCookies = false, - UseProxy = true, - }; - httpClient = new HttpClient(httpClientHandler, false); - } - [TestMethod] public async Task HttpGet() { - using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks4ProxyServer.StartListen(); - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); dynamic json = JsonConvert.DeserializeObject(content); Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); @@ -51,12 +23,9 @@ public async Task HttpGet() [TestMethod] public async Task HttpGetTwoTimes() { - using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks4ProxyServer.StartListen(); - { using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); dynamic json = JsonConvert.DeserializeObject(content); Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); @@ -66,7 +35,7 @@ public async Task HttpGetTwoTimes() { //github will redirect (301) http -> https -> new connection proxy using CONNECT method using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); Assert.AreEqual(content, "TqkLibrary.Proxy data"); } @@ -75,13 +44,10 @@ public async Task HttpGetTwoTimes() [TestMethod] public async Task HttpPost() { - using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks4ProxyServer.StartListen(); - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); httpRequestMessage.Headers.Add("Accept", "application/json"); httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); dynamic json = JsonConvert.DeserializeObject(content); Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/post"); @@ -92,11 +58,8 @@ public async Task HttpPost() [TestMethod] public async Task HttpsGet() { - using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks4ProxyServer.StartListen(); - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); dynamic json = JsonConvert.DeserializeObject(content); Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/get"); @@ -105,13 +68,10 @@ public async Task HttpsGet() [TestMethod] public async Task HttpsPost() { - using var socks4ProxyServer = new Socks4ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks4ProxyServer.StartListen(); - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); httpRequestMessage.Headers.Add("Accept", "application/json"); httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); + using HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); string content = await httpResponseMessage.Content.ReadAsStringAsync(); dynamic json = JsonConvert.DeserializeObject(content); Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/post"); diff --git a/src/TestProxy/BaseUdpTest.cs b/src/TestProxy/BaseUdpTest.cs new file mode 100644 index 0000000..3886c27 --- /dev/null +++ b/src/TestProxy/BaseUdpTest.cs @@ -0,0 +1,7 @@ +namespace TestProxy +{ + public abstract class BaseUdpTest : BaseBindTest + { + + } +} diff --git a/src/TestProxy/HttpProxyServerTest/HttpProxySourceIpV6Test.cs b/src/TestProxy/HttpProxyServerTest/HttpProxySourceIpV6Test.cs deleted file mode 100644 index c7561bc..0000000 --- a/src/TestProxy/HttpProxyServerTest/HttpProxySourceIpV6Test.cs +++ /dev/null @@ -1,146 +0,0 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxyServers; -using TqkLibrary.Proxy.ProxySources; - -namespace TestProxy.HttpProxyServerTest -{ - [TestClass] - public class HttpProxySourceIpV6Test - { - static readonly IProxySource localProxySource; - - static readonly HttpClientHandler httpClientHandler; - static readonly HttpClient httpClient; - - static HttpProxySourceIpV6Test() - { - localProxySource = new LocalProxySource(); - - httpClientHandler = new HttpClientHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"http://{Singleton.AddressIpv6_1}"), - }, - UseCookies = false, - }; - httpClient = new HttpClient(httpClientHandler, false); - } - - - [TestMethod] - public async Task HttpGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.AddressIpv6_0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpGetTwoTimes() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.AddressIpv6_0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - { - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - //Test make new request on 1 connection with proxy - { - //github will redirect (301) http -> https -> new connection proxy using CONNECT method - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - Assert.AreEqual(content, "TqkLibrary.Proxy data"); - } - } - - [TestMethod] - public async Task HttpPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.AddressIpv6_0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - - - [TestMethod] - public async Task HttpsGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.AddressIpv6_0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpsPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.AddressIpv6_0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - } -} diff --git a/src/TestProxy/HttpProxyServerTest/HttpProxySourceTest.cs b/src/TestProxy/HttpProxyServerTest/HttpProxySourceTest.cs deleted file mode 100644 index 54c00a8..0000000 --- a/src/TestProxy/HttpProxyServerTest/HttpProxySourceTest.cs +++ /dev/null @@ -1,146 +0,0 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxyServers; -using TqkLibrary.Proxy.ProxySources; - -namespace TestProxy.HttpProxyServerTest -{ - [TestClass] - public class HttpProxySourceTest - { - static readonly IProxySource localProxySource; - - static readonly HttpClientHandler httpClientHandler; - static readonly HttpClient httpClient; - - static HttpProxySourceTest() - { - localProxySource = new LocalProxySource(); - - httpClientHandler = new HttpClientHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"http://{Singleton.Address1}"), - }, - UseCookies = false, - }; - httpClient = new HttpClient(httpClientHandler, false); - } - - - [TestMethod] - public async Task HttpGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.Address0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpGetTwoTimes() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.Address0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - { - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - //Test make new request on 1 connection with proxy - { - //github will redirect (301) http -> https -> new connection proxy using CONNECT method - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - Assert.AreEqual(content, "TqkLibrary.Proxy data"); - } - } - - [TestMethod] - public async Task HttpPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.Address0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - - - [TestMethod] - public async Task HttpsGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.Address0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpsPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource);//self host - httpProxyServer.StartListen(); - - HttpProxySource httpProxySource = new HttpProxySource(new Uri($"http://{Singleton.Address0}"));//connect to self host - using var httpProxyServer1 = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address1), localProxySource);//create new server on Address1 - httpProxyServer1.StartListen(); - - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - } -} diff --git a/src/TestProxy/HttpProxyServerTest/LocalProxySourceIpV6Test.cs b/src/TestProxy/HttpProxyServerTest/LocalProxySourceIpV6Test.cs deleted file mode 100644 index 547ebcd..0000000 --- a/src/TestProxy/HttpProxyServerTest/LocalProxySourceIpV6Test.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxySources; -using TqkLibrary.Proxy.ProxyServers; -using Newtonsoft.Json; -using TqkLibrary.Proxy.Handlers; -using TqkLibrary.Proxy.Authentications; - -namespace TestProxy.HttpProxyServerTest -{ - [TestClass] - public class LocalProxySourceIpV6Test - { - static readonly IProxySource localProxySource; - - static readonly HttpClientHandler httpClientHandler; - static readonly HttpClient httpClient; - static readonly NetworkCredential networkCredential = new NetworkCredential("user", "password"); - static readonly HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(); - static LocalProxySourceIpV6Test() - { - localProxySource = new LocalProxySource(); - - httpClientHandler = new HttpClientHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"http://{Singleton.AddressIpv6_0}"), - }, - UseCookies = false, - UseProxy = true, - DefaultProxyCredentials = networkCredential, - }; - httpClient = new HttpClient(httpClientHandler, false); - handler.WithAuthentications(networkCredential); - } - - [TestMethod] - public async Task HttpGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpGetTwoTimes() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, handler); - httpProxyServer.StartListen(); - - { - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/get"); - } - - //Test make new request on 1 connection with proxy - { - //github will redirect (301) http -> https -> new connection proxy using CONNECT method - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - Assert.AreEqual(content, "TqkLibrary.Proxy data"); - } - } - - [TestMethod] - public async Task HttpPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "http://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - - - [TestMethod] - public async Task HttpsGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpsPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.AddressIpv6_0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"].ToString(), "https://httpbin.org/post"); - Assert.AreEqual(json["data"].ToString(), "Test post"); - } - } -} diff --git a/src/TestProxy/HttpProxyServerTest/LocalProxySourceTest.cs b/src/TestProxy/HttpProxyServerTest/LocalProxySourceTest.cs deleted file mode 100644 index 4024f45..0000000 --- a/src/TestProxy/HttpProxyServerTest/LocalProxySourceTest.cs +++ /dev/null @@ -1,133 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxySources; -using TqkLibrary.Proxy.ProxyServers; -using Newtonsoft.Json; -using TqkLibrary.Proxy.Handlers; -using TqkLibrary.Proxy.Authentications; - -namespace TestProxy.HttpProxyServerTest -{ - [TestClass] - public class LocalProxySourceTest - { - static readonly IProxySource localProxySource; - - static readonly HttpClientHandler httpClientHandler; - static readonly HttpClient httpClient; - static readonly NetworkCredential networkCredential = new NetworkCredential("user", "password"); - static readonly HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(); - static LocalProxySourceTest() - { - localProxySource = new LocalProxySource(); - - httpClientHandler = new HttpClientHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"http://{Singleton.Address0}"), - }, - UseCookies = false, - UseProxy = true, - DefaultProxyCredentials = networkCredential, - }; - httpClient = new HttpClient(httpClientHandler, false); - handler.WithAuthentications(networkCredential); - } - - //------------------Connect Test----------------// - [TestMethod] - public async Task HttpGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpGetTwoTimes() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, handler); - httpProxyServer.StartListen(); - - { - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); - } - - //Test make new request on 1 connection with proxy - { - //github will redirect (301) http -> https -> new connection proxy using CONNECT method - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - Assert.AreEqual(content, "TqkLibrary.Proxy data"); - } - } - - [TestMethod] - public async Task HttpPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/post"); - Assert.AreEqual(json["data"]?.ToString(), "Test post"); - } - - - [TestMethod] - public async Task HttpsGet() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpsPost() - { - using var httpProxyServer = new HttpProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource, handler); - httpProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/post"); - Assert.AreEqual(json["data"]?.ToString(), "Test post"); - } - - - //------------------Bind Test----------------// - - - - } -} diff --git a/src/TestProxy/HttpProxySourceTest/HttpProxySourceIpV6Test.cs b/src/TestProxy/HttpProxySourceTest/HttpProxySourceIpV6Test.cs new file mode 100644 index 0000000..1afdf14 --- /dev/null +++ b/src/TestProxy/HttpProxySourceTest/HttpProxySourceIpV6Test.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TestProxy.ServerTest; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxyServers; +using TqkLibrary.Proxy.ProxySources; + +namespace TestProxy.HttpProxySourceTest +{ + [TestClass] + public class HttpProxySourceIpV6Test: HttpProxyServerIpV6Test + { + IProxySource _proxySource; + BaseProxyServer _proxyServer2; + protected override IProxySource GetProxySource() + { + _proxySource = base.GetProxySource(); + _proxyServer2 = new HttpProxyServer(IPEndPoint.Parse("[::1]:0"), _proxySource); + _proxyServer2.StartListen(); + + return new HttpProxySource(new Uri($"http://{_proxyServer2.IPEndPoint}"), _networkCredential); + } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + _proxyServer2.Dispose(); + } + } +} diff --git a/src/TestProxy/HttpProxySourceTest/HttpProxySourceTest.cs b/src/TestProxy/HttpProxySourceTest/HttpProxySourceTest.cs new file mode 100644 index 0000000..df87aae --- /dev/null +++ b/src/TestProxy/HttpProxySourceTest/HttpProxySourceTest.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TestProxy.ServerTest; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxyServers; +using TqkLibrary.Proxy.ProxySources; + +namespace TestProxy.HttpProxySourceTest +{ + [TestClass] + public class HttpProxySourceTest : HttpProxyServerTest + { + IProxySource _proxySource; + BaseProxyServer _proxyServer2; + protected override IProxySource GetProxySource() + { + _proxySource = base.GetProxySource(); + _proxyServer2 = new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:0"), _proxySource); + _proxyServer2.StartListen(); + + return new HttpProxySource(new Uri($"http://{_proxyServer2.IPEndPoint}"), _networkCredential); + } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + _proxyServer2.Dispose(); + } + } +} diff --git a/src/TestProxy/ServerTest/HttpProxyServerIpV6Test.cs b/src/TestProxy/ServerTest/HttpProxyServerIpV6Test.cs new file mode 100644 index 0000000..9c632f7 --- /dev/null +++ b/src/TestProxy/ServerTest/HttpProxyServerIpV6Test.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxySources; +using TqkLibrary.Proxy.ProxyServers; +using Newtonsoft.Json; +using TqkLibrary.Proxy.Handlers; +using TqkLibrary.Proxy.Authentications; + +namespace TestProxy.ServerTest +{ + [TestClass] + public class HttpProxyServerIpV6Test : BaseConnectTest + { + protected readonly NetworkCredential _networkCredential = new NetworkCredential("user", "password"); + protected override IProxySource GetProxySource() + { + return new LocalProxySource(); + } + protected override BaseProxyServer CreateServer(IProxySource proxySource) + { + HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(); + handler.WithAuthentications(_networkCredential); + return new HttpProxyServer(IPEndPoint.Parse("[::1]:0"), proxySource, handler); + } + protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer) + { + return new HttpClientHandler() + { + Proxy = new WebProxy() + { + Address = new Uri($"http://{baseProxyServer.IPEndPoint}"), + }, + UseCookies = false, + UseProxy = true, + DefaultProxyCredentials = _networkCredential, + }; + } + } +} diff --git a/src/TestProxy/ServerTest/HttpProxyServerTest.cs b/src/TestProxy/ServerTest/HttpProxyServerTest.cs new file mode 100644 index 0000000..4930e0d --- /dev/null +++ b/src/TestProxy/ServerTest/HttpProxyServerTest.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxySources; +using TqkLibrary.Proxy.ProxyServers; +using Newtonsoft.Json; +using TqkLibrary.Proxy.Handlers; +using TqkLibrary.Proxy.Authentications; + +namespace TestProxy.ServerTest +{ + [TestClass] + public class HttpProxyServerTest : BaseConnectTest + { + protected readonly NetworkCredential _networkCredential = new NetworkCredential("user", "password"); + protected override IProxySource GetProxySource() + { + return new LocalProxySource(); + } + protected override BaseProxyServer CreateServer(IProxySource proxySource) + { + HttpAuthenticationProxyServerHandler handler = new HttpAuthenticationProxyServerHandler(); + handler.WithAuthentications(_networkCredential); + return new HttpProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource, handler); + } + protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer) + { + return new HttpClientHandler() + { + Proxy = new WebProxy() + { + Address = new Uri($"http://{baseProxyServer.IPEndPoint}"), + }, + UseCookies = false, + UseProxy = true, + DefaultProxyCredentials = _networkCredential, + }; + } + } +} diff --git a/src/TestProxy/ServerTest/Socks4ProxyServerTest.cs b/src/TestProxy/ServerTest/Socks4ProxyServerTest.cs new file mode 100644 index 0000000..eb6f42d --- /dev/null +++ b/src/TestProxy/ServerTest/Socks4ProxyServerTest.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxySources; +using TqkLibrary.Proxy.ProxyServers; +using Newtonsoft.Json; + +namespace TestProxy.ServerTest +{ + [TestClass] + public class Socks4ProxyServerTest : BaseBindTest + { + protected override IProxySource GetProxySource() + { + return new LocalProxySource(); + } + protected override BaseProxyServer CreateServer(IProxySource proxySource) + { + return new Socks4ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource); + } + protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer) + { + return new SocketsHttpHandler() + { + Proxy = new WebProxy() + { + Address = new Uri($"socks4://{baseProxyServer.IPEndPoint}"), + }, + UseCookies = false, + UseProxy = true, + }; + } + } +} diff --git a/src/TestProxy/ServerTest/Socks5ProxyServerTest.cs b/src/TestProxy/ServerTest/Socks5ProxyServerTest.cs new file mode 100644 index 0000000..21e3cde --- /dev/null +++ b/src/TestProxy/ServerTest/Socks5ProxyServerTest.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using TqkLibrary.Proxy.Interfaces; +using TqkLibrary.Proxy.ProxySources; +using TqkLibrary.Proxy.ProxyServers; +using Newtonsoft.Json; + +namespace TestProxy.ServerTest +{ + [TestClass] + public class Socks5ProxyServerTest : BaseUdpTest + { + protected override IProxySource GetProxySource() + { + return new LocalProxySource(); + } + protected override BaseProxyServer CreateServer(IProxySource proxySource) + { + return new Socks5ProxyServer(IPEndPoint.Parse("127.0.0.1:0"), proxySource); + } + protected override HttpMessageHandler CreateHttpMessageHandler(BaseProxyServer baseProxyServer) + { + return new SocketsHttpHandler() + { + Proxy = new WebProxy() + { + Address = new Uri($"socks5://{baseProxyServer.IPEndPoint}"), + }, + UseCookies = false, + UseProxy = true, + }; + } + } +} diff --git a/src/TestProxy/Singleton.cs b/src/TestProxy/Singleton.cs deleted file mode 100644 index 9edbcb4..0000000 --- a/src/TestProxy/Singleton.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TestProxy -{ - internal static class Singleton - { - static Singleton() - { - TqkLibrary.Proxy.Singleton.LoggerFactory = LoggerFactory.Create(x => x.AddConsole()); - } - internal const string Address0 = "127.0.0.1:13566"; - internal const string Address1 = "127.0.0.1:13567"; - - internal const string AddressIpv6_0 = "[::1]:13566"; - internal const string AddressIpv6_1 = "[::1]:13567"; - } -} diff --git a/src/TestProxy/Socks5ProxyServerTest/LocalProxySourceTest.cs b/src/TestProxy/Socks5ProxyServerTest/LocalProxySourceTest.cs deleted file mode 100644 index 0ae71a5..0000000 --- a/src/TestProxy/Socks5ProxyServerTest/LocalProxySourceTest.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using TqkLibrary.Proxy.Interfaces; -using TqkLibrary.Proxy.ProxySources; -using TqkLibrary.Proxy.ProxyServers; -using Newtonsoft.Json; - -namespace TestProxy.Socks5ProxyServerTest -{ - [TestClass] - public class LocalProxySourceTest - { - static readonly IProxySource localProxySource; - - static readonly SocketsHttpHandler httpClientHandler; - static readonly HttpClient httpClient; - static LocalProxySourceTest() - { - localProxySource = new LocalProxySource(); - //.Net6 support socks4 and socks5 - //https://devblogs.microsoft.com/dotnet/dotnet-6-networking-improvements/#socks-proxy-support - httpClientHandler = new SocketsHttpHandler() - { - Proxy = new WebProxy() - { - Address = new Uri($"socks5://{Singleton.Address0}"), - }, - UseCookies = false, - UseProxy = true, - }; - httpClient = new HttpClient(httpClientHandler, false); - } - - [TestMethod] - public async Task HttpGet() - { - using var socks5ProxyServer = new Socks5ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks5ProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpGetTwoTimes() - { - using var socks5ProxyServer = new Socks5ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks5ProxyServer.StartListen(); - - { - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/get"); - } - - //Test make new request on 1 connection with proxy - { - //github will redirect (301) http -> https -> new connection proxy using CONNECT method - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://tqk2811.github.io/TqkLibrary.Proxy/Test.txt"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - Assert.AreEqual(content, "TqkLibrary.Proxy data"); - } - } - - [TestMethod] - public async Task HttpPost() - { - using var socks5ProxyServer = new Socks5ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks5ProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "http://httpbin.org/post"); - Assert.AreEqual(json["data"]?.ToString(), "Test post"); - } - - - [TestMethod] - public async Task HttpsGet() - { - using var socks5ProxyServer = new Socks5ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks5ProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://httpbin.org/get"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/get"); - } - - [TestMethod] - public async Task HttpsPost() - { - using var socks5ProxyServer = new Socks5ProxyServer(IPEndPoint.Parse(Singleton.Address0), localProxySource); - socks5ProxyServer.StartListen(); - - using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://httpbin.org/post"); - httpRequestMessage.Headers.Add("Accept", "application/json"); - httpRequestMessage.Content = new StringContent("Test post"); - using HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseHeadersRead); - string content = await httpResponseMessage.Content.ReadAsStringAsync(); - dynamic json = JsonConvert.DeserializeObject(content); - Assert.AreEqual(json["url"]?.ToString(), "https://httpbin.org/post"); - Assert.AreEqual(json["data"]?.ToString(), "Test post"); - } - } -} diff --git a/src/TestProxy/TestProxy.csproj b/src/TestProxy/TestProxy.csproj index a6ef308..a9289e4 100644 --- a/src/TestProxy/TestProxy.csproj +++ b/src/TestProxy/TestProxy.csproj @@ -12,7 +12,6 @@ -