public NancyEngineFixture() { this.resolver = A.Fake<IRouteResolver>(); this.response = new Response(); this.route = new FakeRoute(response); this.context = new NancyContext(); this.errorHandler = A.Fake<IErrorHandler>(); this.requestDispatcher = A.Fake<IRequestDispatcher>(); A.CallTo(() => this.requestDispatcher.Dispatch(A<NancyContext>._)).Invokes(x => this.context.Response = new Response()); A.CallTo(() => errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(false); contextFactory = A.Fake<INancyContextFactory>(); A.CallTo(() => contextFactory.Create()).Returns(context); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, null, null)); var applicationPipelines = new Pipelines(); this.routeInvoker = A.Fake<IRouteInvoker>(); A.CallTo(() => this.routeInvoker.Invoke(A<Route>._, A<DynamicDictionary>._, A<NancyContext>._)).ReturnsLazily(arg => { return (Response)((Route)arg.Arguments[0]).Action.Invoke((DynamicDictionary)arg.Arguments[1]); }); this.engine = new NancyEngine(this.requestDispatcher, contextFactory, new[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => applicationPipelines }; }
public NancyEngineFixture() { this.resolver = A.Fake<IRouteResolver>(); this.response = new Response(); this.route = new FakeRoute(response); this.context = new NancyContext(); this.statusCodeHandler = A.Fake<IStatusCodeHandler>(); this.requestDispatcher = A.Fake<IRequestDispatcher>(); this.diagnosticsConfiguration = new DiagnosticsConfiguration(); A.CallTo(() => this.requestDispatcher.Dispatch(A<NancyContext>._, A<CancellationToken>._)).Invokes((x) => this.context.Response = new Response()); A.CallTo(() => this.statusCodeHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(false); contextFactory = A.Fake<INancyContextFactory>(); A.CallTo(() => contextFactory.Create(A<Request>._)).Returns(context); var resolveResult = new ResolveResult { Route = route, Parameters = DynamicDictionary.Empty, Before = null, After = null, OnError = null }; A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolveResult); var applicationPipelines = new Pipelines(); this.routeInvoker = A.Fake<IRouteInvoker>(); A.CallTo(() => this.routeInvoker.Invoke(A<Route>._, A<CancellationToken>._, A<DynamicDictionary>._, A<NancyContext>._)).ReturnsLazily(arg => { return ((Route)arg.Arguments[0]).Action.Invoke((DynamicDictionary)arg.Arguments[1], A<CancellationToken>._).Result; }); this.engine = new NancyEngine(this.requestDispatcher, this.contextFactory, new[] { this.statusCodeHandler }, A.Fake<IRequestTracing>(), this.diagnosticsConfiguration, new DisabledStaticContentProvider()) { RequestPipelinesFactory = ctx => applicationPipelines }; }
public void Should_create_default_error_hook_when_created_with_default_ctor() { // Given, When var pipelines = new Pipelines(); // Then pipelines.OnError.ShouldNotBeNull(); }
public void Should_create_default_before_request_hook_when_created_with_default_ctor() { // Given, When var pipelines = new Pipelines(); // Then pipelines.BeforeRequest.ShouldNotBeNull(); }
public void Should_clone_error_hooks_when_created_with_existing_pipeline() { // Given Func<NancyContext, Exception, Response> hook = (ctx, ex) => null; var existing = new Pipelines(); existing.OnError.AddItemToEndOfPipeline(hook); // When var pipelines = new Pipelines(existing); // Then pipelines.OnError.PipelineItems.First().Delegate.ShouldBeSameAs(hook); }
public void Should_clone_before_request_hooks_when_created_with_existing_pipeline() { // Given Func<NancyContext, Response> hook = ctx => null; var existing = new Pipelines(); existing.BeforeRequest.AddItemToEndOfPipeline(hook); // When var pipelines = new Pipelines(existing); // Then pipelines.BeforeRequest.PipelineItems.ShouldHaveCount(1); }
public void Should_clone_after_request_hooks_when_created_with_existing_pipeline() { // Given Action<NancyContext> hook = ctx => ctx.Items.Add("foo", 1); var existing = new Pipelines(); existing.AfterRequest.AddItemToEndOfPipeline(hook); // When var pipelines = new Pipelines(existing); // Then pipelines.AfterRequest.PipelineItems.ShouldHaveCount(1); }
/// <summary> /// Creates and initializes the request pipelines. /// </summary> /// <param name="context">The <see cref="NancyContext"/> used by the request.</param> /// <returns>An <see cref="IPipelines"/> instance.</returns> protected virtual IPipelines InitializeRequestPipelines(NancyContext context) { var requestPipelines = new Pipelines(this.ApplicationPipelines); if (this.RequestStartupTaskTypeCache.Any()) { var startupTasks = this.RegisterAndGetRequestStartupTasks(this.ApplicationContainer, this.RequestStartupTaskTypeCache); foreach (var requestStartup in startupTasks) { requestStartup.Initialize(requestPipelines, context); } } this.RequestStartup(this.ApplicationContainer, requestPipelines, context); return(requestPipelines); }
public void Post_request_hook_should_not_return_a_challenge_when_set_to_never() { // Given var config = new BasicAuthenticationConfiguration(A.Fake<IUserValidator>(), "realm", UserPromptBehaviour.Never); var hooks = new Pipelines(); BasicAuthentication.Enable(hooks, config); var context = new NancyContext() { Request = new FakeRequest("GET", "/") }; context.Response = new Response { StatusCode = HttpStatusCode.Unauthorized }; // When hooks.AfterRequest.Invoke(context); // Then context.Response.Headers.ContainsKey("WWW-Authenticate").ShouldBeFalse(); }
public void HandleRequest_prereq_returns_response_should_still_run_postreq() { // Given var returnedResponse = A.Fake<Response>(); var postReqCalled = false; var pipelines = new Pipelines(); pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx => returnedResponse); pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => postReqCalled = true); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When this.engine.HandleRequest(request); // Then postReqCalled.ShouldBeTrue(); }
/// <summary> /// Creates and initializes the request pipelines. /// </summary> /// <param name="context">The <see cref="NancyContext"/> used by the request.</param> /// <returns>An <see cref="IPipelines"/> instance.</returns> protected override sealed IPipelines InitializeRequestPipelines(NancyContext context) { var requestContainer = this.GetConfiguredRequestContainer(context); var requestPipelines = new Pipelines(this.ApplicationPipelines); if (this.RequestStartupTaskTypeCache.Any()) { var startupTasks = this.RegisterAndGetRequestStartupTasks(requestContainer, this.RequestStartupTaskTypeCache); foreach (var requestStartup in startupTasks) { requestStartup.Initialize(requestPipelines, context); } } this.RequestStartup(requestContainer, requestPipelines, context); return(requestPipelines); }
public void Post_request_hook_should_not_return_a_challenge_on_an_ajax_request_when_set_to_nonajax() { // Given var config = new BasicAuthenticationConfiguration(A.Fake<IUserValidator>(), "realm", UserPromptBehaviour.NonAjax); var hooks = new Pipelines(); BasicAuthentication.Enable(hooks, config); var headers = new Dictionary<string,IEnumerable<string>>(); headers.Add(ajaxRequestHeaderKey, new [] { ajaxRequestHeaderValue }); var context = new NancyContext() { Request = new FakeRequest("GET", "/", headers) }; context.Response = new Response { StatusCode = HttpStatusCode.Unauthorized }; // When hooks.AfterRequest.Invoke(context); // Then context.Response.Headers.ContainsKey("WWW-Authenticate").ShouldBeFalse(); }
public NancyEngineFixture() { this.environment = new DefaultNancyEnvironment(); this.environment.Tracing( enabled: true, displayErrorTraces: true); this.resolver = A.Fake<IRouteResolver>(); this.response = new Response(); this.route = new FakeRoute(response); this.context = new NancyContext(); this.statusCodeHandler = A.Fake<IStatusCodeHandler>(); this.requestDispatcher = A.Fake<IRequestDispatcher>(); this.negotiator = A.Fake<IResponseNegotiator>(); A.CallTo(() => this.requestDispatcher.Dispatch(A<NancyContext>._, A<CancellationToken>._)) .Returns(Task.FromResult(new Response())); A.CallTo(() => this.statusCodeHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(false); contextFactory = A.Fake<INancyContextFactory>(); A.CallTo(() => contextFactory.Create(A<Request>._)).Returns(context); var resolveResult = new ResolveResult { Route = route, Parameters = DynamicDictionary.Empty, Before = null, After = null, OnError = null }; A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolveResult); var applicationPipelines = new Pipelines(); this.routeInvoker = A.Fake<IRouteInvoker>(); this.engine = new NancyEngine(this.requestDispatcher, this.contextFactory, new[] { this.statusCodeHandler }, A.Fake<IRequestTracing>(), new DisabledStaticContentProvider(), this.negotiator, this.environment) { RequestPipelinesFactory = ctx => applicationPipelines }; }
public NancyEngineFixture() { this.resolver = A.Fake<IRouteResolver>(); this.response = new Response(); this.route = new FakeRoute(response); this.context = new NancyContext(); this.errorHandler = A.Fake<IErrorHandler>(); A.CallTo(() => errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(false); contextFactory = A.Fake<INancyContextFactory>(); A.CallTo(() => contextFactory.Create()).Returns(context); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(route, DynamicDictionary.Empty, null, null)); var applicationPipelines = new Pipelines(); this.engine = new NancyEngine(resolver, contextFactory, new[] { this.errorHandler }, A.Fake<IRequestTracing>()) { RequestPipelinesFactory = ctx => applicationPipelines }; }
public void Should_persist_original_exception_in_requestexecutionexception_when_pipeline_is_null() { // Given var expectedException = new Exception(); var resolvedRoute = new ResolveResult( new FakeRoute(), DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolvedRoute); A.CallTo(() => this.requestDispatcher.Dispatch(context, A<CancellationToken>._)) .Returns(TaskHelpers.GetFaultedTask<Response>(expectedException)); var pipelines = new Pipelines { OnError = null }; engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); var returnedException = result.Items["ERROR_EXCEPTION"] as RequestExecutionException; // Then returnedException.InnerException.ShouldBeSameAs(expectedException); }
public void Should_not_throw_exception_when_handlerequest_is_invoked_and_pre_request_hook_is_null() { // Given var pipelines = new Pipelines { BeforeRequest = null }; engine.RequestPipelinesFactory = (ctx) => pipelines; // When var request = new Request("GET", "/", "http"); // Then this.engine.HandleRequest(request); }
public void Should_persist_and_unwrap_multiple_nested_original_exception_in_requestexecutionexception_with_exceptions_on_multiple_levels() { // Given var expectedException1 = new Exception(); var expectedException2 = new Exception(); var expectedException3 = new Exception(); var expectedException4 = new Exception(); var expectgedInnerExceptions = 4; var exceptionsListInner = new List<Exception>() { expectedException1, expectedException2, expectedException3 }; var expectedExceptionInner = new AggregateException(exceptionsListInner); var exceptionsListOuter = new List<Exception>() { expectedExceptionInner, expectedException4 }; var aggregateExceptionOuter = new AggregateException(exceptionsListOuter); var resolvedRoute = new ResolveResult( new FakeRoute(), DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolvedRoute); A.CallTo(() => this.requestDispatcher.Dispatch(context, A<CancellationToken>._)) .Returns(TaskHelpers.GetFaultedTask<Response>(aggregateExceptionOuter)); var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline((ctx, exception) => null); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); var returnedException = result.Items["ERROR_EXCEPTION"] as RequestExecutionException; // Then var returnedInnerException = returnedException.InnerException as AggregateException; returnedInnerException.ShouldBeOfType(typeof(AggregateException)); Assert.Equal(expectgedInnerExceptions, returnedInnerException.InnerExceptions.Count); }
public void Should_call_pre_request_hook_should_be_invoked_with_request_from_context() { // Given Request passedRequest = null; var pipelines = new Pipelines(); pipelines.BeforeRequest.AddItemToStartOfPipeline((ctx) => { passedRequest = ctx.Request; return null; }); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); this.context.Request = request; // When this.engine.HandleRequest(request); // Then passedRequest.ShouldBeSameAs(request); }
public void Should_invoke_the_error_request_hook_if_one_exists_when_dispatcher_throws() { // Given var testEx = new Exception(); var errorRoute = new Route("GET", "/", null, (x,c) => { throw testEx; }); var resolvedRoute = new ResolveResult( errorRoute, DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolvedRoute); A.CallTo(() => this.requestDispatcher.Dispatch(context, A<CancellationToken>._)) .Returns(TaskHelpers.GetFaultedTask<Response>(testEx)); Exception handledException = null; NancyContext handledContext = null; var errorResponse = new Response(); A.CallTo(() => this.negotiator.NegotiateResponse(A<object>.Ignored, A<NancyContext>.Ignored)) .Returns(errorResponse); Func<NancyContext, Exception, dynamic> routeErrorHook = (ctx, ex) => { handledContext = ctx; handledException = ex; return errorResponse; }; var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline(routeErrorHook); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then Assert.Equal(testEx, handledException); Assert.Equal(result, handledContext); Assert.Equal(result.Response, errorResponse); }
public void Should_set_user_in_context_with_valid_username_in_auth_header() { // Given var fakePipelines = new Pipelines(); var validator = A.Fake<IUserValidator>(); var fakeUser = A.Fake<IUserIdentity>(); A.CallTo(() => validator.Validate("foo", "bar")).Returns(fakeUser); var cfg = new BasicAuthenticationConfiguration(validator, "realm"); var context = CreateContextWithHeader( "Authorization", new [] { "Basic" + " " + EncodeCredentials("foo", "bar") }); BasicAuthentication.Enable(fakePipelines, cfg); // When fakePipelines.BeforeRequest.Invoke(context, new CancellationToken()); // Then context.CurrentUser.ShouldBeSameAs(fakeUser); }
public void Should_allow_post_request_hook_to_replace_response() { // Given var newResponse = new Response(); var pipelines = new Pipelines(); pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx => ctx.Response = newResponse); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Response.ShouldBeSameAs(newResponse); }
public void Should_add_requestexecutionexception_to_context_when_pipeline_is_null() { // Given var resolvedRoute = new ResolveResult( new FakeRoute(), DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolvedRoute); A.CallTo(() => this.requestDispatcher.Dispatch(context, A<CancellationToken>._)) .Returns(TaskHelpers.GetFaultedTask<Response>(new Exception())); var pipelines = new Pipelines { OnError = null }; engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Items.Keys.Contains("ERROR_EXCEPTION").ShouldBeTrue(); result.Items["ERROR_EXCEPTION"].ShouldBeOfType<RequestExecutionException>(); }
public void Should_add_unhandled_exception_to_context_as_requestexecutionexception() { // Given var routeUnderTest = new Route("GET", "/", null, (x,c) => { throw new Exception(); }); var resolved = new ResolveResult(routeUnderTest, DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolved); A.CallTo(() => this.routeInvoker.Invoke(A<Route>._, A<CancellationToken>._, A<DynamicDictionary>._, A<NancyContext>._)) .Invokes((x) => routeUnderTest.Action.Invoke(DynamicDictionary.Empty, new CancellationToken())); A.CallTo(() => this.requestDispatcher.Dispatch(context, A<CancellationToken>._)) .Returns(TaskHelpers.GetFaultedTask<Response>(new Exception())); var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline((ctx, exception) => null); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Items.Keys.Contains("ERROR_EXCEPTION").ShouldBeTrue(); result.Items["ERROR_EXCEPTION"].ShouldBeOfType<RequestExecutionException>(); }
public void Should_persist_original_exception_in_requestexecutionexception_when_pipeline_is_null() { // Given var expectedException = new Exception(); var routeUnderTest = new Route("GET", "/", null, x => { throw expectedException; }); var resolved = new ResolveResult(routeUnderTest, DynamicDictionary.Empty, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolved); var pipelines = new Pipelines { OnError = null }; engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); var returnedException = result.Items["ERROR_EXCEPTION"] as RequestExecutionException; // Then returnedException.InnerException.ShouldBeSameAs(expectedException); }
public async Task Should_not_throw_exception_when_handlerequest_is_invoked_and_post_request_hook_is_null() { // Given var pipelines = new Pipelines { AfterRequest = null }; engine.RequestPipelinesFactory = (ctx) => pipelines; // When var request = new Request("GET", "/", "http"); // Then await this.engine.HandleRequest(request); }
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_invoke_the_error_request_hook_if_one_exists_when_route_throws() { // Given var testEx = new Exception(); var errorRoute = new Route("GET", "/", null, x => { throw testEx; }); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(new ResolveResult(errorRoute, DynamicDictionary.Empty, null, null)); Exception handledException = null; NancyContext handledContext = null; var errorResponse = new Response(); Func<NancyContext, Exception, Response> routeErrorHook = (ctx, ex) => { handledContext = ctx; handledException = ex; return errorResponse; }; var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline(routeErrorHook); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then Assert.Equal(testEx, handledException); Assert.Equal(result, handledContext); Assert.Equal(result.Response, errorResponse); }
public void Should_add_unhandled_exception_to_context_as_requestexecutionexception() { // Given var routeUnderTest = new Route("GET", "/", null, x => { throw new Exception(); }); var resolved = new ResolveResult(routeUnderTest, DynamicDictionary.Empty, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolved); var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline((ctx, exception) => null); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Items.Keys.Contains("ERROR_EXCEPTION").ShouldBeTrue(); result.Items["ERROR_EXCEPTION"].ShouldBeOfType<RequestExecutionException>(); }
public void Should_return_response_from_pre_request_hook_when_not_null() { // Given var returnedResponse = A.Fake<Response>(); var pipelines = new Pipelines(); pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx => returnedResponse); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Response.ShouldBeSameAs(returnedResponse); }
public void Should_allow_post_request_hook_to_modify_context_items() { // Given var pipelines = new Pipelines(); pipelines.BeforeRequest.AddItemToStartOfPipeline(ctx => { ctx.Items.Add("PostReqTest", new object()); return null; }); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); // Then result.Items.ContainsKey("PostReqTest").ShouldBeTrue(); }
public void Should_persist_original_exception_in_requestexecutionexception() { // Given var expectedException = new Exception(); var resolvedRoute = new ResolveResult( new FakeRoute(), DynamicDictionary.Empty, null, null, null); A.CallTo(() => resolver.Resolve(A<NancyContext>.Ignored)).Returns(resolvedRoute); A.CallTo(() => this.requestDispatcher.Dispatch(context)).Throws(expectedException); var pipelines = new Pipelines(); pipelines.OnError.AddItemToStartOfPipeline((ctx, exception) => null); engine.RequestPipelinesFactory = (ctx) => pipelines; var request = new Request("GET", "/", "http"); // When var result = this.engine.HandleRequest(request); var returnedException = result.Items["ERROR_EXCEPTION"] as RequestExecutionException; // Then returnedException.InnerException.ShouldBeSameAs(expectedException); }
public void Should_set_user_in_context_with_valid_username_in_auth_header() { // Given var fakePipelines = new Pipelines(); var context = CreateContextWithHeader( "Authorization", new[] { "Token" + " " + "mytoken" }); var tokenizer = A.Fake<ITokenizer>(); var fakeUser = A.Fake<IUserIdentity>(); A.CallTo(() => tokenizer.Detokenize("mytoken", context)).Returns(fakeUser); var cfg = new TokenAuthenticationConfiguration(tokenizer); TokenAuthentication.Enable(fakePipelines, cfg); // When fakePipelines.BeforeRequest.Invoke(context, new CancellationToken()); // Then context.CurrentUser.ShouldBeSameAs(fakeUser); }