Example #1
0
        public void Should_set_reason_phrase_on_response()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/customPhrase", (x, m) =>
                {
                    var context =
                        new NancyContext();

                    var negotiator =
                        new Negotiator(context);
                    negotiator.WithReasonPhrase("The test is passing!");

                    return(negotiator);
                });
            });

            var brower = new Browser(with =>
            {
                with.ResponseProcessor <TestProcessor>();

                with.Module(module);
            });

            // When
            var response = brower.Get("/customPhrase");

            // Then
            Assert.Equal("The test is passing!", response.ReasonPhrase);
        }
        public async Task Should_not_set_DefaultModel_when_null_was_returned_from_action()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/null", (x, m) => (object)null);
            });

            module.After += context =>
            {
                if (context.NegotiationContext.DefaultModel == null)
                {
                    context.Response = new NotFoundResponse();
                }
            };

            var browser = new Browser(with =>
            {
                with.Module(module);
            });

            // When
            var response = await browser.Get("/null");

            // Then
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
Example #3
0
        public void Should_return_action_value_as_response_with_content_set_as_value()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/action", (x, m) =>
                {
                    Action <Stream> result = stream =>
                    {
                        var wrapper = new UnclosableStreamWrapper(stream);
                        using (var writer = new StreamWriter(wrapper))
                        {
                            writer.Write("Hiya Nancy!");
                        }
                    };

                    return(result);
                });
            });

            var browser = new Browser(with =>
            {
                with.Module(module);
            });

            // When
            var response = browser.Get("/action");

            // Then
            Assert.Equal("Hiya Nancy!", response.Body.AsString());
        }
Example #4
0
        public void Should_add_negotiated_content_headers_to_response()
        {
            // Given

            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/headers", (x, m) =>
                {
                    var context =
                        new NancyContext();

                    var negotiator =
                        new Negotiator(context);
                    negotiator.WithContentType("text/xml");

                    return(negotiator);
                });
            });

            var brower = new Browser(with =>
            {
                with.ResponseProcessor <TestProcessor>();

                with.Module(module);
            });

            // When
            var response = brower.Get("/headers");

            // Then
            Assert.Equal("text/xml", response.Context.Response.ContentType);
        }
Example #5
0
        public void Should_add_negotiated_headers_to_response()
        {
            // Given

            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/headers", (x, m) =>
                {
                    var context =
                        new NancyContext();

                    var negotiator =
                        new Negotiator(context);
                    negotiator.WithHeader("foo", "bar");

                    return(negotiator);
                });
            });

            var brower = new Browser(with =>
            {
                with.ResponseProcessor <TestProcessor>();

                with.Module(module);
            });

            // When
            var response = brower.Get("/headers");

            // Then
            Assert.True(response.Headers.ContainsKey("foo"));
            Assert.Equal("bar", response.Headers["foo"]);
        }
        public void when_binding_to_a_collection_with_blacklisted_property()
        {
            // Given
            var    guid   = Guid.NewGuid();
            string source = string.Format("{{\"SomeString\":\"some string value\",\"SomeGuid\":\"{0}\"}}", guid);

            var context = new BindingContext
            {
                DestinationType          = typeof(Stuff),
                ValidModelBindingMembers = typeof(Stuff).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(propertyInfo => propertyInfo.Name != "SomeString").Select(p => new BindingMemberInfo(p)),
            };

            // Given
            var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
            {
                var stuff = m.Bind <List <Stuff> >("SomeString");
                return(stuff.ToJSON());
            }));
            var bootstrapper = new TestBootstrapper(config => config.Module(module));

            // When
            var browser = new Browser(bootstrapper);
            var result  = browser.Post("/stuff", with =>
            {
                with.HttpRequest();
                with.JsonBody(new List <Stuff> {
                    new Stuff(1, "one"), new Stuff(2, "two")
                }, new JilSerializer());
            });

            // Then
            Assert.AreEqual("[{\"Id\":1,\"SomeString\":null},{\"Id\":2,\"SomeString\":null}]", result.Body.AsString());
        }
        public async void Should_Bind_To_A_Class()
        {
            var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
            {
                var stuff = m.Bind <TestUser>();
                return(stuff.Id.ToString());
            }));

            var bootstrapper = new TestBootstrapper(config => config.Module(module));

            var user = new TestUser
            {
                Age  = 31,
                Id   = Guid.NewGuid(),
                Name = "Deniz"
            };

#if NETCORE
            var browser         = new Browser(bootstrapper);
            var browserResponse = await browser.Post("/stuff", context =>
            {
                context.HttpRequest();
                context.HyperionBody(user);
            });
#else
            var browser         = new Browser(bootstrapper);
            var browserResponse = browser.Post("/stuff", context =>
            {
                context.HttpRequest();
                context.HyperionBody(user);
            });
