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 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);
        }
 public ModuleExtensionsFixture()
 {
     this.context = new NancyContext();
     this.validatorLocator = A.Fake<IModelValidatorLocator>();
     this.subject = new FakeNancyModule
     {
         Context = this.context,
         ValidatorLocator = this.validatorLocator
     };
 }
        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);
        }
        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 routeCache = new FakeRouteCache(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}")).Returns(
                new FakeRoutePatternMatchResult(x => x.IsMatch(true).AddParameter("two", "fake values")));

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

            A.CallTo(() => this.matcher.Match(request.Path, "/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("module-key-second", context)).MustHaveHappened();
        }
        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_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.Path, "/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();
        }
Exemple #8
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>());

            // Then
            A.CallTo(() => descriptionProvider.GetDescription(A<NancyModule>._, expectedPath)).MustHaveHappened();
        }
Exemple #9
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>());

            // Then
            A.CallTo(() => descriptionProvider.GetDescription(module, A<string>._)).MustHaveHappened();
        }
Exemple #10
0
        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);
        }
Exemple #11
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, HttpUtility.UrlEncode(token));

            // Then
            module.ValidateCsrfToken();
        }
Exemple #12
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, HttpUtility.UrlEncode(token));

            // Then
            module.ValidateCsrfToken();
        }
        public void Should_return_route_with_most_parameter_captures_when_multiple_matches_are_available()
        {
            // Given
            var request = new FakeRequest("get", "/foo/bar");
            var routeCache = new FakeRouteCache(x =>
            {
                x.AddGetRoute("/foo/bar", "module-key-noparameters");
                x.AddGetRoute("/foo/{bar}", "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)));

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

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

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