diff --git a/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs b/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs index 03f98065c..0e0068f27 100644 --- a/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs +++ b/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs @@ -179,7 +179,7 @@ private async Task readBodyAsync(bool isRequest, CancellationToken cance internal async Task SyphonOutBodyAsync(bool isRequest, CancellationToken cancellationToken) { var requestResponse = isRequest ? (RequestResponseBase)WebSession.Request : WebSession.Response; - if (requestResponse.OriginalIsBodyRead || !requestResponse.OriginalHasBody) + if (requestResponse.IsBodyRead || !requestResponse.OriginalHasBody) { return; } diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/InterceptionTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/InterceptionTests.cs new file mode 100644 index 000000000..fd3971573 --- /dev/null +++ b/tests/Titanium.Web.Proxy.IntegrationTests/InterceptionTests.cs @@ -0,0 +1,46 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Net.Mime; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Titanium.Web.Proxy.IntegrationTests +{ + [TestClass] + public class InterceptionTests + { + [TestMethod] + public void CanInterceptPostRequests() + { + string testUrl = "http://interceptthis.com"; + int proxyPort = 8086; + var proxy = new ProxyTestController(); + proxy.StartProxy(proxyPort); + + using (var client = CreateHttpClient(testUrl, proxyPort)) + { + var response = client.PostAsync(new Uri(testUrl), new StringContent("hello!")).Result; + + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + var body = response.Content.ReadAsStringAsync().Result; + Assert.IsTrue(body.Contains("TitaniumWebProxy-Stopped!!")); + } + } + + private HttpClient CreateHttpClient(string url, int localProxyPort) + { + var handler = new HttpClientHandler + { + Proxy = new WebProxy($"http://localhost:{localProxyPort}", false), + UseProxy = true + }; + + var client = new HttpClient(handler); + + return client; + } + + } +} \ No newline at end of file diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/ProxyTestController.cs b/tests/Titanium.Web.Proxy.IntegrationTests/ProxyTestController.cs new file mode 100644 index 000000000..855fb7d0a --- /dev/null +++ b/tests/Titanium.Web.Proxy.IntegrationTests/ProxyTestController.cs @@ -0,0 +1,105 @@ +using System; +using System.Diagnostics; +using System.Net; +using System.Net.Security; +using System.Threading.Tasks; +using Titanium.Web.Proxy.EventArguments; +using Titanium.Web.Proxy.Models; + +namespace Titanium.Web.Proxy.IntegrationTests +{ + public class ProxyTestController + { + private readonly ProxyServer proxyServer; + + public ProxyTestController() + { + proxyServer = new ProxyServer(); + } + + public void StartProxy(int proxyPort) + { + proxyServer.BeforeRequest += OnRequest; + proxyServer.BeforeResponse += OnResponse; + proxyServer.ServerCertificateValidationCallback += OnCertificateValidation; + proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection; + + var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true); + + // An explicit endpoint is where the client knows about the existance of a proxy + // So client sends request in a proxy friendly manner + proxyServer.AddEndPoint(explicitEndPoint); + proxyServer.Start(); + + foreach (var endPoint in proxyServer.ProxyEndPoints) + { + Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", + endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); + } + } + + public void Stop() + { + proxyServer.BeforeRequest -= OnRequest; + proxyServer.BeforeResponse -= OnResponse; + proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation; + proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection; + + proxyServer.Stop(); + } + + // intecept & cancel, redirect or update requests + public async Task OnRequest(object sender, SessionEventArgs e) + { + Debug.WriteLine(e.WebSession.Request.Url); + + if (e.WebSession.Request.Url.Contains("interceptthis.com")) + { + if (e.WebSession.Request.HasBody) + { + var body = await e.GetRequestBodyAsString(); + } + + e.Ok("TitaniumWebProxy-Stopped!!"); + return; + } + + + await Task.FromResult(0); + } + + // Modify response + public async Task OnResponse(object sender, SessionEventArgs e) + { + await Task.FromResult(0); + } + + /// + /// Allows overriding default certificate validation logic + /// + /// + /// + public Task OnCertificateValidation(object sender, CertificateValidationEventArgs e) + { + // set IsValid to true/false based on Certificate Errors + if (e.SslPolicyErrors == SslPolicyErrors.None) + { + e.IsValid = true; + } + + return Task.FromResult(0); + } + + /// + /// Allows overriding default client certificate selection logic during mutual authentication + /// + /// + /// + public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e) + { + // set e.clientCertificate to override + + return Task.FromResult(0); + } + } +} \ No newline at end of file diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/SslTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/SslTests.cs index 2e8311592..87893d2a5 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/SslTests.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/SslTests.cs @@ -1,12 +1,7 @@ using System; -using System.Diagnostics; using System.Net; using System.Net.Http; -using System.Net.Security; -using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Titanium.Web.Proxy.EventArguments; -using Titanium.Web.Proxy.Models; namespace Titanium.Web.Proxy.IntegrationTests { @@ -43,86 +38,4 @@ private HttpClient CreateHttpClient(string url, int localProxyPort) return client; } } - - public class ProxyTestController - { - private readonly ProxyServer proxyServer; - - public ProxyTestController() - { - proxyServer = new ProxyServer(); - } - - public void StartProxy(int proxyPort) - { - proxyServer.BeforeRequest += OnRequest; - proxyServer.BeforeResponse += OnResponse; - proxyServer.ServerCertificateValidationCallback += OnCertificateValidation; - proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection; - - var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true); - - // An explicit endpoint is where the client knows about the existance of a proxy - // So client sends request in a proxy friendly manner - proxyServer.AddEndPoint(explicitEndPoint); - proxyServer.Start(); - - foreach (var endPoint in proxyServer.ProxyEndPoints) - { - Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", - endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); - } - } - - public void Stop() - { - proxyServer.BeforeRequest -= OnRequest; - proxyServer.BeforeResponse -= OnResponse; - proxyServer.ServerCertificateValidationCallback -= OnCertificateValidation; - proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection; - - proxyServer.Stop(); - } - - // intecept & cancel, redirect or update requests - public async Task OnRequest(object sender, SessionEventArgs e) - { - Debug.WriteLine(e.WebSession.Request.Url); - await Task.FromResult(0); - } - - // Modify response - public async Task OnResponse(object sender, SessionEventArgs e) - { - await Task.FromResult(0); - } - - /// - /// Allows overriding default certificate validation logic - /// - /// - /// - public Task OnCertificateValidation(object sender, CertificateValidationEventArgs e) - { - // set IsValid to true/false based on Certificate Errors - if (e.SslPolicyErrors == SslPolicyErrors.None) - { - e.IsValid = true; - } - - return Task.FromResult(0); - } - - /// - /// Allows overriding default client certificate selection logic during mutual authentication - /// - /// - /// - public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e) - { - // set e.clientCertificate to override - - return Task.FromResult(0); - } - } } diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/Titanium.Web.Proxy.IntegrationTests.csproj b/tests/Titanium.Web.Proxy.IntegrationTests/Titanium.Web.Proxy.IntegrationTests.csproj index f3ddfd026..2dfb9b2d0 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/Titanium.Web.Proxy.IntegrationTests.csproj +++ b/tests/Titanium.Web.Proxy.IntegrationTests/Titanium.Web.Proxy.IntegrationTests.csproj @@ -70,6 +70,8 @@ + +