public void Should_add_negotiated_content_headers_to_response()
        {
            // Given

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

              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);
        }
        public void Should_add_negotiated_headers_to_response()
        {
            // Given

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

                    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());
        }
Exemple #4
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");
        }
        public async Task 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 = await browser.Get("/httpstatuscode");

            // Then
            Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
        }
        public async Task 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 = await browser.Get("/string");

            // Then
            Assert.Equal("hello", response.Body.AsString());
        }
        public async Task 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 = await browser.Get("/int");

            // Then
            Assert.Equal((HttpStatusCode)200, response.StatusCode);
        }
        public void 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 = 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()));
        }
        public async Task 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 = await browser.Post("/stuff", with =>
            {
                with.HttpRequest();
                with.JsonBody(new Stuff(1), new JsonNetSerializer());
            });

            // Then
            Assert.Equal(1, int.Parse(result.Body.AsString()));
        }
        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 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());
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigurableNancyModuleConfigurator"/> class.
 /// </summary>
 /// <param name="module">The <see cref="ConfigurableNancyModule"/> that should be configured.</param>
 public ConfigurableNancyModuleConfigurator(ConfigurableNancyModule module)
 {
     this.module = module;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigurableNancyModuleConfigurator"/> class.
 /// </summary>
 /// <param name="module">The <see cref="ConfigurableNancyModule"/> that should be configured.</param>
 public ConfigurableNancyModuleConfigurator(ConfigurableNancyModule module)
 {
     this.module = module;
 }
        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!").WithStatusCode(404);

                    return negotiator;
                });
            });

            var browser = new Browser(with =>
            {
                with.StatusCodeHandler<DefaultStatusCodeHandler>();
                with.ResponseProcessor<TestProcessor>();
                with.Module(module);
            });

            // When
            var response = browser.Get("/customPhrase", with => with.Accept("application/json"));

            // Then
            Assert.Equal("The test is passing!", response.ReasonPhrase);
        }
Exemple #15
0
 private static ConfigurableNancyModule GetModule()
 {
     var module = new ConfigurableNancyModule(with => with.Get("/echo/{slug*}", (o, nancyModule) => o.slug));
     return module;
 }
Exemple #16
0
 private static Browser GetBrowser(ConfigurableNancyModule module)
 {
     var browser = new Browser(with => with.Module(module));
     return browser;
 }