#endif
            Assert.Equal(user.Id.ToString(), browserResponse.Body.AsString());
        }
Example #8
0
        public async void Can_deserialize_csv()
        {
            var testModule = new ConfigurableNancyModule(config =>
            {
                config.Post("/", (o, module) =>
                {
                    var u = module.Bind <List <User> >();
                    u.ShouldBeEquivalentTo(_users);
                    return(u);
                });
            });

            var browser = new Browser(with =>
            {
                with.Module(testModule);
            });

            var response = await browser.Post("/", context =>
            {
                context.Body(_usersString, "text/csv");
                context.Accept("text/csv");
            });

            response.Body.ContentType.ShouldBeEquivalentTo("text/csv");
            response.Body.AsString().ShouldBeEquivalentTo(_usersString);
        }
Example #9
0
        public void TestWithoutDots()
        {
            var module  = new ConfigurableNancyModule(with => with.Get("/echo/{slug*}", (o, nancyModule) => o.slug));
            var browser = new Browser(with => with.Module(module));

            Assert.Equal(browser.Get("/echo/foo").Body.AsString(), "foo");
            Assert.Equal(browser.Get("/echo/foo/bar").Body.AsString(), "foo/bar");
            Assert.Equal(browser.Get("/echo/foo.bar/gaz").Body.AsString(), "foo.bar/gaz");
            Assert.Equal(browser.Get("/echo/foo.bar").Body.AsString(), "foo.bar");
        }
Example #10
0
        public void Should_return_httpstatuscode_value_from_get_route_as_response_with_content_set_as_value()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/httpstatuscode", (x, m) => HttpStatusCode.Accepted);
            });

            var browser = new Browser(with =>
            {
                with.Module(module);
            });

            // When
            var response = browser.Get("/httpstatuscode");

            // Then
            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
        }
Example #11
0
        public void Should_return_string_value_from_get_route_as_response_with_content_set_as_value()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/string", (x, m) => "hello");
            });

            var browser = new Browser(with =>
            {
                with.Module(module);
            });

            // When
            var response = browser.Get("/string");

            // Then
            Assert.Equal("hello", response.Body.AsString());
        }
Example #12
0
        public void Should_return_int_value_from_get_route_as_response_with_status_code_set_to_value()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/int", (x, m) => 200);
            });

            var browser = new Browser(with =>
            {
                with.Module(module);
            });

            // When
            var response = browser.Get("/int");

            // Then
            Assert.Equal((HttpStatusCode)200, response.StatusCode);
        }
        public void when_binding_to_a_class()
        {
            // Given
            var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
            {
                var stuff = m.Bind <Stuff>();
                return(stuff.Id.ToString());
            }));
            var bootstrapper = new TestBootstrapper(config => config.Module(module));

            // When
            var browser = new Browser(bootstrapper);
            var result  = browser.Post("/stuff", with =>
            {
                with.HttpRequest();
                with.JsonBody(new Stuff(1), new JsonNetSerializer());
            });

            // Then
            Assert.Equal(1, int.Parse(result.Body.AsString()));
        }
Example #14
0
        public async void Can_serialize_csv()
        {
            var testModule = new ConfigurableNancyModule(config =>
            {
                config.Get("/", (o, module) => _users);
            });

            var browser = new Browser(with =>
            {
                with.Module(testModule);
            });

            var response = await browser.Get("/", context =>
            {
                context.Accept(new MediaRange("text/csv"));
            });

            response.StatusCode.Should().Be(HttpStatusCode.OK);

            response.ContentType.Should().Be("text/csv");

            response.Body.AsString().ShouldBeEquivalentTo(_usersString);
        }
        public async Task When_binding_to_a_collection()
        {
            // Given
            var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
            {
                var stuff = m.Bind <List <Stuff> >();
                return(stuff.Count.ToString());
            }));

            var bootstrapper = new TestBootstrapper(config => config.Module(module));

            // When
            var browser = new Browser(bootstrapper);
            var result  = await browser.Post("/stuff", with =>
            {
                with.HttpRequest();
                with.JsonBody(new List <Stuff> {
                    new Stuff(1), new Stuff(2)
                }, new JsonNetSerializer());
            });

            // Then
            Assert.Equal(2, int.Parse(result.Body.AsString()));
        }
Example #16
0
        private static ConfigurableNancyModule GetModule()
        {
            var module = new ConfigurableNancyModule(with => with.Get("/echo/{slug*}", (o, nancyModule) => o.slug));

            return(module);
        }
Example #17
0
        private static Browser GetBrowser(ConfigurableNancyModule module)
        {
            var browser = new Browser(with => with.Module(module));

            return(browser);
        }