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"));
        }
        public void Adds_content_length_response_header_if_none()
        {
            var app = new StaticApp(
                    "200 OK",
                    new Dictionary<string, string>(),
                    Delegates.ToDelegate((write, fault, end) =>
                    {
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null);
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null);
                        end();
                        return () => { };
                    }));

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

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

            Assert.That(new Environment(app.Env).Body, Is.Null);
            Assert.That(mockResponseDelegate.Head.Headers.Keys, Contains.Item("Content-Length"));
            Assert.That(mockResponseDelegate.Head.Headers["Content-Length"], Is.EqualTo("10"));

            var body = mockResponseDelegate.Body.Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
        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, (onNext, onError, onComplete) =>
            {
                onNext(bytes("kanye "), null);
                onNext(bytes("west "), null);
                onNext(bytes("is "), null);
                onNext(bytes("a "), null);
                onNext(bytes("pussy."), null);
                return () => { };
            });

            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("kanye west is a pussy."));
        }
        public void Wrapped_apps_onNext_gets_rescheduled_continuation_iff_wrapping_app_provides_continuation()
        {
            var app = new StaticApp(null, null, null);

            bool gotComplete = false;
            int gotOnNext = 0;
            Action ack0 = null;
            Action ack1 = null;

            var scheduler = new MockScheduler();

            app.OnRequest = () => {
                var env = new Environment(app.Env);

                env.Body((data, ack) => {
                    gotOnNext++;

                    if (gotOnNext == 1)
                        ack0 = ack;
                    if (gotOnNext == 2)
                        ack1 = ack;

                    if (ack == null)
                        return false;
                    else
                    {
                        return true;
                    }
                },
                e => {
                },
                () => { gotComplete = true; });
            };

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

            scheduler.Post(() =>
            {
                middleware.Invoke(new Environment() {
                    Body = (onNext, onError, onComplete) => {
                        onNext(default(ArraySegment<byte>), null);
                        onNext(default(ArraySegment<byte>), () => onComplete());
                        return () => { };
                    }
                }, (status, headers, body) =>
                {

                },
                e => { });
            });

            scheduler.Start();

            Assert.That(gotOnNext, Is.EqualTo(2));
            Assert.That(ack0, Is.Null);
            Assert.That(ack1, Is.Not.Null);

            // queues the continuation (which invokes onComplete) on the scheduler. onComplete should not be called yet.
            ack1();
            Assert.That(gotComplete, Is.False);

            // now run the scheduler more. after its done onComplete should have been called.
            scheduler.Start();
            Assert.That(gotComplete, Is.True);
        }
        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", "chunked" }
                    },
                    Delegates.ToDelegate((write, fault, end) =>
                    {
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("12345")), null);
                        write(new ArraySegment<byte>(Encoding.ASCII.GetBytes("67890")), null);
                        end();
                        return () => { };
                    }));

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

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

            Assert.That(new Environment(app.Env).Body, 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);
        }
        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 bodyAction = new Environment(app.Env).Body;
            Assert.That(bodyAction, Is.Not.Null);
            var body = Delegates.ToDelegate(bodyAction).Consume();
            Assert.That(body.Buffer.GetString(), Is.EqualTo("1234567890"));
            Assert.That(body.GotEnd, Is.True);
        }
        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.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"));
        }