public async Task CatchAnExceptionViaMiddlware() { Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); bool caughtException = false; m.OnContextCreated(async(context, next) => { try { await next(); Assert.Fail("Should not get here"); } catch (Exception ex) { Assert.IsTrue(ex.Message == "test"); caughtException = true; } }); m.OnContextCreated(async(context, next) => { throw new Exception("test"); }); await m.ContextCreated(null); Assert.IsTrue(caughtException); }
public async Task RunCodeBeforeAndAfter() { bool didRun1 = false; bool codeafter2run = false; bool didRun2 = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.OnContextCreated(async(context, next) => { Assert.IsFalse(didRun1, "Looks like the 1st middleware has already run"); didRun1 = true; await next(); Assert.IsTrue(didRun1, "The 2nd middleware should have run now."); codeafter2run = true; }); m.OnContextCreated(async(context, next) => { Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run"); Assert.IsFalse(codeafter2run, "The code that runs after middleware 2 is complete has already run."); didRun2 = true; await next(); }); await m.ContextCreated(null); Assert.IsTrue(didRun1); Assert.IsTrue(didRun2); Assert.IsTrue(codeafter2run); }
public async Task MixedMiddlewareInOrderAnonymousLast() { bool didRun1 = false; bool didRun2 = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use( new CallMeMiddlware(() => { Assert.IsFalse(didRun2, "Second Middleware was called"); didRun1 = true; })); m.OnContextCreated(async(context, next) => { Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run"); didRun2 = true; await next(); }); await m.ContextCreated(null); Assert.IsTrue(didRun1); Assert.IsTrue(didRun2); }
public async Task BubbleUncaughtException() { Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.OnContextCreated(async(context, next) => { throw new InvalidOperationException("test"); }); await m.ContextCreated(null); Assert.Fail("Should never have gotten here"); }
public async Task TwoAnonymousMiddleware() { bool didRun1 = false; bool didRun2 = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.OnContextCreated(async(context, next) => { didRun1 = true; await next(); }); m.OnContextCreated(async(context, next) => { didRun2 = true; await next(); }); await m.ContextCreated(null); Assert.IsTrue(didRun1); Assert.IsTrue(didRun2); }
public async Task TwoAnonymousMiddlewareInOrder() { bool didRun1 = false; bool didRun2 = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.OnContextCreated(async(context, next) => { Assert.IsFalse(didRun2, "Looks like the 2nd one has already run"); didRun1 = true; await next(); }); m.OnContextCreated(async(context, next) => { Assert.IsTrue(didRun1, "Looks like the 1nd one has not yet run"); didRun2 = true; await next(); }); await m.ContextCreated(null); Assert.IsTrue(didRun1); Assert.IsTrue(didRun2); }