public async Task TwoMiddlewareItemsInOrder() { bool called1 = false; bool called2 = false; CallMeMiddlware one = new CallMeMiddlware(() => { Assert.IsFalse(called2, "Second Middleware was called"); called1 = true; }); CallMeMiddlware two = new CallMeMiddlware(() => { Assert.IsTrue(called1, "First Middleware was not called"); called2 = true; }); Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use(one); m.Use(two); await m.ContextCreated(null); Assert.IsTrue(called1); Assert.IsTrue(called2); }
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 TwoAnonymousMiddlewareInOrder() { bool didRun1 = false; bool didRun2 = false; MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use(new AnonymousContextCreatedMiddleware(async(context, next) => { Assert.IsFalse(didRun2, "Looks like the 2nd one has already run"); didRun1 = true; await next(); })); m.Use(new AnonymousContextCreatedMiddleware(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); }
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 OneMiddlewareItem() { WasCalledMiddlware simple = new WasCalledMiddlware(); Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use(simple); Assert.IsFalse(simple.Called); await m.ContextCreated(null); Assert.IsTrue(simple.Called); }
public async Task TwoMiddlewareItems() { WasCalledMiddlware one = new WasCalledMiddlware(); WasCalledMiddlware two = new WasCalledMiddlware(); Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use(one); m.Use(two); await m.ContextCreated(null); Assert.IsTrue(one.Called); Assert.IsTrue(two.Called); }
public async Task AnonymousMiddleware() { bool didRun = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.OnContextCreated(async(context, next) => { didRun = true; await next(); }); Assert.IsFalse(didRun); await m.ContextCreated(null); Assert.IsTrue(didRun); }
public async Task NestedSet_AllIsRun() { bool innerOnSendCalled = false; bool innerOnReceiveCalled = false; bool innerOnCreatedCalled = false; string replyMessage = Guid.NewGuid().ToString(); MiddlewareSet inner = new MiddlewareSet(); inner.Use(new AnonymousSendActivityMiddleware(async(context, activities, next) => { Assert.IsTrue(activities.Count == 1, "incorrect activity count"); Assert.IsTrue(activities[0].AsMessageActivity().Text == replyMessage, "unexpected message"); innerOnSendCalled = true; await next(); })); inner.Use(new AnonymousReceiveMiddleware(async(context, next) => { context.Responses.Add(MessageFactory.Text(replyMessage)); innerOnReceiveCalled = true; await next(); })); inner.Use(new AnonymousContextCreatedMiddleware(async(context, next) => { innerOnCreatedCalled = true; await next(); })); Middleware.MiddlewareSet outer = new Middleware.MiddlewareSet(); outer.Use(inner); IBotContext c = TestUtilities.CreateEmptyContext(); await outer.ContextCreated(c); await outer.ReceiveActivity(c); await outer.SendActivity(c, c.Responses); Assert.IsTrue(innerOnReceiveCalled, "Inner Middleware Receive Activity was not called."); Assert.IsTrue(innerOnCreatedCalled, "Inner Middleware Create Context was not called."); Assert.IsTrue(innerOnSendCalled, "Inner Middleware SendActivity was not called."); }
public async Task TwoAnonymousMiddleware() { bool didRun1 = false; bool didRun2 = false; Middleware.MiddlewareSet m = new Middleware.MiddlewareSet(); m.Use(new AnonymousContextCreatedMiddleware(async(context, next) => { didRun1 = true; await next(); })); m.Use(new AnonymousContextCreatedMiddleware(async(context, next) => { didRun2 = true; await next(); })); await m.ContextCreated(null); Assert.IsTrue(didRun1); Assert.IsTrue(didRun2); }