Ejemplo n.º 1
0
        public void Environment_items_conform_to_spec_by_default()
        {
            Request  request          = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var      app = new StaticApp(expectedResponse);

            IDictionary <string, object> env = null;

            app.OnRequest = () =>
            {
                env = app.Env;
            };

            var requestDelegate = new GateRequestDelegate(app.Invoke, null);

            requestDelegate.OnRequest(new HttpRequestHead()
            {
            }, null, mockResponseDelegate);

            var req = new Request(env);

            Assert.That(req.Body, Is.EqualTo(Stream.Null));
            Assert.That(req.Headers, Is.Not.Null);
            Assert.That(req.Method, Is.Not.Null);
            Assert.That(req.Path, Is.Not.Null);
            Assert.That(req.PathBase, Is.Not.Null);
            Assert.That(req.QueryString, Is.Not.Null);
            Assert.That(req.Scheme, Is.EqualTo("http"));
            Assert.That(req.Version, Is.EqualTo("1.0"));
        }
Ejemplo n.º 2
0
        public void Context_passed_to_constructor_is_passed_through_and_not_modified_by_requests()
        {
            var context = new Dictionary<string, object>()
            {
                { "Key", "Value" }
            };

            Request request = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var app = new StaticApp(expectedResponse);

            IDictionary<string, object> appContext = null;

            app.OnRequest = () =>
            {
                app.Env["OtherKey"] = "OtherValue";
                appContext = app.Env;
            };

            var requestDelegate = new GateRequestDelegate(app.Invoke, context);
            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            Assert.That(context.ContainsKey("Key"), Is.True);
            Assert.That(context["Key"], Is.EqualTo("Value"));
            Assert.That(context.ContainsKey("OtherKey"), Is.False);

            Assert.That(appContext.ContainsKey("Key"), Is.True);
            Assert.That(appContext["Key"], Is.EqualTo("Value"));
            Assert.That(appContext.ContainsKey("OtherKey"), Is.True);
            Assert.That(appContext["OtherKey"], Is.EqualTo("OtherValue"));
        }
Ejemplo n.º 3
0
        public void Context_passed_to_constructor_is_passed_through_and_not_modified_by_requests()
        {
            var context = new Dictionary <string, object>()
            {
                { "Key", "Value" }
            };

            Request  request          = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var      app = new StaticApp(expectedResponse);

            IDictionary <string, object> appContext = null;

            app.OnRequest = () =>
            {
                app.Env["OtherKey"] = "OtherValue";
                appContext          = app.Env;
            };

            var requestDelegate = new GateRequestDelegate(app.Invoke, context);

            requestDelegate.OnRequest(new HttpRequestHead()
            {
            }, null, mockResponseDelegate);

            Assert.That(context.ContainsKey("Key"), Is.True);
            Assert.That(context["Key"], Is.EqualTo("Value"));
            Assert.That(context.ContainsKey("OtherKey"), Is.False);

            Assert.That(appContext.ContainsKey("Key"), Is.True);
            Assert.That(appContext["Key"], Is.EqualTo("Value"));
            Assert.That(appContext.ContainsKey("OtherKey"), Is.True);
            Assert.That(appContext["OtherKey"], Is.EqualTo("OtherValue"));
        }
Ejemplo n.º 4
0
        [Ignore] // TODO: Implement the request body stream
        public void Request_body_is_passed_through()
        {
            Request  request          = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var      app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary <string, object>());

            requestDelegate.OnRequest(new HttpRequestHead()
            {
            }, new MockDataProducer(c =>
            {
                c.OnData(new ArraySegment <byte>(Encoding.ASCII.GetBytes("12345")), null);
                c.OnData(new ArraySegment <byte>(Encoding.ASCII.GetBytes("67890")), null);
                c.OnEnd();
                return(() => { });
            }), mockResponseDelegate);

            var bodyStream = app.Env.Get <Stream>(OwinConstants.RequestBody);

            Assert.That(bodyStream, Is.Not.Null);
            var body = bodyStream.Consume();

            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 5
0
        [Ignore] // TODO: Implement the response body stream
        public void Does_not_add_content_length_response_header_if_transfer_encoding_chunked()
        {
            Request  request          = Request.Create();
            Response expectedResponse = new Response(request.Environment);

            expectedResponse.OutputStream = new MemoryStream();
            expectedResponse.Headers.SetHeader("Transfer-Encoding", "chunked");
            expectedResponse.Write("12345");
            expectedResponse.Write("67890");
            expectedResponse.OutputStream.Seek(0, SeekOrigin.Begin);
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary <string, object>());

            requestDelegate.OnRequest(new HttpRequestHead()
            {
            }, null, mockResponseDelegate);

            Assert.That(app.Env.Get <Stream>(OwinConstants.RequestBody), Is.EqualTo(Stream.Null));
            Assert.IsFalse(mockResponseDelegate.Head.Headers.Keys.Contains("Content-Length"), "should not contain Content-Length");
            // chunks are not chunked-encoded at this level. eventually kayak will do this automatically.
            Assert.That(mockResponseDelegate.Body, Is.Not.Null);
            var body = mockResponseDelegate.Body.Consume();

            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 6
