public void BuildTrie(RouteCache cache) { foreach (var cacheItem in cache) { var moduleKey = cacheItem.Key; var routeDefinitions = cacheItem.Value; foreach (var routeDefinition in routeDefinitions) { var routeIndex = routeDefinition.Item1; var routeDescription = routeDefinition.Item2; TrieNode trieNode; if (!this._routeTries.TryGetValue(routeDescription.Method, out trieNode)) { trieNode = this._nodeFactory.GetNodeForSegment(null, null); this._routeTries.Add(routeDefinition.Item2.Method, trieNode); } var segments = routeDefinition.Item2.Segments.ToArray(); trieNode.Add(segments, moduleKey, routeIndex, routeDescription); } } }
private RequestDispatcher BuildRequestDispatcher(IModuleContainer container) { var moduleCatalog = new ModuleCatalog( () => { return container.GetAllModules(); }, (Type moduleType) => { return container.GetModule(moduleType); } ); var routeSegmentExtractor = new RouteSegmentExtractor(); var routeDescriptionProvider = new RouteDescriptionProvider(); var routeCache = new RouteCache(routeSegmentExtractor, routeDescriptionProvider); routeCache.BuildCache(moduleCatalog.GetAllModules()); var trieNodeFactory = new TrieNodeFactory(); var routeTrie = new RouteResolverTrie(trieNodeFactory); routeTrie.BuildTrie(routeCache); var serializers = new List<ISerializer>() { new JsonSerializer(), new XmlSerializer() }; var responseFormatterFactory = new ResponseFormatterFactory(serializers); var moduleBuilder = new ModuleBuilder(responseFormatterFactory); var routeResolver = new RouteResolver(moduleCatalog, moduleBuilder, routeTrie); var negotiator = new ResponseNegotiator(); var routeInvoker = new RouteInvoker(negotiator); var requestDispatcher = new RequestDispatcher(routeResolver, routeInvoker); return requestDispatcher; }
public static void Enable(DiagnosticsConfiguration diagnosticsConfiguration, IPipelines pipelines, IEnumerable<IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IEnumerable<ISerializer> serializers, IRequestTracing requestTracing, NancyInternalConfiguration configuration, IModelBinderLocator modelBinderLocator, IEnumerable<IResponseProcessor> responseProcessors, ICultureService cultureService) { var keyGenerator = new DefaultModuleKeyGenerator(); var diagnosticsModuleCatalog = new DiagnosticsModuleCatalog(keyGenerator, providers, rootPathProvider, requestTracing, configuration, diagnosticsConfiguration); var diagnosticsRouteCache = new RouteCache(diagnosticsModuleCatalog, keyGenerator, new DefaultNancyContextFactory(cultureService), new DefaultRouteSegmentExtractor(), new DefaultRouteDescriptionProvider(), cultureService); var diagnosticsRouteResolver = new DefaultRouteResolver( diagnosticsModuleCatalog, new DefaultRoutePatternMatcher(), new DiagnosticsModuleBuilder(rootPathProvider, serializers, modelBinderLocator), diagnosticsRouteCache, responseProcessors); var serializer = new DefaultObjectSerializer(); pipelines.BeforeRequest.AddItemToStartOfPipeline( new PipelineItem<Func<NancyContext, Response>>( PipelineKey, ctx => { if (!ctx.ControlPanelEnabled) { return null; } if (!ctx.Request.Path.StartsWith(diagnosticsConfiguration.Path, StringComparison.OrdinalIgnoreCase)) { return null; } ctx.Items[ItemsKey] = true; var resourcePrefix = string.Concat(diagnosticsConfiguration.Path, "/Resources/"); if (ctx.Request.Path.StartsWith(resourcePrefix, StringComparison.OrdinalIgnoreCase)) { var resourceNamespace = "Nancy.Diagnostics.Resources"; var path = Path.GetDirectoryName(ctx.Request.Url.Path.Replace(resourcePrefix, string.Empty)) ?? string.Empty; if (!string.IsNullOrEmpty(path)) { resourceNamespace += string.Format(".{0}", path.Replace('\\', '.')); } return new EmbeddedFileResponse( typeof(DiagnosticsHook).Assembly, resourceNamespace, Path.GetFileName(ctx.Request.Url.Path)); } RewriteDiagnosticsUrl(diagnosticsConfiguration, ctx); return diagnosticsConfiguration.Valid ? ExecuteDiagnostics(ctx, diagnosticsRouteResolver, diagnosticsConfiguration, serializer) : GetDiagnosticsHelpView(ctx); })); }
public void Should_not_return_a_route_if_matching_and_the_filter_returns_false() { // Given var moduleCatalog = new FakeModuleCatalog(); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake <INancyContextFactory>(), A.Fake <IRouteSegmentExtractor>(), this.routeDescriptionProvider); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder, routeCache, null); var request = new FakeRequest("GET", "/filtered"); var context = new NancyContext { Request = request }; // When var route = specificResolver.Resolve(context).Item1; // Then route.ShouldBeOfType(typeof(NotFoundRoute)); }
public void Should_return_route_whos_filter_returns_true_when_there_is_also_a_matching_route_with_a_failing_filter() { // Given var moduleCatalog = new FakeModuleCatalog(); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake <INancyContextFactory>(), A.Fake <IRouteSegmentExtractor>(), this.routeDescriptionProvider, A.Fake <ICultureService>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder, routeCache, null); var request = new FakeRequest("GET", "/filt"); var context = new NancyContext { Request = request }; // When var route = specificResolver.Resolve(context).Item1; // Then route.Description.Condition(context).ShouldBeTrue(); }
public void Should_return_prereq_and_postreq_from_module() { // Given var moduleCatalog = A.Fake <INancyModuleCatalog>(); A.CallTo(() => moduleCatalog.GetAllModules(A <NancyContext> .Ignored)).Returns(new[] { new FakeNancyModuleWithPreAndPostHooks() }); A.CallTo(() => moduleCatalog.GetModuleByKey(A <string> .Ignored, A <NancyContext> .Ignored)).Returns( new FakeNancyModuleWithPreAndPostHooks()); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake <INancyContextFactory>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder); var request = new FakeRequest("GET", "/PrePost"); var context = new NancyContext { Request = request }; // When var result = specificResolver.Resolve(context, routeCache); // Then result.Item3.ShouldNotBeNull(); result.Item4.ShouldNotBeNull(); }
public static void RegisterMazeServices(this ContainerBuilder builder, Action <RouteCache> configureRouteCache) { var routeCache = new RouteCache(); configureRouteCache(routeCache); builder.RegisterType <TrieNodeFactory>().As <ITrieNodeFactory>().InstancePerLifetimeScope(); builder.RegisterType <RouteResolverTrie>().As <IRouteResolverTrie>().SingleInstance(); builder.RegisterType <RouteResolver>().As <IRouteResolver>().InstancePerLifetimeScope(); builder.RegisterInstance(routeCache).As <IRouteCache>(); builder.RegisterType <MazeRequestExecuter>().As <IMazeRequestExecuter>().SingleInstance(); builder.RegisterType <ModelBinderFactory>().As <IModelBinderFactory>().SingleInstance(); builder.RegisterInstance(new OptionsWrapper <MazeServerOptions>(new MazeServerOptions())) .As <IOptions <MazeServerOptions> >(); builder.RegisterType <DefaultOutputFormatterSelector>().As <OutputFormatterSelector>().SingleInstance(); builder.RegisterType <MemoryPoolHttpResponseStreamWriterFactory>().As <IHttpResponseStreamWriterFactory>() .SingleInstance(); builder.RegisterType <ObjectResultExecutor>().As <IActionResultExecutor <ObjectResult> >().SingleInstance(); builder.RegisterType <FileStreamResultExecutor>().As <IActionResultExecutor <FileStreamResult> >() .SingleInstance(); }
public static void Enable( DiagnosticsConfiguration diagnosticsConfiguration, IPipelines pipelines, IEnumerable <IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IRequestTracing requestTracing, NancyInternalConfiguration configuration, IModelBinderLocator modelBinderLocator, IEnumerable <IResponseProcessor> responseProcessors, IEnumerable <IRouteSegmentConstraint> routeSegmentConstraints, ICultureService cultureService, IRequestTraceFactory requestTraceFactory, IEnumerable <IRouteMetadataProvider> routeMetadataProviders, ITextResource textResource) { var diagnosticsModuleCatalog = new DiagnosticsModuleCatalog(providers, rootPathProvider, requestTracing, configuration, diagnosticsConfiguration); var diagnosticsRouteCache = new RouteCache( diagnosticsModuleCatalog, new DefaultNancyContextFactory(cultureService, requestTraceFactory, textResource), new DefaultRouteSegmentExtractor(), new DefaultRouteDescriptionProvider(), cultureService, routeMetadataProviders); var diagnosticsRouteResolver = new DefaultRouteResolver( diagnosticsModuleCatalog, new DiagnosticsModuleBuilder(rootPathProvider, modelBinderLocator), diagnosticsRouteCache, new RouteResolverTrie(new TrieNodeFactory(routeSegmentConstraints))); var serializer = new DefaultObjectSerializer(); pipelines.BeforeRequest.AddItemToStartOfPipeline( new PipelineItem <Func <NancyContext, Response> >( PipelineKey, ctx => { if (!ctx.ControlPanelEnabled) { return(null); } if (!ctx.Request.Path.StartsWith(diagnosticsConfiguration.Path, StringComparison.OrdinalIgnoreCase)) { return(null); } ctx.Items[ItemsKey] = true; var resourcePrefix = string.Concat(diagnosticsConfiguration.Path, "/Resources/"); if (ctx.Request.Path.StartsWith(resourcePrefix, StringComparison.OrdinalIgnoreCase)) { var resourceNamespace = "Nancy.Diagnostics.Resources"; var path = Path.GetDirectoryName(ctx.Request.Url.Path.Replace(resourcePrefix, string.Empty)) ?? string.Empty; if (!string.IsNullOrEmpty(path)) { resourceNamespace += string.Format(".{0}", path.Replace(Path.DirectorySeparatorChar, '.')); } return(new EmbeddedFileResponse( typeof(DiagnosticsHook).Assembly, resourceNamespace, Path.GetFileName(ctx.Request.Url.Path))); } RewriteDiagnosticsUrl(diagnosticsConfiguration, ctx); return(diagnosticsConfiguration.Valid ? ExecuteDiagnostics(ctx, diagnosticsRouteResolver, diagnosticsConfiguration, serializer) : GetDiagnosticsHelpView(ctx)); })); }
public void Should_return_route_whos_filter_returns_true_when_there_is_also_a_matching_route_with_a_failing_filter() { // Given var moduleCatalog = new FakeModuleCatalog(); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake<INancyContextFactory>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder); var request = new FakeRequest("GET", "/filt"); var context = new NancyContext {Request = request}; // When var route = specificResolver.Resolve(context, routeCache).Item1; // Then route.Description.Condition(context).ShouldBeTrue(); }
public void Should_return_prereq_and_postreq_from_module() { // Given var moduleCatalog = A.Fake<INancyModuleCatalog>(); A.CallTo(() => moduleCatalog.GetAllModules(A<NancyContext>.Ignored)).Returns(new[] { new FakeNancyModuleWithPreAndPostHooks() }); A.CallTo(() => moduleCatalog.GetModuleByKey(A<string>.Ignored, A<NancyContext>.Ignored)).Returns( new FakeNancyModuleWithPreAndPostHooks()); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake<INancyContextFactory>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder); var request = new FakeRequest("GET", "/PrePost"); var context = new NancyContext { Request = request }; // When var result = specificResolver.Resolve(context, routeCache); // Then result.Item3.ShouldNotBeNull(); result.Item4.ShouldNotBeNull(); }
public void Should_return_a_route_if_matching_and_the_filter_returns_true() { // Given var moduleCatalog = new FakeModuleCatalog(); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake<INancyContextFactory>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder); var request = new FakeRequest("GET", "/notfiltered"); var context = new NancyContext {Request = request}; // When var route = specificResolver.Resolve(context, routeCache).Item1; // Then route.ShouldBeOfType(typeof (Route)); }
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(); }
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(); }
public ActionResult Index( ) { return(Redirect(RouteCache.Get(Url, RouteNames.AccountArea.Account_Login))); }
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 void Should_not_return_a_route_if_matching_and_the_filter_returns_false() { // Given var moduleCatalog = new FakeModuleCatalog(); var routeCache = new RouteCache(moduleCatalog, new FakeModuleKeyGenerator(), A.Fake<INancyContextFactory>(), A.Fake<IRouteSegmentExtractor>(), this.routeDescriptionProvider, A.Fake<ICultureService>()); var specificResolver = new DefaultRouteResolver(moduleCatalog, this.matcher, this.moduleBuilder, routeCache, null); var request = new FakeRequest("GET", "/filtered"); var context = new NancyContext { Request = request }; // When var route = specificResolver.Resolve(context).Item1; // Then route.ShouldBeOfType(typeof(NotFoundRoute)); }
/// <summary> /// Enables the diagnostics dashboard and will intercept all requests that are passed to /// the condigured paths. /// </summary> public static void Enable(IPipelines pipelines, IEnumerable<IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IRequestTracing requestTracing, NancyInternalConfiguration configuration, IModelBinderLocator modelBinderLocator, IEnumerable<IResponseProcessor> responseProcessors, IEnumerable<IRouteSegmentConstraint> routeSegmentConstraints, ICultureService cultureService, IRequestTraceFactory requestTraceFactory, IEnumerable<IRouteMetadataProvider> routeMetadataProviders, ITextResource textResource, INancyEnvironment environment, ITypeCatalog typeCatalog) { var diagnosticsConfiguration = environment.GetValue<DiagnosticsConfiguration>(); var diagnosticsEnvironment = GetDiagnosticsEnvironment(); var diagnosticsModuleCatalog = new DiagnosticsModuleCatalog(providers, rootPathProvider, requestTracing, configuration, diagnosticsEnvironment, typeCatalog); var diagnosticsRouteCache = new RouteCache( diagnosticsModuleCatalog, new DefaultNancyContextFactory(cultureService, requestTraceFactory, textResource, environment), new DefaultRouteSegmentExtractor(), new DefaultRouteDescriptionProvider(), cultureService, routeMetadataProviders); var diagnosticsRouteResolver = new DefaultRouteResolver( diagnosticsModuleCatalog, new DiagnosticsModuleBuilder(rootPathProvider, modelBinderLocator, diagnosticsEnvironment, environment), diagnosticsRouteCache, new RouteResolverTrie(new TrieNodeFactory(routeSegmentConstraints)), environment); var serializer = new DefaultObjectSerializer(); pipelines.BeforeRequest.AddItemToStartOfPipeline( new PipelineItem<Func<NancyContext, Response>>( PipelineKey, ctx => { if (!ctx.ControlPanelEnabled) { return null; } if (!ctx.Request.Path.StartsWith(diagnosticsConfiguration.Path, StringComparison.OrdinalIgnoreCase)) { return null; } if (!diagnosticsConfiguration.Enabled) { return HttpStatusCode.NotFound; } ctx.Items[ItemsKey] = true; var resourcePrefix = string.Concat(diagnosticsConfiguration.Path, "/Resources/"); if (ctx.Request.Path.StartsWith(resourcePrefix, StringComparison.OrdinalIgnoreCase)) { var resourceNamespace = "Nancy.Diagnostics.Resources"; var path = Path.GetDirectoryName(ctx.Request.Url.Path.Replace(resourcePrefix, string.Empty)) ?? string.Empty; if (!string.IsNullOrEmpty(path)) { resourceNamespace += string.Format(".{0}", path.Replace(Path.DirectorySeparatorChar, '.')); } return new EmbeddedFileResponse( typeof(DiagnosticsHook).Assembly, resourceNamespace, Path.GetFileName(ctx.Request.Url.Path)); } RewriteDiagnosticsUrl(diagnosticsConfiguration, ctx); return ValidateConfiguration(diagnosticsConfiguration) ? ExecuteDiagnostics(ctx, diagnosticsRouteResolver, diagnosticsConfiguration, serializer, diagnosticsEnvironment) : new DiagnosticsViewRenderer(ctx, environment)["help"]; })); }
/// <summary> /// Enables the diagnostics dashboard and will intercept all requests that are passed to /// the condigured paths. /// </summary> public static void Enable(IPipelines pipelines, IEnumerable <IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IRequestTracing requestTracing, NancyInternalConfiguration configuration, IModelBinderLocator modelBinderLocator, IEnumerable <IResponseProcessor> responseProcessors, IEnumerable <IRouteSegmentConstraint> routeSegmentConstraints, ICultureService cultureService, IRequestTraceFactory requestTraceFactory, IEnumerable <IRouteMetadataProvider> routeMetadataProviders, ITextResource textResource, INancyEnvironment environment, ITypeCatalog typeCatalog, IAssemblyCatalog assemblyCatalog, AcceptHeaderCoercionConventions acceptHeaderCoercionConventions) { var diagnosticsConfiguration = environment.GetValue <DiagnosticsConfiguration>(); var diagnosticsEnvironment = GetDiagnosticsEnvironment(); var diagnosticsModuleCatalog = new DiagnosticsModuleCatalog(providers, rootPathProvider, requestTracing, configuration, diagnosticsEnvironment, typeCatalog, assemblyCatalog); var diagnosticsRouteCache = new RouteCache( diagnosticsModuleCatalog, new DefaultNancyContextFactory(cultureService, requestTraceFactory, textResource, environment), new DefaultRouteSegmentExtractor(), new DefaultRouteDescriptionProvider(), cultureService, routeMetadataProviders); var diagnosticsRouteResolver = new DefaultRouteResolver( diagnosticsModuleCatalog, new DiagnosticsModuleBuilder(rootPathProvider, modelBinderLocator, diagnosticsEnvironment, environment), diagnosticsRouteCache, new RouteResolverTrie(new TrieNodeFactory(routeSegmentConstraints)), environment); var diagnosticResponseNegotiator = new DefaultResponseNegotiator(responseProcessors, acceptHeaderCoercionConventions); var diagnosticRouteInvoker = new DefaultRouteInvoker(diagnosticResponseNegotiator); var serializer = new DefaultObjectSerializer(); pipelines.BeforeRequest.AddItemToStartOfPipeline( new PipelineItem <Func <NancyContext, Response> >( PipelineKey, ctx => { if (!ctx.ControlPanelEnabled) { return(null); } if (!ctx.Request.Path.StartsWith(diagnosticsConfiguration.Path, StringComparison.OrdinalIgnoreCase)) { return(null); } if (!diagnosticsConfiguration.Enabled) { return(HttpStatusCode.NotFound); } ctx.Items[ItemsKey] = true; var resourcePrefix = string.Concat(diagnosticsConfiguration.Path, "/Resources/"); if (ctx.Request.Path.StartsWith(resourcePrefix, StringComparison.OrdinalIgnoreCase)) { var resourceNamespace = "Nancy.Diagnostics.Resources"; var path = Path.GetDirectoryName(ctx.Request.Url.Path.Replace(resourcePrefix, string.Empty)) ?? string.Empty; if (!string.IsNullOrEmpty(path)) { resourceNamespace += string.Format(".{0}", path.Replace(Path.DirectorySeparatorChar, '.')); } return(new EmbeddedFileResponse( typeof(DiagnosticsHook).GetTypeInfo().Assembly, resourceNamespace, Path.GetFileName(ctx.Request.Url.Path))); } RewriteDiagnosticsUrl(diagnosticsConfiguration, ctx); return(ValidateConfiguration(diagnosticsConfiguration) ? ExecuteDiagnostics(ctx, diagnosticsRouteResolver, diagnosticsConfiguration, serializer, diagnosticsEnvironment, diagnosticRouteInvoker) : new DiagnosticsViewRenderer(ctx, environment)["help"]); })); }