Commit 6e9016cf authored by justcoding121's avatar justcoding121

cleanup integration tests

parent 2e0864c4
...@@ -15,11 +15,10 @@ namespace Titanium.Web.Proxy.IntegrationTests ...@@ -15,11 +15,10 @@ namespace Titanium.Web.Proxy.IntegrationTests
public async Task Can_Intercept_Post_Requests() public async Task Can_Intercept_Post_Requests()
{ {
string testUrl = "http://interceptthis.com"; string testUrl = "http://interceptthis.com";
int proxyPort = 8086;
var proxy = new ProxyTestController();
proxy.StartProxy(proxyPort);
using (var client = TestHelper.CreateHttpClient(testUrl, proxyPort)) using (var proxy = new ProxyTestController())
{
using (var client = TestHelper.CreateHttpClient(testUrl, proxy.ListeningPort))
{ {
var response = await client.PostAsync(new Uri(testUrl), new StringContent("hello!")); var response = await client.PostAsync(new Uri(testUrl), new StringContent("hello!"));
...@@ -29,32 +28,23 @@ namespace Titanium.Web.Proxy.IntegrationTests ...@@ -29,32 +28,23 @@ namespace Titanium.Web.Proxy.IntegrationTests
Assert.IsTrue(body.Contains("TitaniumWebProxy-Stopped!!")); Assert.IsTrue(body.Contains("TitaniumWebProxy-Stopped!!"));
} }
} }
}
private class ProxyTestController private class ProxyTestController : IDisposable
{ {
private readonly ProxyServer proxyServer; private readonly ProxyServer proxyServer;
public int ListeningPort => proxyServer.ProxyEndPoints[0].Port;
public ProxyTestController() public ProxyTestController()
{ {
proxyServer = new ProxyServer(); proxyServer = new ProxyServer();
proxyServer.CertificateManager.RootCertificateName = "root-certificate-for-integration-test.pfx"; proxyServer.CertificateManager.RootCertificateName = "root-certificate-for-integration-test.pfx";
} var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 0, true);
public void StartProxy(int proxyPort)
{
proxyServer.BeforeRequest += OnRequest;
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true);
proxyServer.AddEndPoint(explicitEndPoint); proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.BeforeRequest += OnRequest;
proxyServer.Start(); proxyServer.Start();
} }
public void Stop()
{
proxyServer.BeforeRequest -= OnRequest;
proxyServer.Stop();
proxyServer.Dispose();
}
public async Task OnRequest(object sender, SessionEventArgs e) public async Task OnRequest(object sender, SessionEventArgs e)
{ {
if (e.WebSession.Request.Url.Contains("interceptthis.com")) if (e.WebSession.Request.Url.Contains("interceptthis.com"))
...@@ -70,6 +60,14 @@ namespace Titanium.Web.Proxy.IntegrationTests ...@@ -70,6 +60,14 @@ namespace Titanium.Web.Proxy.IntegrationTests
await Task.FromResult(0); await Task.FromResult(0);
} }
public void Dispose()
{
proxyServer.BeforeRequest -= OnRequest;
proxyServer.Stop();
proxyServer.Dispose();
}
} }
} }
} }
\ No newline at end of file
using System; using System;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.IntegrationTests namespace Titanium.Web.Proxy.IntegrationTests
...@@ -16,41 +13,35 @@ namespace Titanium.Web.Proxy.IntegrationTests ...@@ -16,41 +13,35 @@ namespace Titanium.Web.Proxy.IntegrationTests
public async Task TestSsl() public async Task TestSsl()
{ {
string testUrl = "https://google.com"; string testUrl = "https://google.com";
int proxyPort = 8086; using (var proxy = new ProxyTestController())
var proxy = new ProxyTestController(); {
proxy.StartProxy(proxyPort); using (var client = TestHelper.CreateHttpClient(testUrl, proxy.ListeningPort))
using (var client = TestHelper.CreateHttpClient(testUrl, proxyPort))
{ {
var response = await client.GetAsync(new Uri(testUrl)); var response = await client.GetAsync(new Uri(testUrl));
Assert.IsNotNull(response); Assert.IsNotNull(response);
} }
} }
}
private class ProxyTestController : IDisposable
private class ProxyTestController
{ {
private readonly ProxyServer proxyServer; private readonly ProxyServer proxyServer;
public int ListeningPort => proxyServer.ProxyEndPoints[0].Port;
public ProxyTestController() public ProxyTestController()
{ {
proxyServer = new ProxyServer(); proxyServer = new ProxyServer();
proxyServer.CertificateManager.RootCertificateName = "root-certificate-for-integration-test.pfx"; proxyServer.CertificateManager.RootCertificateName = "root-certificate-for-integration-test.pfx";
} var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 0, true);
public void StartProxy(int proxyPort)
{
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true);
proxyServer.AddEndPoint(explicitEndPoint); proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start(); proxyServer.Start();
} }
public void Stop() public void Dispose()
{ {
proxyServer.Stop(); proxyServer.Stop();
proxyServer.Dispose(); proxyServer.Dispose();
} }
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment