Example #1
0
        public void Pipeline_containing_method_returning_null_throws_InvalidOperationException()
        {
            pipeline.AddItemToEndOfPipeline(ReturnNull);

            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                pipeline.Invoke(CreateContext(), new CancellationToken());
            });

            Assert.Equal("The after-pipeline action ReturnNull returned null; a Task was expected.", exception.Message);
        }
Example #2
0
        public void Should_add_response_cookie_if_it_has_changed()
        {
            var beforePipeline = new BeforePipeline();
            var afterPipeline  = new AfterPipeline();
            var hooks          = A.Fake <IPipelines>();

            A.CallTo(() => hooks.BeforeRequest).Returns(beforePipeline);
            A.CallTo(() => hooks.AfterRequest).Returns(afterPipeline);
            CookieBasedSessions.Enable(hooks, new CryptographyConfiguration(this.fakeEncryptionProvider, this.fakeHmacProvider)).WithSerializer(this.fakeObjectSerializer);
            var request = CreateRequest("encryptedkey1=value1");

            A.CallTo(() => this.fakeEncryptionProvider.Decrypt("encryptedkey1=value1")).Returns("key1=value1;");
            var response     = A.Fake <Response>();
            var nancyContext = new NancyContext()
            {
                Request = request, Response = response
            };

            beforePipeline.Invoke(nancyContext, new CancellationToken());
            request.Session["Testing"] = "Test";

            afterPipeline.Invoke(nancyContext, new CancellationToken());

            response.Cookies.Count.ShouldEqual(1);
        }
Example #3
0
        public void Pipeline_containing_another_pipeline_will_invoke_items_in_both_pipelines()
        {
            var item1Called             = false;
            Action <NancyContext> item1 = (r) => { item1Called = true; };
            var item2Called             = false;
            Action <NancyContext> item2 = (r) => { item2Called = true; };
            var item3Called             = false;
            Action <NancyContext> item3 = (r) => { item3Called = true; };
            var item4Called             = false;
            Action <NancyContext> item4 = (r) => { item4Called = true; };

            pipeline += item1;
            pipeline += item2;
            var subPipeline = new AfterPipeline();

            subPipeline += item3;
            subPipeline += item4;

            pipeline.AddItemToEndOfPipeline(subPipeline);
            pipeline.Invoke(CreateContext(), new CancellationToken());

            Assert.True(item1Called);
            Assert.True(item2Called);
            Assert.True(item3Called);
            Assert.True(item4Called);
        }
