Пример #1
0
        public void TunnelTest()
        {
            var clientTcp  = new TestTcpClient();
            var connection = new ProxyConnection(clientTcp)
            {
                CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port)
            };
            var exceptions = new List <Exception>();

            connection.FatalException += (_, e) => exceptions.Add(e);

            connection.StartReceiving();

            var tcsSession = new TaskCompletionSource <IReadOnlySession>();

            connection.HttpResponseSent += result => tcsSession.TrySetResult(result);

            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 session = tcsSession.GetResult();

            session.Request.ToString().Is(request);

            var expectedResponse =
                @"HTTP/1.1 200 Connection Established
Date: Mon, 01 Jan 2018 01:00:00 GMT

";

            session.Response.ToString().Is(expectedResponse);

            connection.serverConnection.client.AsTest().Host.Is("www.example.com");
            connection.serverConnection.client.AsTest().Port.Is(443);
            connection.serverConnection.IsTunnelMode.IsTrue();
            connection.clientConnection.IsTunnelMode.IsTrue();

            var expectedString = "hogehoge\r\n";

            clientTcp.WriteToInput(expectedString);

            // HTTP と解釈できないので例外が発生するが、データは送信されている。
            var serverTcp = connection.serverConnection.client.AsTest();

            serverTcp.GetStream().AsTest().OutputStream.Is(expectedString);

            exceptions.Count.Is(1);

            connection.Dispose();
        }
Пример #2
0
        public void Session302Test()
        {
            var clientTcp  = new TestTcpClient();
            var connection = new ProxyConnection(clientTcp)
            {
                CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port)
            };

            connection.StartReceiving();

            var tcsRequest = new TaskCompletionSource <IReadOnlyHttpRequest>();

            connection.HttpRequestSent += result => tcsRequest.TrySetResult(result);

            var tcsSession = new TaskCompletionSource <IReadOnlySession>();

            connection.HttpResponseSent += result => tcsSession.TrySetResult(result);

            var tcsClose = new TaskCompletionSource <ProxyConnection>();

            connection.Disposing += result => tcsClose.TrySetResult(result);

            var request =
                @"GET http://www.example.com/netgame/social/-/gadgets/=/app_id=854854/ HTTP/1.1
Host: www.example.com
Proxy-Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

";

            clientTcp.WriteToInput(request);

            var requestResult = tcsRequest.GetResult();

            connection.serverConnection.client.AsTest().WriteFileToInput("TestData/Response302");

            var sessionResult = tcsSession.GetResult();

            sessionResult.Request.ToString().Is(request);

            var response = sessionResult.Response as HttpResponse;

            response.GetBodyAsString().Is("");

            connection.serverConnection.client.AsTest().Close();    // サーバーから閉じる
            //clientTcp.Close();  // クライアントから閉じる

            tcsClose.GetResult().Is(connection);

            connection.Dispose();
        }
Пример #3
0
        public void SessionNoBodyTest()
        {
            var clientTcp  = new TestTcpClient();
            var connection = new ProxyConnection(clientTcp)
            {
                CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port)
            };

            connection.StartReceiving();

            var tcsRequest = new TaskCompletionSource <IReadOnlyHttpRequest>();

            connection.HttpRequestSent += result => tcsRequest.TrySetResult(result);

            var tcsSession = new TaskCompletionSource <IReadOnlySession>();

            connection.HttpResponseSent += result => tcsSession.TrySetResult(result);

            var tcsClose = new TaskCompletionSource <ProxyConnection>();

            connection.Disposing += result => tcsClose.TrySetResult(result);

            var request =
                @"GET http://203.104.209.71/kcs2/resources/voice/titlecall_1/005.mp3 HTTP/1.1
Host: 203.104.209.71

";

            clientTcp.WriteToInput(request);

            var requestResult = tcsRequest.GetResult();

            connection.serverConnection.client.AsTest().WriteFileToInput("TestData/ResponseNoBody");

            var sessionResult = tcsSession.GetResult();

            sessionResult.Request.ToString().Is(request);

            var response = sessionResult.Response as HttpResponse;

            response.GetBodyAsString().Is("");

            connection.serverConnection.client.AsTest().Close();    // サーバーから閉じる
            //clientTcp.Close();  // クライアントから閉じる

            tcsClose.GetResult().Is(connection);

            connection.Dispose();
        }
