public void Should_not_throw_exception_when_setting_nancy_version_header_and_it_already_existed() { // Given var cachedResponse = new Response(); cachedResponse.Headers.Add("Nancy-Version", "1.2.3.4"); Func <NancyContext, Response> preRequestHook = (ctx) => cachedResponse; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preRequestHook, null)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new[] { this.errorHandler }, A.Fake <IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When var exception = Record.Exception(() => localEngine.HandleRequest(request)); // Then exception.ShouldBeNull(); }
public void HandleRequest_should_call_route_prereq_then_invoke_route_then_call_route_postreq() { // Given var executionOrder = new List <String>(); Func <NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return(null); }; Action <NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return(null); }; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, new IErrorHandler[] { this.errorHandler }) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Prehook", "RouteInvoke", "Posthook" }).ShouldBeTrue(); }
public async Task Should_return_static_content_response_if_one_returned() { // Given var localResponse = new Response(); var staticContent = A.Fake <IStaticContentProvider>(); A.CallTo(() => staticContent.GetContent(A <NancyContext> ._)) .Returns(localResponse); var localEngine = new NancyEngine( this.requestDispatcher, this.contextFactory, new[] { this.statusCodeHandler }, A.Fake <IRequestTracing>(), staticContent, this.negotiator , this.environment); var request = new Request("GET", "/", "http"); // When var result = await localEngine.HandleRequest(request); // Then result.Response.ShouldBeSameAs(localResponse); }
public void HandleRequest_should_not_invoke_route_if_route_prereq_returns_response() { // Given var executionOrder = new List <String>(); Func <NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return(new Response()); }; Action <NancyContext> postHook = (ctx) => executionOrder.Add("Posthook"); this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return(null); }; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake <IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Contains("RouteInvoke").ShouldBeFalse(); }
public void HandleRequest_route_prereq_returns_response_should_still_run_route_postreq_and_postreq() { // Given var executionOrder = new List <String>(); Action <NancyContext> postHook = (ctx) => executionOrder.Add("Posthook"); Func <NancyContext, Response> routePreHook = (ctx) => { executionOrder.Add("Routeprehook"); return(new Response()); }; Action <NancyContext> routePostHook = (ctx) => executionOrder.Add("Routeposthook"); this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return(null); }; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, routePreHook, routePostHook)); var pipelines = new Pipelines(); pipelines.AfterRequest.AddItemToStartOfPipeline(postHook); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake <IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Routeprehook", "Routeposthook", "Posthook" }).ShouldBeTrue(); }
public void HandleRequest_should_allow_route_postreq_to_change_response() { // Given var postResponse = new Response(); Action <NancyContext> postHook = (ctx) => ctx.Response = postResponse; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake <IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When var result = localEngine.HandleRequest(request); // Then result.Response.ShouldBeSameAs(postResponse); }
public async Task Should_catch_exception_inside_status_code_handler_and_return_default_error_response() { // Given A.CallTo(() => this.requestDispatcher.Dispatch(A <NancyContext> .Ignored, A <CancellationToken> .Ignored)) .Returns(new Response() { StatusCode = HttpStatusCode.InternalServerError }); var statusCodeHandlers = new[] { this.statusCodeHandler, new DefaultStatusCodeHandler(this.negotiator, this.environment), }; var engine = new NancyEngine(this.requestDispatcher, this.contextFactory, statusCodeHandlers, A.Fake <IRequestTracing>(), new DisabledStaticContentProvider(), this.negotiator, this.environment) { RequestPipelinesFactory = ctx => new Pipelines() }; var request = new Request("GET", "/", "http"); A.CallTo(() => this.statusCodeHandler.HandlesStatusCode(A <HttpStatusCode> .Ignored, A <NancyContext> .Ignored)).Returns(true); A.CallTo(() => this.statusCodeHandler.Handle(A <HttpStatusCode> .Ignored, A <NancyContext> .Ignored)) .Throws <Exception>(); // When await engine.HandleRequest(request); // Then this.context.Response.StatusCode.ShouldEqual(HttpStatusCode.InternalServerError); }
private Message HandleAll(Stream body) { var context = WebOperationContext.Current; var request = CreateNancyRequestFromIncomingRequest(context.IncomingRequest, body); var response = engine.HandleRequest(request); SetNancyResponseToOutgoingResponse(context.OutgoingResponse, response); return(context.CreateStreamResponse(response.Contents, response.ContentType)); }
public void HandleRequest_should_allow_route_postreq_to_add_items_to_context() { Action <NancyContext> postHook = (ctx) => ctx.Items.Add("RoutePostReq", new object()); var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Items.ContainsKey("RoutePostReq").ShouldBeTrue(); }
public void HandleRequest_should_allow_route_postreq_to_change_response() { var postResponse = new Response(); Action <NancyContext> postHook = (ctx) => ctx.Response = postResponse; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(postResponse); }
public void HandleRequest_should_return_response_from_route_prereq_if_one_returned() { var preResponse = new Response(); Func <NancyContext, Response> preHook = (ctx) => preResponse; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, null)); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(preResponse); }
public void Should_set_path_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/", new Dictionary <string, IEnumerable <string> >(), new MemoryStream()); var r = new FakeRouteResolver(); var e = new NancyEngine(this.locator, r); A.CallTo(() => this.locator.GetModules()).Returns(this.modules); // When e.HandleRequest(request); // Then r.Path.ShouldEqual("/"); }
public async Task Should_throw_operationcancelledexception_when_disposed_handling_request() { // Given var request = new Request("GET", "/", "http"); var engine = new NancyEngine(A.Fake <IRequestDispatcher>(), A.Fake <INancyContextFactory>(), new[] { this.statusCodeHandler }, A.Fake <IRequestTracing>(), new DisabledStaticContentProvider(), this.negotiator, this.environment); engine.Dispose(); // When var exception = await RecordAsync.Exception(async() => await engine.HandleRequest(request)); // Then exception.ShouldBeOfType <OperationCanceledException>(); }
public void ProcessRequest(HttpContextBase context) { var url = context.Request.Url.AbsolutePath; if (url.Contains("favicon.ico")) { return; } var request = CreateNancyRequest(context); var engine = new NancyEngine( new AppDomainModuleLocator(), new RouteResolver()); var response = engine.HandleRequest(request); SetNancyResponseToHttpResponse(context, response); }
public void HandleRequest_should_not_invoke_route_if_route_prereq_returns_response() { var executionOrder = new List <String>(); Func <NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return(new Response()); }; Action <NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return(null); }; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); localEngine.HandleRequest(request); executionOrder.Contains("RouteInvoke").ShouldBeFalse(); }
public void Should_set_action_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/", new Dictionary <string, IEnumerable <string> >(), new MemoryStream()); var r = new FakeRouteResolver(); var e = new NancyEngine(this.locator, r); A.CallTo(() => this.locator.GetModules()).Returns(this.modules); var expectedAction = (new FakeNancyModuleWithBasePath()).Post["/"]; // When e.HandleRequest(request); // Then r.Action.ShouldBeSameAs(expectedAction); }
public void Should_return_static_content_response_if_one_returned() { var localResponse = new Response(); var staticContent = A.Fake <IStaticContentProvider>(); A.CallTo(() => staticContent.GetContent(A <NancyContext> ._)) .Returns(localResponse); var localEngine = new NancyEngine( this.requestDispatcher, this.contextFactory, new[] { this.statusCodeHandler }, A.Fake <IRequestTracing>(), this.diagnosticsConfiguration, staticContent); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(localResponse); }
public void ProcessRequest(HttpContext context) { var url = context.Request.Url.AbsolutePath; if (url.Contains("favicon.ico")) { return; } var request = CreateNancyRequest(context); var assembly = context.ApplicationInstance.GetType().BaseType.Assembly; var engine = new NancyEngine(new NancyModuleLocator(assembly), new RouteResolver()); var response = engine.HandleRequest(request); SetNancyResponseToHttpResponse(context, response); }
public void HandleRequest_route_prereq_returns_response_should_still_run_route_postreq_and_postreq() { var executionOrder = new List <String>(); Action <NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; Func <NancyContext, Response> routePreHook = (ctx) => { executionOrder.Add("Routeprehook"); return(new Response()); }; Action <NancyContext> routePostHook = (ctx) => { executionOrder.Add("Routeposthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return(null); }; var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored, A <IRouteCache> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, routePreHook, routePostHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake <IRouteCache>(), contextFactory, this.errorHandler); localEngine.PostRequestHook = postHook; var request = new Request("GET", "/", "http"); localEngine.HandleRequest(request); executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Routeprehook", "Routeposthook", "Posthook" }).ShouldBeTrue(); }
public void HandleRequest_should_allow_route_postreq_to_add_items_to_context() { // Given Action <NancyContext> postHook = (ctx) => ctx.Items.Add("RoutePostReq", new object()); var prePostResolver = A.Fake <IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A <NancyContext> .Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake <IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When var result = localEngine.HandleRequest(request); // Then result.Items.ContainsKey("RoutePostReq").ShouldBeTrue(); }
public void HandleRequest_should_not_invoke_route_if_route_prereq_returns_response() { var executionOrder = new List<String>(); Func<NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return new Response(); }; Action<NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return null; }; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); localEngine.HandleRequest(request); executionOrder.Contains("RouteInvoke").ShouldBeFalse(); }
public void HandleRequest_should_allow_route_postreq_to_change_response() { var postResponse = new Response(); Action<NancyContext> postHook = (ctx) => ctx.Response = postResponse; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(postResponse); }
public void HandleRequest_should_call_route_prereq_then_invoke_route_then_call_route_postreq() { // Given var executionOrder = new List<String>(); Func<NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return null; }; Action<NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return null; }; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Prehook", "RouteInvoke", "Posthook" }).ShouldBeTrue(); }
public void HandleRequest_should_allow_route_postreq_to_add_items_to_context() { // Given Action<NancyContext> postHook = (ctx) => ctx.Items.Add("RoutePostReq", new object()); var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When var result = localEngine.HandleRequest(request); // Then result.Items.ContainsKey("RoutePostReq").ShouldBeTrue(); }
public void HandleRequest_should_allow_route_postreq_to_add_items_to_context() { Action<NancyContext> postHook = (ctx) => ctx.Items.Add("RoutePostReq", new object()); var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, postHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Items.ContainsKey("RoutePostReq").ShouldBeTrue(); }
public void Should_set_path_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/"); var r = new FakeRouteResolver(); var e = new NancyEngine( r, this.application); A.CallTo(() => this.application.ModuleMetas).Returns(this.modules); // When e.HandleRequest(request); // Then r.Path.ShouldEqual("/"); }
public void HandleRequest_should_not_invoke_route_if_route_prereq_returns_response() { // Given var executionOrder = new List<String>(); Func<NancyContext, Response> preHook = (ctx) => { executionOrder.Add("Prehook"); return new Response(); }; Action<NancyContext> postHook = (ctx) => executionOrder.Add("Posthook"); this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return null; }; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, postHook)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Contains("RouteInvoke").ShouldBeFalse(); }
public async Task Should_throw_operationcancelledexception_when_disposed_handling_request() { // Given var request = new Request("GET", "/", "http"); var engine = new NancyEngine(A.Fake<IRequestDispatcher>(), A.Fake<INancyContextFactory>(), new[] {this.statusCodeHandler}, A.Fake<IRequestTracing>(), new DisabledStaticContentProvider(), this.negotiator); engine.Dispose(); // When var exception = await RecordAsync.Exception(async () => await engine.HandleRequest(request)); // Then exception.ShouldBeOfType<OperationCanceledException>(); }
public void Should_return_static_content_response_if_one_returned() { var localResponse = new Response(); var staticContent = A.Fake<IStaticContentProvider>(); A.CallTo(() => staticContent.GetContent(A<NancyContext>._)) .Returns(localResponse); var localEngine = new NancyEngine( this.requestDispatcher, this.contextFactory, new[] { this.statusCodeHandler }, A.Fake<IRequestTracing>(), staticContent, this.negotiator); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(localResponse); }
public void Should_set_path_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/", new Dictionary<string, IEnumerable<string>>(), new MemoryStream()); var r = new FakeRouteResolver(); var e = new NancyEngine(this.locator, r); A.CallTo(() => this.locator.GetModules()).Returns(this.modules); // When e.HandleRequest(request); // Then r.Path.ShouldEqual("/"); }
public void HandleRequest_route_prereq_returns_response_should_still_run_route_postreq_and_postreq() { // Given var executionOrder = new List<String>(); Action<NancyContext> postHook = (ctx) => executionOrder.Add("Posthook"); Func<NancyContext, Response> routePreHook = (ctx) => { executionOrder.Add("Routeprehook"); return new Response(); }; Action<NancyContext> routePostHook = (ctx) => executionOrder.Add("Routeposthook"); this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return null; }; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, routePreHook, routePostHook)); var pipelines = new Pipelines(); pipelines.AfterRequest.AddItemToStartOfPipeline(postHook); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When localEngine.HandleRequest(request); // Then executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Routeprehook", "Routeposthook", "Posthook" }).ShouldBeTrue(); }
public void Should_set_action_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/", new Dictionary<string, IEnumerable<string>>(), new MemoryStream()); var r = new FakeRouteResolver(); var e = new NancyEngine(this.locator, r); A.CallTo(() => this.locator.GetModules()).Returns(this.modules); var expectedAction = (new FakeNancyModuleWithBasePath()).Post["/"]; // When e.HandleRequest(request); // Then r.Action.ShouldBeSameAs(expectedAction); }
public void Should_set_base_route_on_descriptions_that_are_passed_to_resolver() { // Given var request = new Request("POST", "/"); var r = new FakeRouteResolver(); var e = new NancyEngine(this.locator, r); A.CallTo(() => this.locator.GetModules()).Returns(this.modules); // When e.HandleRequest(request); // Then r.ModulePath.ShouldEqual("/fake"); }
public async Task Should_catch_exception_inside_status_code_handler_and_return_default_error_response() { // Given A.CallTo(() => this.requestDispatcher.Dispatch(A<NancyContext>.Ignored, A<CancellationToken>.Ignored)) .Returns(new Response() { StatusCode = HttpStatusCode.InternalServerError }); var statusCodeHandlers = new[] { this.statusCodeHandler, new DefaultStatusCodeHandler(this.negotiator, this.environment), }; var engine = new NancyEngine(this.requestDispatcher, this.contextFactory, statusCodeHandlers, A.Fake<IRequestTracing>(), new DisabledStaticContentProvider(), this.negotiator, this.environment) { RequestPipelinesFactory = ctx => new Pipelines() }; var request = new Request("GET", "/", "http"); A.CallTo(() => this.statusCodeHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(true); A.CallTo(() => this.statusCodeHandler.Handle(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)) .Throws<Exception>(); // When await engine.HandleRequest(request); // Then this.context.Response.StatusCode.ShouldEqual(HttpStatusCode.InternalServerError); }
public void HandleRequest_should_return_response_from_route_prereq_if_one_returned() { var preResponse = new Response(); Func<NancyContext, Response> preHook = (ctx) => preResponse; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, null)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); var result = localEngine.HandleRequest(request); result.Response.ShouldBeSameAs(preResponse); }
public void HandleRequest_should_return_response_from_route_prereq_if_one_returned() { // Given var preResponse = new Response(); Func<NancyContext, Response> preHook = (ctx) => preResponse; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preHook, null)); var pipelines = new Pipelines(); var localEngine = new NancyEngine(prePostResolver, contextFactory, new IErrorHandler[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => pipelines }; var request = new Request("GET", "/", "http"); // When var result = localEngine.HandleRequest(request); // Then result.Response.ShouldBeSameAs(preResponse); }
public void Should_not_throw_exception_when_setting_nancy_version_header_and_it_already_existed() { // Given var cachedResponse = new Response(); cachedResponse.Headers.Add("Nancy-Version", "1.2.3.4"); Func<NancyContext, Response> preRequestHook = (ctx) => cachedResponse; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, preRequestHook, null)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory, this.errorHandler); var request = new Request("GET", "/", "http"); // When var exception = Record.Exception(() => localEngine.HandleRequest(request)); // Then exception.ShouldBeNull(); }
public void HandleRequest_route_prereq_returns_response_should_still_run_route_postreq_and_postreq() { var executionOrder = new List<String>(); Action<NancyContext> postHook = (ctx) => { executionOrder.Add("Posthook"); }; Func<NancyContext, Response> routePreHook = (ctx) => { executionOrder.Add("Routeprehook"); return new Response(); }; Action<NancyContext> routePostHook = (ctx) => { executionOrder.Add("Routeposthook"); }; this.route.Action = (d) => { executionOrder.Add("RouteInvoke"); return null; }; var prePostResolver = A.Fake<IRouteResolver>(); A.CallTo(() => prePostResolver.Resolve(A<NancyContext>.Ignored, A<IRouteCache>.Ignored.Argument)).Returns(new ResolveResult(route, DynamicDictionary.Empty, routePreHook, routePostHook)); var localEngine = new NancyEngine(prePostResolver, A.Fake<IRouteCache>(), contextFactory); localEngine.PostRequestHook = postHook; var request = new Request("GET", "/", "http"); localEngine.HandleRequest(request); executionOrder.Count().ShouldEqual(3); executionOrder.SequenceEqual(new[] { "Routeprehook", "Routeposthook", "Posthook" }).ShouldBeTrue(); }