0
        [Ignore] // TODO: Implement the response body stream
        public void Adds_connection_close_response_header_if_no_length_or_encoding()
        {
            Request  request          = Request.Create();
            Response expectedResponse = new Response(request.Environment);

            expectedResponse.OutputStream = new MemoryStream();
            expectedResponse.Write("12345");
            expectedResponse.Write("67890");
            expectedResponse.OutputStream.Seek(0, SeekOrigin.Begin);
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary <string, object>());

            requestDelegate.OnRequest(new HttpRequestHead()
            {
            }, null, mockResponseDelegate);

            Assert.That(app.Env.Get <Stream>(OwinConstants.RequestBody), Is.EqualTo(Stream.Null));
            Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Connection"));
            Assert.That(mockResponseDelegate.Head.Headers["Connection"], Is.EqualTo("close"));

            Assert.That(mockResponseDelegate.Body, Is.Not.Null);
            var body = mockResponseDelegate.Body.Consume();

            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 7
0
        public void Adds_connection_close_response_header_if_no_length_or_encoding()
        {
            Response expectedResponse = new Response(200);
            expectedResponse.Write("12345");
            expectedResponse.Write("67890");
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>());

            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            Assert.That(app.Call.Body, Is.Null);
            Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Connection"));
            Assert.That(mockResponseDelegate.Head.Headers["Connection"], Is.EqualTo("close"));

            Assert.That(mockResponseDelegate.Body, Is.Not.Null);
            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 8
0
        public void Adds_connection_close_response_header_if_no_length_or_encoding()
        {
            Request request = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            expectedResponse.OutputStream = new MemoryStream();
            expectedResponse.Write("12345");
            expectedResponse.Write("67890");
            expectedResponse.OutputStream.Seek(0, SeekOrigin.Begin);
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>());

            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            Assert.That(app.Env.Get<Stream>(OwinConstants.RequestBody), Is.EqualTo(Stream.Null));
            Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Connection"));
            Assert.That(mockResponseDelegate.Head.Headers["Connection"], Is.EqualTo("close"));

            Assert.That(mockResponseDelegate.Body, Is.Not.Null);
            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 9
0
        public void Does_not_add_content_length_response_header_if_transfer_encoding_chunked()
        {
            Request request = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            expectedResponse.OutputStream = new MemoryStream();
            expectedResponse.Headers.SetHeader("Transfer-Encoding", "chunked");
            expectedResponse.Write("12345");
            expectedResponse.Write("67890");
            expectedResponse.OutputStream.Seek(0, SeekOrigin.Begin);
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>());

            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            Assert.That(app.Env.Get<Stream>(OwinConstants.RequestBody), Is.EqualTo(Stream.Null));
            Assert.IsFalse(mockResponseDelegate.Head.Headers.Keys.Contains("Content-Length"), "should not contain Content-Length");
            // chunks are not chunked-encoded at this level. eventually kayak will do this automatically.
            Assert.That(mockResponseDelegate.Body, Is.Not.Null);
            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 10
0
        public void Request_body_is_passed_through()
        {
            Request request = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var app = new StaticApp(expectedResponse);

            var requestDelegate = new GateRequestDelegate(app.Invoke, new Dictionary<string, object>());

            requestDelegate.OnRequest(new HttpRequestHead() { }, new MockDataProducer(c =>
            {
                c.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null);
                c.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null);
                c.OnEnd();
                return () => { };
            }), mockResponseDelegate);

            var bodyStream = app.Env.Get<Stream>(OwinConstants.RequestBody);
            Assert.That(bodyStream, Is.Not.Null);
            var body = bodyStream.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Ejemplo n.º 11
0
        public void Environment_items_conform_to_spec_by_default()
        {
            Request request = Request.Create();
            Response expectedResponse = new Response(request.Environment);
            var app = new StaticApp(expectedResponse);

            IDictionary<string, object> env = null;

            app.OnRequest = () =>
            {
                env = app.Env;
            };

            var requestDelegate = new GateRequestDelegate(app.Invoke, null);
            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            var req = new Request(env);

            Assert.That(req.Body, Is.EqualTo(Stream.Null));
            Assert.That(req.Headers, Is.Not.Null);
            Assert.That(req.Method, Is.Not.Null);
            Assert.That(req.Path, Is.Not.Null);
            Assert.That(req.PathBase, Is.Not.Null);
            Assert.That(req.QueryString, Is.Not.Null);
            Assert.That(req.Scheme, Is.EqualTo("http"));
            Assert.That(req.Version, Is.EqualTo("1.0"));
        }
Ejemplo n.º 12
0
        public void Environment_items_conform_to_spec_by_default()
        {
            var app = new StaticApp(new Response(200));

            CallParameters call = default(CallParameters);

            app.OnRequest = () =>
            {
                call = app.Call;
            };

            var requestDelegate = new GateRequestDelegate(app.Invoke, null);
            requestDelegate.OnRequest(new HttpRequestHead() { }, null, mockResponseDelegate);

            var env = new Request(call);

            Assert.That(call.Body, Is.Null);
            Assert.That(env.Headers, Is.Not.Null);
            Assert.That(env.Method, Is.Not.Null);
            Assert.That(env.Path, Is.Not.Null);
            Assert.That(env.PathBase, Is.Not.Null);
            Assert.That(env.QueryString, Is.Not.Null);
            Assert.That(env.Scheme, Is.EqualTo("http"));
            Assert.That(env.Version, Is.EqualTo("1.0"));
        }