Example #4
0
        public void Should_add_response_cookie_if_it_has_changed()
        {
            var beforePipeline = new BeforePipeline();
            var afterPipeline  = new AfterPipeline();
            var hooks          = A.Fake <IApplicationPipelines>();

            A.CallTo(() => hooks.BeforeRequest).Returns(beforePipeline);
            A.CallTo(() => hooks.AfterRequest).Returns(afterPipeline);
            CookieBasedSessions.Enable(hooks, encryptionProvider, hmacProvider, "this passphrase", "this is a salt", "hmac passphrase").WithFormatter(new Fakes.FakeSessionObjectFormatter());
            var request = CreateRequest("encryptedkey1=value1");

            A.CallTo(() => this.encryptionProvider.Decrypt("encryptedkey1=value1", A <string> .Ignored, A <byte[]> .Ignored)).Returns("key1=value1;");
            var response     = A.Fake <Response>();
            var nancyContext = new NancyContext()
            {
                Request = request, Response = response
            };

            beforePipeline.Invoke(nancyContext);
            request.Session["Testing"] = "Test";

            afterPipeline.Invoke(nancyContext);

            response.Cookies.Count.ShouldEqual(1);
        }
        private void ExecutePost(NancyContext context, CancellationToken cancellationToken, AfterPipeline postHook, Func <NancyContext, Exception, dynamic> onError, TaskCompletionSource <Response> tcs)
        {
            if (postHook == null)
            {
                tcs.SetResult(context.Response);
                return;
            }

            postHook.Invoke(context, cancellationToken).WhenCompleted(
                completedTask => tcs.SetResult(context.Response),
                completedTask => this.HandlePostHookFaultedTask(context, onError, completedTask, tcs));
        }
        public void Should_add_response_cookie_if_it_has_changed()
        {
            var beforePipeline = new BeforePipeline();
            var afterPipeline = new AfterPipeline();
            var hooks = A.Fake<IApplicationPipelines>();
            A.CallTo(() => hooks.BeforeRequest).Returns(beforePipeline);
            A.CallTo(() => hooks.AfterRequest).Returns(afterPipeline);
            CookieBasedSessions.Enable(hooks, new CryptographyConfiguration(this.fakeEncryptionProvider, this.fakeHmacProvider)).WithSerializer(new Fakes.FakeObjectSerializer());
            var request = CreateRequest("encryptedkey1=value1");
            A.CallTo(() => this.fakeEncryptionProvider.Decrypt("encryptedkey1=value1")).Returns("key1=value1;");
            var response = A.Fake<Response>();
            var nancyContext = new NancyContext() { Request = request, Response = response };
            beforePipeline.Invoke(nancyContext);
            request.Session["Testing"] = "Test";

            afterPipeline.Invoke(nancyContext);

            response.Cookies.Count.ShouldEqual(1);
        }
        public void Should_add_response_cookie_if_it_has_changed()
        {
            var beforePipeline = new BeforePipeline();
            var afterPipeline = new AfterPipeline();
            var hooks = A.Fake<IApplicationPipelines>();
            A.CallTo(() => hooks.BeforeRequest).Returns(beforePipeline);
            A.CallTo(() => hooks.AfterRequest).Returns(afterPipeline);
            CookieBasedSessions.Enable(hooks, encryptionProvider, "this passphrase", "this is a salt").WithFormatter(new Fakes.FakeSessionObjectFormatter());
            var request = CreateRequest("encryptedkey1=value1");
            A.CallTo(() => this.encryptionProvider.Decrypt("encryptedkey1=value1", A<string>.Ignored, A<byte[]>.Ignored)).Returns("key1=value1;");
            var response = A.Fake<Response>();
            var nancyContext = new NancyContext() { Request = request, Response = response };
            beforePipeline.Invoke(nancyContext);
            request.Session["Testing"] = "Test";

            afterPipeline.Invoke(nancyContext);

            response.Cookies.Count.ShouldEqual(1);
        }
        public void When_cast_to_func_and_invoked_members_are_invoked()
        {
            var item1Called = false;
            Action<NancyContext> item1 = (r) => { item1Called = true; };
            var item2Called = false;
            Action<NancyContext> item2 = (r) => { item2Called = true; };
            var item3Called = false;
            Action<NancyContext> item3 = (r) => { item3Called = true; };
            pipeline.AddItemToEndOfPipeline(item1);
            pipeline.AddItemToEndOfPipeline(item2);
            pipeline.AddItemToEndOfPipeline(item3);

            Action<NancyContext> action = context => { };
            pipeline += action;

            pipeline.Invoke(CreateContext(), CancellationToken.None);

            Assert.True(item1Called);
            Assert.True(item2Called);
            Assert.True(item3Called);
        }
        private async Task ExecutePost(NancyContext context, CancellationToken cancellationToken, AfterPipeline postHook, Func <NancyContext, Exception, dynamic> onError)
        {
            if (postHook == null)
            {
                return;
            }

            try
            {
                await postHook.Invoke(context, cancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                context.Response = this.ResolveErrorResult(context, onError, ex);

                if (context.Response == null)
                {
                    throw;
                }
            }
        }
Example #10
0
        public void When_cast_to_func_and_invoked_members_are_invoked()
        {
            var item1Called             = false;
            Action <NancyContext> item1 = (r) => { item1Called = true; };
            var item2Called             = false;
            Action <NancyContext> item2 = (r) => { item2Called = true; };
            var item3Called             = false;
            Action <NancyContext> item3 = (r) => { item3Called = true; };

            pipeline.AddItemToEndOfPipeline(item1);
            pipeline.AddItemToEndOfPipeline(item2);
            pipeline.AddItemToEndOfPipeline(item3);

            Action <NancyContext> action = context => { };

            pipeline += action;

            pipeline.Invoke(CreateContext(), CancellationToken.None);

            Assert.True(item1Called);
            Assert.True(item2Called);
            Assert.True(item3Called);
        }
        public void Pipeline_containing_another_pipeline_will_invoke_items_in_both_pipelines()
        {
            var item1Called = false;
            Action<NancyContext> item1 = (r) => { item1Called = true; };
            var item2Called = false;
            Action<NancyContext> item2 = (r) => { item2Called = true; };
            var item3Called = false;
            Action<NancyContext> item3 = (r) => { item3Called = true; };
            var item4Called = false;
            Action<NancyContext> item4 = (r) => { item4Called = true; };
            pipeline += item1;
            pipeline += item2;
            var subPipeline = new AfterPipeline();
            subPipeline += item3;
            subPipeline += item4;

            pipeline.AddItemToEndOfPipeline(subPipeline);
            pipeline.Invoke(CreateContext());

            Assert.True(item1Called);
            Assert.True(item2Called);
            Assert.True(item3Called);
            Assert.True(item4Called);
        }
Example #12
0
        private void ExecutePost(NancyContext context, CancellationToken cancellationToken, AfterPipeline postHook, TaskCompletionSource<Response> tcs)
        {
            if (postHook == null)
            {
                tcs.SetResult(context.Response);
                return;
            }

            postHook.Invoke(context, cancellationToken).WhenCompleted(
                                        t => tcs.SetResult(context.Response),
                                        t => tcs.SetException(t.Exception),
                                        false);
        }
        private static void ExecutePost(NancyContext context, CancellationToken cancellationToken, AfterPipeline postHook, Func<NancyContext, Exception, Response> onError, TaskCompletionSource<Response> tcs)
        {
            if (postHook == null)
            {
                tcs.SetResult(context.Response);
                return;
            }

            postHook.Invoke(context, cancellationToken).WhenCompleted(
                completedTask => tcs.SetResult(context.Response),
                completedTask => HandlePostHookFaultedTask(context, onError, completedTask, tcs),
                false);
        }
Example #14
0
 private static Task InvokePostRequestHook(Context context, CancellationToken cancellationToken, AfterPipeline pipeline)
 {
     return(pipeline == null?Task.FromResult <object>(null) : pipeline.Invoke(context, cancellationToken));
 }
        private async Task ExecutePost(NancyContext context, CancellationToken cancellationToken, AfterPipeline postHook, Func<NancyContext, Exception, dynamic> onError)
        {
            if (postHook == null)
            {
                return;
            }

            try
            {
                await postHook.Invoke(context, cancellationToken)
                    .ConfigureAwait(false);
            }
            catch(Exception ex)
            {
                context.Response = this.ResolveErrorResult(context, onError, ex);

                if(context.Response == null)
                {
                    throw;
                }
            }
        }