示例#1
0
        public void TestHttpMessageParser()
        {
            var httpResponse =
                @"HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Date: Fri, 13 Oct 2017 05:54:52 GMT
Expires: -1
P3P: CP=""This is not a P3P policy! See g.co/p3phelp for more info.""
Set-Cookie: 1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly
Server: gws
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked";

            var parser = new HttpMessageParser(Encoding.ASCII.GetBytes(httpResponse));

            parser.Append("foo: bar");
            parser.StatusCode.Should().Be(HttpStatusCode.OK);
            parser.Reason.Should().Be("OK");
            parser.Headers["X-XSS-Protection"].Should().Be("1; mode=block");
            parser.Headers["X-Frame-Options"].Should().Be("SAMEORIGIN");
            parser.Headers["Cache-Control"].Should().Be("private, max-age=0");
            parser.Headers["P3P"].Should().Be("CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\"");
            parser.Headers["Set-Cookie"].Should().Be(
                "1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly");
            parser.Headers["foo"].Should().Be("bar");

            parser = new HttpMessageParser("HTTP/1.1 200 OK");
            parser.StatusCode.Should().Be(HttpStatusCode.OK);
            parser.Reason.Should().Be("OK");
        }
示例#2
0
        public void TestAppenNullToHttpMessageParser()
        {
            var httpResponse =
                @"HTTP/1.1 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Date: Fri, 13 Oct 2017 05:54:52 GMT
Expires: -1
P3P: CP=""This is not a P3P policy! See g.co/p3phelp for more info.""
Set-Cookie: 1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly
Server: gws
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked";
            Exception e      = null;
            var       parser = new HttpMessageParser(Encoding.ASCII.GetBytes(httpResponse));

            try {
                parser.Append(null);
            } catch (Exception ex) {
                e = ex;
            }
            e.Should().NotBeNull("because an exception is expected");
        }
示例#3
0
        public void TestCheckHeader()
        {
            HttpMessageParser parser = new HttpMessageParser("HTTP/1.1 401 Unauthorized");

            parser.Append("Content - Type: application / json");
            parser.Append("Server: Couchbase Sync Gateway/2.0.0");
            parser.Append("Www-Authenticate: Basic realm=\"Couchbase Sync Gateway\"");
            parser.Append("Date: Mon, 06 Aug 2018 17:44:51 GMT");
            parser.Append("Content-Length: 50");
            string key           = "Connection";
            string expectedValue = "Upgrade";
            bool   caseSens      = false; // true

            var method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static);
            var res    = method.Invoke(null, new object[4] {
                parser, key, expectedValue, caseSens
            });

            res.Should().Be(false);

            parser = new HttpMessageParser("HTTP/1.1 101 Switching Protocols");
            parser.Append("Upgrade: websocket");
            parser.Append("Connection: Upgrade");
            parser.Append("Sec-WebSocket-Accept: R3ztu/aZLI+izEEtS3Ao1kzub4s=");
            parser.Append("Sec-WebSocket-Protocol: BLIP_3+CBMobile_2");

            key           = "Connection";
            expectedValue = "Upgrade";
            caseSens      = false;

            method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static);
            res    = method.Invoke(null, new object[4] {
                parser, key, expectedValue, caseSens
            });

            res.Should().Be(true);

            key           = "Upgrade";
            expectedValue = "websocket";
            caseSens      = false;

            method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static);
            res    = method.Invoke(null, new object[4] {
                parser, key, expectedValue, caseSens
            });

            res.Should().Be(true);

            key           = "Sec-WebSocket-Accept";
            expectedValue = "R3ztu/aZLI+izEEtS3Ao1kzub4s=";
            caseSens      = true;

            method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static);
            res    = method.Invoke(null, new object[4] {
                parser, key, expectedValue, caseSens
            });

            res.Should().Be(true);

            method = WebSocketWrapperType.GetMethod("Connected", BindingFlags.NonPublic | BindingFlags.Instance);
            //res = method.Invoke(webSocketWrapper, null);

            method = WebSocketWrapperType.GetMethod("ReceivedHttpResponse", BindingFlags.NonPublic | BindingFlags.Instance);
            //res = method.Invoke(webSocketWrapper, new object[1] { parser });

            method = HttpLogicType.GetMethod("ReceivedResponse", BindingFlags.Public | BindingFlags.Instance);
            res    = method.Invoke(hTTPLogic, new object[1] {
                parser
            });

            method = HttpLogicType.GetMethod("Redirect", BindingFlags.NonPublic | BindingFlags.Instance);
            res    = method.Invoke(hTTPLogic, new object[1] {
                parser
            });
        }