microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Apps.Tests/Events/ErrorEventTests.cs
138lines · modecode
| 1 | using System.Net; |
| 2 | using System.Text; |
| 3 | |
| 4 | using Microsoft.Teams.Api.Activities; |
| 5 | using Microsoft.Teams.Api.Auth; |
| 6 | using Microsoft.Teams.Apps.Activities; |
| 7 | using Microsoft.Teams.Apps.Events; |
| 8 | using Microsoft.Teams.Apps.Testing.Plugins; |
| 9 | using Microsoft.Teams.Common.Http; |
| 10 | |
| 11 | namespace Microsoft.Teams.Apps.Tests.Events; |
| 12 | |
| 13 | public class ErrorEventTests |
| 14 | { |
| 15 | private readonly App _app; |
| 16 | private readonly TestPlugin _plugin; |
| 17 | private readonly IToken _token; |
| 18 | |
| 19 | private class CustomException(string message) : Exception(message) |
| 20 | { |
| 21 | |
| 22 | } |
| 23 | |
| 24 | public ErrorEventTests() |
| 25 | { |
| 26 | _app = new App(); |
| 27 | _plugin = new TestPlugin(); |
| 28 | _app.AddPlugin(_plugin); |
| 29 | _token = Globals.Token; |
| 30 | } |
| 31 | |
| 32 | [Fact] |
| 33 | public async Task Should_CallHandler_OnErrorEvent() |
| 34 | { |
| 35 | var calls = 0; |
| 36 | |
| 37 | _app.OnEvent(EventType.Error, (sender, @event) => |
| 38 | { |
| 39 | calls++; |
| 40 | Assert.True(@event is ErrorEvent error && error.Exception is CustomException custom && custom.Message == "testing123"); |
| 41 | }); |
| 42 | |
| 43 | _app.OnError((sender, @event) => |
| 44 | { |
| 45 | calls++; |
| 46 | Assert.True(@event is not null); |
| 47 | Assert.True(@event.Exception is CustomException custom && custom.Message == "testing123"); |
| 48 | }); |
| 49 | |
| 50 | _app.OnActivity((_, @event) => |
| 51 | { |
| 52 | throw new CustomException("testing123"); |
| 53 | }); |
| 54 | |
| 55 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 56 | |
| 57 | Assert.Equal(HttpStatusCode.InternalServerError, res.Status); |
| 58 | Assert.Equal(2, calls); |
| 59 | } |
| 60 | |
| 61 | [Fact] |
| 62 | public async Task Should_NotCallHandler_OnErrorEvent() |
| 63 | { |
| 64 | var calls = 0; |
| 65 | |
| 66 | _app.OnEvent(EventType.Error, (sender, @event) => calls++); |
| 67 | _app.OnError((sender, @event) => calls++); |
| 68 | _app.OnActivity((_, @event) => |
| 69 | { |
| 70 | Assert.True(@event.Activity is MessageActivity message && message.Text == "hello world"); |
| 71 | }); |
| 72 | |
| 73 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 74 | |
| 75 | Assert.Equal(HttpStatusCode.OK, res.Status); |
| 76 | Assert.Equal(0, calls); |
| 77 | } |
| 78 | |
| 79 | [Fact] |
| 80 | public async Task Should_CallHandler_OnErrorEvent_On_HttpException() |
| 81 | { |
| 82 | var calls = 0; |
| 83 | var json = """{ "message": "some error content" }"""; |
| 84 | |
| 85 | _app.OnEvent(EventType.Error, (sender, @event) => |
| 86 | { |
| 87 | calls++; |
| 88 | |
| 89 | if (@event is not ErrorEvent error) |
| 90 | { |
| 91 | Assert.Fail(); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (error.Exception is not HttpException ex) |
| 96 | { |
| 97 | Assert.Fail($"received unexpected type {error.Exception.GetType()}"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode); |
| 102 | }); |
| 103 | |
| 104 | _app.OnError((sender, @event) => |
| 105 | { |
| 106 | calls++; |
| 107 | |
| 108 | if (@event.Exception is not HttpException ex) |
| 109 | { |
| 110 | Assert.Fail($"received unexpected type {@event.Exception.GetType()}"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode); |
| 115 | }); |
| 116 | |
| 117 | _app.OnMessage(context => |
| 118 | { |
| 119 | var res = new HttpResponseMessage |
| 120 | { |
| 121 | StatusCode = HttpStatusCode.BadRequest, |
| 122 | Content = new StringContent(json, Encoding.UTF8, "application/json"), |
| 123 | }; |
| 124 | |
| 125 | throw new HttpException() |
| 126 | { |
| 127 | Headers = res.Headers, |
| 128 | StatusCode = res.StatusCode, |
| 129 | Body = res.Content |
| 130 | }; |
| 131 | }); |
| 132 | |
| 133 | var res = await _plugin.Do(_token, new MessageActivity("hello world")); |
| 134 | |
| 135 | Assert.Equal(HttpStatusCode.InternalServerError, res.Status); |
| 136 | Assert.Equal(2, calls); |
| 137 | } |
| 138 | } |