Commit 5cb49c70 authored by jmh76's avatar jmh76

Merge remote-tracking branch 'upstream/master'

parents 9570acbb 771ccfb4
......@@ -321,7 +321,7 @@ namespace Titanium.Web.Proxy
calledRequestHandler = true;
// Now create the request
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
cancellationTokenSource, connectHostname, connectArgs?.HttpClient.ConnectRequest, prefetchConnectionTask);
cancellationTokenSource, connectHostname, connectArgs, prefetchConnectionTask);
}
catch (ProxyException e)
{
......
......@@ -47,9 +47,11 @@ namespace Titanium.Web.Proxy
/// <param name="prefetchConnectionTask">Prefetched server connection for current client using Connect/SNI headers.</param>
private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, TcpClientConnection clientConnection,
CustomBufferedStream clientStream, HttpResponseWriter clientStreamWriter,
CancellationTokenSource cancellationTokenSource, string httpsConnectHostname, ConnectRequest connectRequest,
CancellationTokenSource cancellationTokenSource, string httpsConnectHostname, TunnelConnectSessionEventArgs connectArgs,
Task<TcpServerConnection> prefetchConnectionTask = null)
{
var connectRequest = connectArgs?.HttpClient.ConnectRequest;
var prefetchTask = prefetchConnectionTask;
TcpServerConnection connection = null;
bool closeServerConnection = false;
......@@ -73,7 +75,8 @@ namespace Titanium.Web.Proxy
var args = new SessionEventArgs(this, endPoint, cancellationTokenSource)
{
ProxyClient = { Connection = clientConnection },
HttpClient = { ConnectRequest = connectRequest }
HttpClient = { ConnectRequest = connectRequest },
UserData = connectArgs?.UserData
};
try
......@@ -204,7 +207,7 @@ namespace Titanium.Web.Proxy
//update connection to latest used
connection = result.LatestConnection;
closeServerConnection = !result.Continue;
closeServerConnection = !result.Continue;
//throw if exception happened
if (!result.IsSuccess)
......
......@@ -6,14 +6,15 @@ namespace Titanium.Web.Proxy.IntegrationTests.Helpers
{
public static class TestHelper
{
public static HttpClient GetHttpClient(int localProxyPort)
public static HttpClient GetHttpClient(int localProxyPort,
bool enableBasicProxyAuthorization = false)
{
var proxy = new TestProxy($"http://localhost:{localProxyPort}");
var proxy = new TestProxy($"http://localhost:{localProxyPort}", enableBasicProxyAuthorization);
var handler = new HttpClientHandler
{
Proxy = proxy,
UseProxy = true
UseProxy = true
};
return new HttpClient(handler);
......@@ -29,12 +30,16 @@ namespace Titanium.Web.Proxy.IntegrationTests.Helpers
public Uri ProxyUri { get; set; }
public ICredentials Credentials { get; set; }
public TestProxy(string proxyUri)
public TestProxy(string proxyUri, bool enableAuthorization)
: this(new Uri(proxyUri))
{
if (enableAuthorization)
{
Credentials = new NetworkCredential("test", "Test56");
}
}
public TestProxy(Uri proxyUri)
private TestProxy(Uri proxyUri)
{
this.ProxyUri = proxyUri;
}
......
......@@ -3,6 +3,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace Titanium.Web.Proxy.IntegrationTests
......@@ -35,5 +37,47 @@ namespace Titanium.Web.Proxy.IntegrationTests
Assert.AreEqual("I am server. I received your greetings.", body);
}
[TestMethod]
public async Task Smoke_Test_Nested_Proxy_UserData()
{
var testSuite = new TestSuite();
var server = testSuite.GetServer();
server.HandleRequest((context) =>
{
return context.Response.WriteAsync("I am server. I received your greetings.");
});
var proxy1 = testSuite.GetProxy();
proxy1.ProxyBasicAuthenticateFunc = async (session, username, password) =>
{
session.UserData = "Test";
return await Task.FromResult(true);
};
var proxy2 = testSuite.GetProxy();
proxy1.GetCustomUpStreamProxyFunc = async (session) =>
{
Assert.AreEqual("Test", session.UserData);
return await Task.FromResult(new Models.ExternalProxy()
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port
});
};
var client = testSuite.GetClient(proxy1, true);
var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client."));
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
Assert.AreEqual("I am server. I received your greetings.", body);
}
}
}
......@@ -43,9 +43,9 @@ namespace Titanium.Web.Proxy.IntegrationTests
return new TestProxyServer(true).ProxyServer;
}
public HttpClient GetClient(ProxyServer proxyServer)
public HttpClient GetClient(ProxyServer proxyServer, bool enableBasicProxyAuthorization = false)
{
return TestHelper.GetHttpClient(proxyServer.ProxyEndPoints[0].Port);
return TestHelper.GetHttpClient(proxyServer.ProxyEndPoints[0].Port, enableBasicProxyAuthorization);
}
public HttpClient GetReverseProxyClient()
......
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