Example #1
0
        public void Should_return_first_route_with_when_multiple_matches_are_available_and_contains_same_number_of_parameter_captures()
        {
            // Given
            var request = new FakeRequest("get", "/foo/bar");
            var context = new NancyContext();

            context.Request = request;
            var routeCache = new FakeRouteCache(x =>
            {
                x.AddGetRoute("/foo/{bar}", "first-module-key-parameters");
                x.AddGetRoute("/foo/{bar}", "second-module-key-parameters");
            });

            this.expectedModule = new FakeNancyModule(x =>
            {
                x.AddGetRoute("/foo/{bar}", this.expectedAction);
            });

            A.CallTo(() => this.matcher.Match(request.Uri, "/foo/{bar}")).Returns(
                new FakeRoutePatternMatchResult(x => x.IsMatch(true).AddParameter("bar", "fake value")));

            // When
            this.resolver.Resolve(context, routeCache);

            // Then
            A.CallTo(() => this.catalog.GetModuleByKey("first-module-key-parameters", context)).MustHaveHappened();
        }
Example #2
0
        public DefaultRouteResolverFixture()
        {
            this.moduleBuilder = A.Fake <INancyModuleBuilder>();
            A.CallTo(() => this.moduleBuilder.BuildModule(A <NancyModule> .Ignored, A <NancyContext> .Ignored)).
            ReturnsLazily(r => r.Arguments[0] as NancyModule);

            this.catalog = A.Fake <INancyModuleCatalog>();
            A.CallTo(() => this.catalog.GetModuleByKey(A <string> .Ignored, A <NancyContext> .Ignored)).Returns(expectedModule);

            this.expectedAction = x => HttpStatusCode.OK;
            this.expectedModule = new FakeNancyModule(x =>
            {
                x.AddGetRoute("/foo/bar", this.expectedAction);
            });

            A.CallTo(() => this.catalog.GetModuleByKey(A <string> .Ignored, A <NancyContext> .Ignored)).ReturnsLazily(() => this.expectedModule);

            this.matcher = A.Fake <IRoutePatternMatcher>();
            A.CallTo(() => this.matcher.Match(A <string> .Ignored, A <string> .Ignored)).ReturnsLazily(x =>
                                                                                                       new FakeRoutePatternMatchResult(c =>
            {
                c.IsMatch(((string)x.Arguments[0]).Equals(((string)x.Arguments[1])));
                c.AddParameter("foo", "bar");
            }));

            this.resolver = new DefaultRouteResolver(this.catalog, this.matcher, this.moduleBuilder);
        }
Example #3
0
        public void Should_invoke_route_description_provider_with_module_that_route_is_defined_in()
        {
            // Given
            var module = new FakeNancyModule(with =>
            {
                with.AddGetRoute("/");
            });

            var catalog = A.Fake <INancyModuleCatalog>();

            A.CallTo(() => catalog.GetAllModules(A <NancyContext> ._)).Returns(new[] { module });

            var descriptionProvider =
                A.Fake <IRouteDescriptionProvider>();

            // When
            var cache = new RouteCache(
                catalog,
                A.Fake <INancyContextFactory>(),
                this.routeSegmentExtractor,
                descriptionProvider,
                A.Fake <ICultureService>(),
                new IRouteMetadataProvider[0]);

            // Then
            A.CallTo(() => descriptionProvider.GetDescription(module, A <string> ._)).MustHaveHappened();
        }