Пример #4
0
        public void SessionContentLengthTest()
        {
            var clientTcp  = new TestTcpClient();
            var connection = new ProxyConnection(clientTcp)
            {
                CreateTcpClientForServer = (host, port) => new TestTcpClient(host, port)
            };

            connection.StartReceiving();

            var tcsRequest = new TaskCompletionSource <IReadOnlyHttpRequest>();

            connection.HttpRequestSent += result => tcsRequest.TrySetResult(result);

            var tcsSession = new TaskCompletionSource <IReadOnlySession>();

            connection.HttpResponseSent += result => tcsSession.TrySetResult(result);

            var tcsClose = new TaskCompletionSource <ProxyConnection>();

            connection.Disposing += result => tcsClose.TrySetResult(result);

            var request =
                @"GET http://203.104.209.71/kcs2/resources/voice/titlecall_1/005.mp3 HTTP/1.1
Host: 203.104.209.71
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
Accept: */*
Referer: http://203.104.209.71/kcs2/index.php?api_root=/kcsapi&voice_root=/kcs/sound&osapi_root=osapi.example.com&version=4.1.1.4
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

";

            clientTcp.WriteToInput(request);

            //var tcsRequestSuccess = tcsRequest.Task.Wait(1000);
            //if (!tcsRequestSuccess)
            //    Assert.False(true, "request timeout");
            var requestResult = tcsRequest.GetResult();

            connection.serverConnection.client.AsTest().WriteFileToInput("TestData/ResponseContentLength");

            //var tcsSessionSuccess = tcsSession.Task.Wait(1000);
            //if (!tcsSessionSuccess)
            //    Assert.False(true, "session timeout");
            var sessionResult = tcsSession.GetResult();

            var requestHeaders = sessionResult.Request.Headers as HttpHeaders;

            sessionResult.Request.RequestLine.HttpVersion.Is(new Version(1, 1));
            sessionResult.Request.RequestLine.Method.Is(HttpMethod.Get);
            sessionResult.Request.RequestLine.RequestTarget.Is("http://203.104.209.71/kcs2/resources/voice/titlecall_1/005.mp3");
            requestHeaders.Host.Is("203.104.209.71");
            requestHeaders.GetFirstValue("Connection").Is("keep-alive");
            requestHeaders.GetFirstValue("User-Agent").Is("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36");
            requestHeaders.GetFirstValue("Accept").Is("*/*");
            requestHeaders.GetFirstValue("Referer").Is("http://203.104.209.71/kcs2/index.php?api_root=/kcsapi&voice_root=/kcs/sound&osapi_root=osapi.example.com&version=4.1.1.4");
            requestHeaders.GetFirstValue("Accept-Encoding").Is("gzip, deflate");
            requestHeaders.GetFirstValue("Accept-Language").Is("en-US,en;q=0.9");
            requestHeaders.HasHeader("Via").IsFalse();
            sessionResult.Request.Body.IsNull();


            var response = sessionResult.Response as HttpResponse;

            response.StatusLine.HttpVersion.Is(new Version(1, 1));
            response.StatusLine.StatusCode.Is(HttpStatusCode.OK);
            response.StatusLine.ReasonPhrase.Is("OK");
            response.Headers.ContentLength.Exists.Is(true);
            response.Headers.ContentLength.Is(47);
            response.Headers.TransferEncoding.Exists.Is(false);
            response.Headers.IsChunked.Is(false);
            response.Headers.GetFirstValue("Access-Control-Allow-Credentials").Is("true");
            response.Headers.GetFirstValue("Access-Control-Allow-Methods").Is("POST,GET,HEAD,OPTIONS");
            response.Headers.GetFirstValue("Access-Control-Allow-Origin").Is("http://d.hatena.ne.jp");
            response.Headers.GetFirstValue("Cache-Control").Is("no-store, no-cache");
            response.Headers.ContentType.Is("application/json");
            response.Headers.GetFirstValue("Date").Is("Thu, 20 Sep 2018 01:59:09 GMT");
            response.Headers.GetFirstValue("Expires").Is("Mon, 15 Jun 1998 00:00:00 GMT");
            response.Headers.GetFirstValue("Pragma").Is("no-cache");
            response.Headers.GetFirstValue("Server").Is("Adtech Adserver");
            response.GetBodyAsString().Is(@"{""id"":""31259257328373081"",""seatbid"":[],""nbr"":1}");



            //connection.serverConnection.client.AsTest().Close();    // サーバーから閉じる
            clientTcp.Close();  // クライアントから閉じる

            tcsClose.GetResult().Is(connection);

            connection.Dispose();
        }