public void TcpServer_Should_Dispose() { var tcpServer = new TestTcpServer(_tcpServerChannelFactory, _logger, _eventLoopGroupFactory); tcpServer.DisposeProxy(true); _logger.Received(1).Debug($"Disposing{typeof(TestTcpServer).Name}"); }
public void IsCaptureBodyTest() { var server = new TestTcpServer(); var engine = new DefaultEngine(server) { IsCaptureBody = false }; var clientTcp = new TestTcpClient(); server.AcceptTcp(clientTcp); var connection = engine.connections.Last(); connection.CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port); var tcsRequest = new TaskCompletionSource <IReadOnlyHttpRequest>(); connection.HttpRequestSent += r => tcsRequest.TrySetResult(r); var tcsSession = new TaskCompletionSource <IReadOnlySession>(); connection.HttpResponseSent += s => tcsSession.TrySetResult(s); var request = @"GET / HTTP/1.1 Host: hoge.example.com "; clientTcp.WriteToInput(request); tcsRequest.Task.Wait(5000); connection.serverConnection.client.AsTest().WriteFileToInput("TestData/ResponseChunked"); var session = tcsSession.GetResult(); (session.Request as HttpRequest).Source.Is(request); session.Response.ToString().Is(@"HTTP/1.1 200 OK Server: nginx Date: Thu, 20 Sep 2018 01:59:09 GMT Content-Type: application/javascript; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Cache-Control: private, no-cache, no-cache=""Set-Cookie"", proxy-revalidate Pragma: no-cache Access-Control-Allow-Origin: * P3P: CP=""ADM NOI OUR"" Content-Encoding: gzip "); session.Response.Body.Length.Is(0); (engine as IReadOnlyHttpProxyEngine).Stop(); }
public void TcpPingSuccessTest() { using (var server = new TestTcpServer(5)) { server.EventStatus += EventHandler; server.Start(_port); Thread.Sleep(1000); Assert.AreEqual(TestTcpServer.ServerWaitStatus, _serverOutput); using (var tcpRequest = new TcpConnection(new HostInput(_host, new TimeSpan(0, 0, 5)))) { tcpRequest.Ping().Wait(); Assert.IsTrue(tcpRequest.Response.Status == PingStatus.Ok && tcpRequest.Response.Code == 0); } } _serverOutput = null; }
public void HandlerExceptionTest() { var server = new TestTcpServer(); var engine = new DefaultEngine(server); var clientTcp = new TestTcpClient(); server.AcceptTcp(clientTcp); var connection = engine.connections.Last(); connection.CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port); var tcsRequest = new TaskCompletionSource <IReadOnlyHttpRequest>(); connection.HttpRequestSent += r => tcsRequest.TrySetResult(r); var tcsSession = new TaskCompletionSource <IReadOnlySession>(); (engine as IReadOnlyHttpProxyEngine).HttpResponseSent += (_, s) => tcsSession.TrySetResult(s.Session); (engine as IReadOnlyHttpProxyEngine).HttpResponseSent += (_, s) => throw new Exception(); var request = @"GET / HTTP/1.1 Host: hoge.example.com "; clientTcp.WriteToInput(request); tcsRequest.Task.Wait(5000); connection.serverConnection.client.AsTest().WriteFileToInput("TestData/ResponseChunked"); var session = tcsSession.GetResult(); engine.connections.Count.Is(1); (engine as IReadOnlyHttpProxyEngine).Stop(); }
public void AfterSessionCompleteTest() { var server = new TestTcpServer(); var engine = new DefaultEngine(server); var proxy = HttpProxyFactory.Create(engine); var tcsComplete = new TaskCompletionSource <IReadOnlySession>(); proxy.HttpResponseSent += (_, s) => tcsComplete.TrySetResult(s.Session); var clientTcp = new TestTcpClient(); server.AcceptTcp(clientTcp); var connection = engine.connections.Last(); connection.CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port); var request = @"CONNECT www.example.com:443 HTTP/1.1 Host: www.example.com:443 Proxy-Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36 "; clientTcp.WriteToInput(request); var expectedResponse = @"HTTP/1.1 200 Connection Established Date: Mon, 01 Jan 2018 01:00:00 GMT "; var session = tcsComplete.GetResult(); session.Request.ToString().Is(request); session.Response.ToString().Is(expectedResponse); }
public void ChangeUpstreamProxyTest() { var server = new TestTcpServer(); var engine = new DefaultEngine(server); var clientTcp1 = new TestTcpClient(); server.AcceptTcp(clientTcp1); var connection1 = engine.connections.Last(); connection1.CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port); engine.UpstreamProxyConfig = new UpstreamProxyConfig("first.example.com", 1); var tcsRequest1_1 = new TaskCompletionSource <IReadOnlyHttpRequest>(); connection1.HttpRequestSent += r => tcsRequest1_1.TrySetResult(r); var request = @"GET / HTTP/1.1 Host: hoge.example.com "; clientTcp1.WriteToInput(request); var request1_1 = tcsRequest1_1.GetResult(); var serverTcp1 = connection1.serverConnection.client.AsTest(); serverTcp1.Host.Is("first.example.com"); serverTcp1.Port.Is(1); // UpstreamProxyConfig 変更 engine.UpstreamProxyConfig = new UpstreamProxyConfig("second.example.com", 2); var tcsRequest1_2 = new TaskCompletionSource <IReadOnlyHttpRequest>(); connection1.HttpRequestSent += r => tcsRequest1_2.TrySetResult(r); clientTcp1.WriteToInput(request); // 接続中のコネクションには反映されない var request1_2 = tcsRequest1_2.GetResult(); serverTcp1.Host.Is("first.example.com"); serverTcp1.Port.Is(1); // 次の接続から反映される var clientTcp2 = new TestTcpClient(); server.AcceptTcp(clientTcp2); var connection2 = engine.connections.Last(); connection2.CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port); connection2.HttpRequestSent += r => tcsRequest1_1.TrySetResult(r); var tcsRequest2 = new TaskCompletionSource <IReadOnlyHttpRequest>(); connection2.HttpRequestSent += r => tcsRequest2.TrySetResult(r); clientTcp2.WriteToInput(request); var request2 = tcsRequest2.GetResult(); var serverTcp2 = connection2.serverConnection.client.AsTest(); serverTcp2.Host.Is("second.example.com"); serverTcp2.Port.Is(2); (engine as IReadOnlyHttpProxyEngine).Stop(); }