Example #4
0
        public void Should_invoke_route_description_provider_with_path_of_route()
        {
            // Given
            const string expectedPath = "/some/path/{capture}";

            var module = new FakeNancyModule(with =>
            {
                with.AddGetRoute(expectedPath);
            });

            var catalog = A.Fake <INancyModuleCatalog>();

            A.CallTo(() => catalog.GetAllModules(A <NancyContext> ._)).Returns(new[] { module });

            var descriptionProvider =
                A.Fake <IRouteDescriptionProvider>();

            // When
            var cache = new RouteCache(
                catalog,
                A.Fake <INancyContextFactory>(),
                this.routeSegmentExtractor,
                descriptionProvider,
                A.Fake <ICultureService>(),
                new IRouteMetadataProvider[0]);

            // Then
            A.CallTo(() => descriptionProvider.GetDescription(A <NancyModule> ._, expectedPath)).MustHaveHappened();
        }
        public void Should_handle_null_metadata()
        {
            // Given
            var module = new FakeNancyModule(with =>
            {
                with.AddGetRoute("/");
            });

            var catalog = A.Fake <INancyModuleCatalog>();

            A.CallTo(() => catalog.GetAllModules(A <NancyContext> ._)).Returns(new[] { module });

            var metadataProvider = A.Fake <IRouteMetadataProvider>();

            A.CallTo(() => metadataProvider.GetMetadata(null, null)).WithAnyArguments().Returns(null);

            // When
            var cache = new RouteCache(
                catalog,
                A.Fake <INancyContextFactory>(),
                this.routeSegmentExtractor,
                A.Fake <IRouteDescriptionProvider>(),
                A.Fake <ICultureService>(),
                new[] { metadataProvider });

            // Then
            cache[module.GetType()][0].Item2.Metadata.Raw.Count.ShouldEqual(0);
        }
        public DefaultRouteResolverFixture()
        {
            this.routeDescriptionProvider = A.Fake <IRouteDescriptionProvider>();

            this.moduleBuilder = A.Fake <INancyModuleBuilder>();
            A.CallTo(() => this.moduleBuilder.BuildModule(A <NancyModule> .Ignored, A <NancyContext> .Ignored)).
            ReturnsLazily(r => r.Arguments[0] as NancyModule);

            this.catalog = A.Fake <INancyModuleCatalog>();
            A.CallTo(() => this.catalog.GetModuleByKey(A <string> .Ignored, A <NancyContext> .Ignored)).Returns(expectedModule);

            this.expectedAction = x => HttpStatusCode.OK;
            this.expectedModule = new FakeNancyModule(x => x.AddGetRoute("/foo/bar", this.expectedAction));

            A.CallTo(() => this.catalog.GetModuleByKey(A <string> .Ignored, A <NancyContext> .Ignored)).ReturnsLazily(() => this.expectedModule);

            this.matcher = A.Fake <IRoutePatternMatcher>();
            A.CallTo(() => this.matcher.Match(A <string> .Ignored, A <string> .Ignored, A <IEnumerable <string> > .Ignored, A <NancyContext> .Ignored)).ReturnsLazily(x =>
                                                                                                                                                                      new FakeRoutePatternMatchResult(c =>
            {
                c.IsMatch(((string)x.Arguments[0]).Equals(((string)x.Arguments[1])));
                c.AddParameter("foo", "bar");
            }));

            this.moduleKeyGenerator = A.Fake <IModuleKeyGenerator>();
            A.CallTo(() => moduleKeyGenerator.GetKeyForModuleType(A <Type> ._)).ReturnsLazily(x => (string)x.Arguments[0]);
        }
 public ModuleExtensionsFixture()
 {
     this.validatorLocator = A.Fake <IModelValidatorLocator>();
     this.subject          = new FakeNancyModule
     {
         ValidatorLocator = validatorLocator
     };
 }
 public ModuleExtensionsFixture()
 {
     this.context          = new NancyContext();
     this.validatorLocator = A.Fake <IModelValidatorLocator>();
     this.subject          = new FakeNancyModule
     {
         Context          = this.context,
         ValidatorLocator = this.validatorLocator
     };
 }
Example #9
0
        public DefaultNancyModuleBuilderFixture()
        {
            this.module = new FakeNancyModule();

            this.responseFormatterFactory =
                A.Fake <IResponseFormatterFactory>();

            this.viewFactory        = A.Fake <IViewFactory>();
            this.modelBinderLocator = A.Fake <IModelBinderLocator>();
            this.validatorLocator   = A.Fake <IModelValidatorLocator>();

            this.builder = new DefaultNancyModuleBuilder(this.viewFactory, this.responseFormatterFactory, this.modelBinderLocator, this.validatorLocator);
        }
Example #10
0
        public void ValidateCsrfToken_gets_provided_token_from_request_header_if_not_present_in_form_data()
        {
            // Given
            var token   = Csrf.GenerateTokenString();
            var context = new NancyContext();
            var module  = new FakeNancyModule {
                Context = context
            };

            // When
            context.Request = RequestWithHeader(CsrfToken.DEFAULT_CSRF_KEY, token);
            context.Request.Cookies.Add(CsrfToken.DEFAULT_CSRF_KEY, token);

            // Then
            module.ValidateCsrfToken();
        }
