Example #1
0
        public async Task NormalScenarioAsync(Dictionary <string, object> payload)
        {
            var logger           = new Mock <ILogger <DefaultWebHookSender> >();
            var signature        = "FIXED_SIGNATURE";
            var signatureService = new Mock <ISignatureService>();

            signatureService.Setup(s => s.GetSignature(It.IsAny <string>(), It.IsAny <string>())).Returns(signature);

            var webHook = new WebHook {
                Callback = new Uri("http://www.example.com")
            };
            var notif = new WebHookNotification {
                TriggerId = "noun.verb", Payload = payload
            };

            var callbackHasBeenCalled = false;
            var httpClient            = HttpClientMocker.Callback(async m =>
            {
                callbackHasBeenCalled = true;
                Assert.Equal(HttpMethod.Post, m.Method);
                Assert.Equal(webHook.Callback, m.RequestUri);

                var content = JsonConvert.DeserializeObject <Dictionary <string, string> >(await m.Content.ReadAsStringAsync());
                Assert.NotNull(content);
                Assert.Contains(DefaultWebHookSender.TriggerKey, content.Keys);
                Assert.Equal(notif.TriggerId, content[DefaultWebHookSender.TriggerKey]);
                Assert.Contains(DefaultWebHookSender.TimestampKey, content.Keys);
                Assert.Contains(DefaultWebHookSender.UniqueIdKey, content.Keys);

                if (notif.Payload != null)
                {
                    foreach (var kvp in notif.Payload)
                    {
                        Assert.Contains(kvp.Key, content.Keys);
                        Assert.Equal(kvp.Value, content[kvp.Key]);
                    }
                }

                Assert.Contains(DefaultWebHookSender.SignatureHeader, m.Headers.Select(kvp => kvp.Key));
                Assert.Equal(signature, m.Headers.GetValues(DefaultWebHookSender.SignatureHeader).First());
            });

            var service = new CounterDefaultWebHookSender(httpClient, signatureService.Object, logger.Object);
            await service.SendAsync(new WebHookWorkItem(notif, webHook), CancellationToken.None);

            Assert.True(callbackHasBeenCalled);
            Assert.Equal(1, service.Successes);
        }
Example #2
0
        public async Task ErrorScenarioAsync()
        {
            var logger    = new Mock <ILogger <DefaultWebHookSender> >();
            var signature = new Mock <ISignatureService>();

            var webHook = new WebHook {
                Callback = new Uri("http://www.example.com")
            };
            var notif = new WebHookNotification {
                TriggerId = "noun.verb"
            };

            var httpClient = HttpClientMocker.AlwaysFail(new Exception());

            var service = new CounterDefaultWebHookSender(httpClient, signature.Object, logger.Object);
            await service.SendAsync(new WebHookWorkItem(notif, webHook), CancellationToken.None);

            Assert.Equal(1, service.Failures);
        }
Example #3
0
        public async Task NotFoundScenarioAsync(System.Net.HttpStatusCode code)
        {
            var logger    = new Mock <ILogger <DefaultWebHookSender> >();
            var signature = new Mock <ISignatureService>();

            var webHook = new WebHook {
                Callback = new Uri("http://www.example.com")
            };
            var notif = new WebHookNotification {
                TriggerId = "noun.verb"
            };

            var httpClient = HttpClientMocker.Static(code, "");

            var service = new CounterDefaultWebHookSender(httpClient, signature.Object, logger.Object);
            await service.SendAsync(new WebHookWorkItem(notif, webHook), CancellationToken.None);

            Assert.Equal(1, service.NotFounds);
        }