Exemplo 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"));
        }
Exemplo n.º 2
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);
        }
Exemplo 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"));
        }
Exemplo 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);
        }
Exemplo n.º 5
0
        public void Context_passed_to_constructor_is_passed_through_and_not_modified_by_requests()
        {
            var context = new Dictionary<string, object>()
            {
                { "Key", "Value" }
            };

            var app = new StaticApp(null, null, null);

            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"));
        }
Exemplo 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);
        }
Exemplo n.º 7
0
        public void Response_stream_is_rebuffered()
        {
            // strategy:
            // reuse a single buffer for all writes.
            // don't spool through the scheduler until after app is done writing.
            //
            // if implementation simply keeps a reference to the supplied buffer, 
            // its contents will be the contents of the final write and the
            // received body will be garbage. consumer must copy out of
            // producer's buffer.

            byte[] b = new byte[6];

            Func<string, ArraySegment<byte>> bytes = s =>
            {
                var sb = Encoding.ASCII.GetBytes(s);
                System.Buffer.BlockCopy(sb, 0, b, 0, sb.Length);
                return new ArraySegment<byte>(b, 0, sb.Length);
            };

            var app = new StaticApp(null, null, (write, end, cancel) =>
            {
                write(bytes("alpha "), null);
                write(bytes("beta "), null);
                write(bytes("gamma "), null);
                write(bytes("delta "), null);
                write(bytes("omega."), null);
                end(null);
            });

            var scheduler = new MockScheduler();
            var middleware = new RescheduleCallbacksMiddleware(app.Invoke, scheduler);
            var responseDelegate = new MockResponseDelegate();

            BufferingConsumer bodyConsumer = null;

            scheduler.Post(() =>
                {
                    middleware.Invoke(new Dictionary<string, object>(), (status, headers, body) =>
                        {
                            bodyConsumer = body.Consume();
                        }, e => { });
                });

            scheduler.Start();

            var bodyString = bodyConsumer.Buffer.GetString();

            Assert.That(bodyString, Is.EqualTo("alpha beta gamma delta omega."));
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public void Adds_connection_close_response_header_if_no_length_or_encoding()
        {
            var app = new StaticApp(
                    "200 OK",
                    new Dictionary<string, string[]>(),
                    (write, end, cancel) =>
                    {
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null);
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null);
                        end(null);
                    });

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

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

            Assert.That(new Environment(app.Env).BodyAction, Is.Null);
            Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Connection"));
            Assert.That(mockResponseDelegate.Head.Headers["Connection"], Is.EqualTo("close"));

            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Exemplo n.º 11
0
        public void Request_body_is_passed_through()
        {
            var app = new StaticApp(null, null, null);

            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 bodyDelegate = new Environment(app.Env).BodyDelegate;
            Assert.That(bodyDelegate, Is.Not.Null);
            var body = bodyDelegate.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Exemplo n.º 12
0
        public void Environment_items_conform_to_spec_by_default()
        {
            var app = new StaticApp(null, null, null);

            IDictionary<string, object> appContext = null;

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

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

            var env = new Environment(appContext);

            Assert.That(env.BodyAction, 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"));
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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"));
        }
Exemplo n.º 15
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"));
        }
Exemplo n.º 16
0
        public void Does_not_add_content_length_response_header_if_transfer_encoding_chunked()
        {
            var app = new StaticApp(
                    "200 OK",
                    new Dictionary<string, string[]>()
                    {
                        { "Transfer-Encoding", new[]{"chunked"} }
                    },
                    (write, end, cancel) =>
                    {
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null);
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null);
                        end(null);

                    });

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

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

            Assert.That(new Environment(app.Env).BodyAction, Is.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.
            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
Exemplo n.º 17
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);
        }