Example #11
0
        public void ValidateCsrfToken_gets_provided_token_from_form_data()
        {
            // Given
            var token   = Csrf.GenerateTokenString();
            var context = new NancyContext {
                Request = this.request
            };
            var module = new FakeNancyModule {
                Context = context
            };

            // When
            context.Request.Form[CsrfToken.DEFAULT_CSRF_KEY] = token;
            context.Request.Cookies.Add(CsrfToken.DEFAULT_CSRF_KEY, token);

            // Then
            module.ValidateCsrfToken();
        }
Example #12
0
        public void Should_return_route_with_module_returned_by_module_builder()
        {
            // Given
            var request = new FakeRequest("GET", "/foo/bar");
            var context = new NancyContext {
                Request = request
            };
            var routeCache = new FakeRouteCache(x => x.AddGetRoute("/foo/bar"));

            var moduleReturnedByBuilder = new FakeNancyModule(x => x.AddGetRoute("/bar/foo"));

            A.CallTo(() => this.moduleBuilder.BuildModule(A <NancyModule> .Ignored, A <NancyContext> .Ignored)).Returns(
                moduleReturnedByBuilder);

            // When
            var route = this.resolver.Resolve(context, routeCache);

            // Then
            route.Item1.Description.Path.ShouldEqual("/bar/foo");
        }
        public void Should_find_exact_match()
        {
            // Given
            var catalog = A.Fake<INancyModuleCatalog>();
            A.CallTo(() => catalog.GetModuleByKey(A<string>._, A<NancyContext>._)).Returns(new FakeNancyModule());

            var cache = new FakeRouteCache(with =>
            {
                with.AddGetRoute("/foo", "modulekey");
            });

            var module = new FakeNancyModule(with => {
                with.AddGetRoute("/foo");
            });

            var bootstrapper = new ConfigurableBootstrapper(with =>{
                with.Module(module, "modulekey");
                with.RouteCache(cache);
            });

            var browser = new Browser(bootstrapper);

            // When
            var timer = new Stopwatch();
            timer.Start();

            for (var i = 0; i < numberOfTimesToResolveRoute; i++)
            {
                var result = browser.Get("/foo");
                result.StatusCode.ShouldEqual(HttpStatusCode.OK);
            }

            timer.Stop();

            // Then
            Debug.WriteLine(" took {0} to execute {1} times", timer.Elapsed, numberOfTimesToResolveRoute);
        }
        public void Should_return_the_route_with_the_most_specific_path_matches()
        {
            // The most specific path match is the one with the most matching segments (delimited by '/') and the
            // least parameter captures (i.e. the most exact matching segments)
            // Given
            var request = new FakeRequest("get", "/foo/bar/me");
            var context = new NancyContext {
                Request = request
            };

            var resolver = CreateResolver(x =>
            {
                x.AddGetRoute("/foo/{bar}", "module-key-first");
                x.AddGetRoute("/foo/{bar}/{two}", "module-key-third");
                x.AddGetRoute("/foo/bar/{two}", "module-key-second");
                x.AddGetRoute("/foo/{bar}/{two}", "module-key-third");
            });

            this.expectedModule = new FakeNancyModule(x => x.AddGetRoute("/foo/bar/{two}", this.expectedAction));

            A.CallTo(() => this.matcher.Match(request.Path, "/foo/bar/{two}", A <NancyContext> .Ignored)).Returns(
                new FakeRoutePatternMatchResult(x => x.IsMatch(true).AddParameter("two", "fake values")));

            A.CallTo(() => this.matcher.Match(request.Path, "/foo/{bar}/{two}", A <NancyContext> .Ignored)).Returns(
                new FakeRoutePatternMatchResult(x => x.IsMatch(true)
                                                .AddParameter("bar", "fake values")
                                                .AddParameter("two", "fake values")));

            A.CallTo(() => this.matcher.Match(request.Path, "/foo/{bar}", A <NancyContext> .Ignored)).Returns(
                new FakeRoutePatternMatchResult(x => x.IsMatch(true).AddParameter("bar", "fake value")));

            // When
            resolver.Resolve(context);

            // Then
            A.CallTo(() => this.catalog.GetModuleByKey("module-key-second", context)).MustHaveHappened();
        }