Provides an implementation of IWebHookManager for managing notifications and mapping them to registered WebHooks.
Inheritance: IWebHookManager, IDisposable
        public void CreateWebHookRequest_CreatesExpectedRequest()
        {
            // Arrange
            WebHookWorkItem workItem = CreateWorkItem();

            workItem.WebHook.Headers.Add("Content-Language", "da");

            // Act
            HttpRequestMessage actual = WebHookManager.CreateWebHookRequest(workItem, _loggerMock.Object);

            // Assert
            Assert.Equal(HttpMethod.Post, actual.Method);
            Assert.Equal(workItem.WebHook.WebHookUri, actual.RequestUri.AbsoluteUri);

            IEnumerable <string> headers;

            actual.Headers.TryGetValues("h1", out headers);
            Assert.Equal("hv1", headers.Single());

            actual.Headers.TryGetValues("ms-signature", out headers);
            Assert.Equal(WebHookSignature, headers.Single());

            Assert.Equal("da", actual.Content.Headers.ContentLanguage.Single());
            Assert.Equal("application/json; charset=utf-8", actual.Content.Headers.ContentType.ToString());
        }
        public async Task NotifyAsync_StopsOnLastLastFailureOrFirstSuccessAndFirstGone(TimeSpan[] delays, Func <HttpRequestMessage, int, Task <HttpResponseMessage> > handler, int expected)
        {
            // Arrange
            ManualResetEvent done = new ManualResetEvent(initialState: false);
            WebHookWorkItem  success = null, failure = null;

            _manager = new WebHookManager(_storeMock.Object, _loggerMock.Object, delays, _options, _httpClient, onWebHookSuccess: item =>
            {
                success = item;
                done.Set();
            }, onWebHookFailure: item =>
            {
                failure = item;
                done.Set();
            });
            _handlerMock.Handler = handler;
            NotificationDictionary notification = new NotificationDictionary("a1", data: null);

            // Act
            int actual = await _manager.NotifyAsync(TestUser, new[] { notification });

            done.WaitOne();

            // Assert
            _storeMock.Verify();
            if (expected >= 0)
            {
                Assert.Equal(expected, success.Offset);
            }
            else
            {
                Assert.Equal(Math.Abs(expected), failure.Offset);
            }
        }
        public void Dispose_Succeeds()
        {
            // Arrange
            WebHookManager m = new WebHookManager(_storeMock.Object, _loggerMock.Object);

            // Act
            m.Dispose();
            m.Dispose();
        }
        public void CreateWebHookRequestBody_CreatesExpectedBody()
        {
            // Arrange
            WebHookWorkItem workItem = CreateWorkItem();

            // Act
            JObject actual = WebHookManager.CreateWebHookRequestBody(workItem);

            // Assert
            Assert.Equal(SerializedWebHook, actual.ToString());
        }
        public void GetWorkItems_FilterSingleNotification(IEnumerable <WebHook> webHooks, NotificationDictionary notification)
        {
            // Act
            IEnumerable <WebHookWorkItem> actual = WebHookManager.GetWorkItems(webHooks.ToArray(), new[] { notification });

            // Assert
            Assert.Equal(webHooks.Count(), actual.Count());
            foreach (WebHookWorkItem workItem in actual)
            {
                Assert.Same(workItem.Notifications.Single(), notification);
            }
        }
        public async Task VerifyWebHookAsync_Throws_IEmptySuccessResponse()
        {
            // Arrange
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The WebHook URI did not return the expected echo query parameter value in a plain text response body. This is necessary to ensure that the WebHook is connected correctly.", ex.Message);
        }
        public async Task VerifyWebHookAsync_Throws_IfHttpClientThrows()
        {
            // Arrange
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => { throw new Exception("Catch this!"); };
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("WebHook verification failed. Please ensure that the WebHook URI is valid and that the endpoint is accessible. Error encountered: Catch this!", ex.Message);
        }
        public async Task VerifyWebHookAsync_Throws_IfNotHttpOrHttpsUri(string webHookUri)
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            WebHook webHook = CreateWebHook();

            webHook.WebHookUri = webHookUri != null ? new Uri(webHookUri) : null;

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal(string.Format(CultureInfo.InvariantCulture, "The WebHook URI must be absolute with a scheme of either 'http' or 'https' but received '{0}'.", webHook.WebHookUri), ex.Message);
        }
        public async Task VerifyWebHookAsync_Throws_IfInvalidWebHookSecret(string secret)
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            WebHook webHook = CreateWebHook();

            webHook.Secret = secret;

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The WebHook secret key parameter must be between 32 and 64 characters long.", ex.Message);
        }
        public async Task VerifyWebHookAsync_Throws_INotSuccessResponse()
        {
            // Arrange
            _response.StatusCode = HttpStatusCode.NotFound;
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("WebHook verification failed. Please ensure that the WebHook URI is valid and that the endpoint is accessible. Error encountered: NotFound", ex.Message);
        }
        public async Task VerifyWebHookAsync_Throws_IfEchoDoesNotMatch()
        {
            // Arrange
            _response.Content    = new StringContent("Hello World");
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The HTTP request echo query parameter was not returned as plain text in the response. Please return the echo parameter to verify that the WebHook is working as expected.", ex.Message);
        }
        public async Task VerifyWebHookAsync_Succeeds_EchoResponse()
        {
            // Arrange
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) =>
            {
                NameValueCollection query = req.RequestUri.ParseQueryString();
                _response.Content = new StringContent(query["echo"]);
                return(Task.FromResult(_response));
            };
            WebHook webHook = CreateWebHook();

            // Act
            await _manager.VerifyWebHookAsync(webHook);
        }
        public void GetWorkItems_FilterMultipleNotifications(IEnumerable <WebHook> webHooks, IEnumerable <NotificationDictionary> notifications, int expected)
        {
            // Act
            IEnumerable <WebHookWorkItem> actual = WebHookManager.GetWorkItems(webHooks.ToArray(), notifications.ToArray());

            // Assert
            Assert.Equal(expected, actual.Count());
            foreach (WebHookWorkItem workItem in actual)
            {
                foreach (NotificationDictionary notification in workItem.Notifications)
                {
                    Assert.True(workItem.WebHook.MatchesAction(notification.Action));
                }
            }
        }
        public async Task VerifyWebHookAsync_Stops_IfNoEchoParameter(string query)
        {
            // Arrange
            bool error = false;

            _response.Content    = new StringContent("Hello World");
            _manager             = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) =>
            {
                error = true;
                return(Task.FromResult(_response));
            };
            WebHook webHook = CreateWebHook();

            webHook.WebHookUri = new Uri("http://localhost/hook?" + query);

            // Act
            await _manager.VerifyWebHookAsync(webHook);

            // Assert
            Assert.False(error);
        }
        public async Task SignWebHookRequest_SignsBodyCorrectly()
        {
            // Arrange
            WebHookWorkItem    workItem = CreateWorkItem();
            HttpRequestMessage request  = new HttpRequestMessage();
            JObject            body     = WebHookManager.CreateWebHookRequestBody(workItem);

            // Act
            WebHookManager.SignWebHookRequest(workItem.WebHook, request, body);

            // Assert
            IEnumerable <string> signature;

            request.Headers.TryGetValues("ms-signature", out signature);
            Assert.Equal(WebHookSignature, signature.Single());

            string requestBody = await request.Content.ReadAsStringAsync();

            Assert.Equal(SerializedWebHook, requestBody);

            Assert.Equal("application/json; charset=utf-8", request.Content.Headers.ContentType.ToString());
        }
示例#16
0
        public async Task VerifyWebHookAsync_Throws_IfEchoDoesNotMatch()
        {
            // Arrange
            _response.Content = new StringContent("Hello World");
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The HTTP request echo query parameter was not returned as plain text in the response. Please return the echo parameter to verify that the WebHook is working as expected.", ex.Message);
        }
示例#17
0
        public async Task VerifyWebHookAsync_Throws_INotSuccessResponse()
        {
            // Arrange
            _response.StatusCode = HttpStatusCode.NotFound;
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("WebHook verification failed. Please ensure that the WebHook URI is valid and that the endpoint is accessible. Error encountered: NotFound", ex.Message);
        }
示例#18
0
        public async Task VerifyWebHookAsync_Throws_IEmptySuccessResponse()
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => Task.FromResult(_response);
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The WebHook URI did not return the expected echo query parameter value in a plain text response body. This is necessary to ensure that the WebHook is connected correctly.", ex.Message);
        }
示例#19
0
        public async Task VerifyWebHookAsync_Throws_IfHttpClientThrows()
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => { throw new Exception("Catch this!"); };
            WebHook webHook = CreateWebHook();

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("WebHook verification failed. Please ensure that the WebHook URI is valid and that the endpoint is accessible. Error encountered: Catch this!", ex.Message);
        }
示例#20
0
        public void Dispose_Succeeds()
        {
            // Arrange
            WebHookManager m = new WebHookManager(_storeMock.Object, _loggerMock.Object);

            // Act
            m.Dispose();
            m.Dispose();
        }
示例#21
0
        public async Task VerifyWebHookAsync_Throws_IfInvalidWebHookSecret(string secret)
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            WebHook webHook = CreateWebHook();
            webHook.Secret = secret;

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal("The WebHook secret key parameter must be between 32 and 64 characters long.", ex.Message);
        }
示例#22
0
        public async Task NotifyAsync_StopsOnLastLastFailureOrFirstSuccessAndFirstGone(TimeSpan[] delays, Func<HttpRequestMessage, int, Task<HttpResponseMessage>> handler, int expected)
        {
            // Arrange
            ManualResetEvent done = new ManualResetEvent(initialState: false);
            WebHookWorkItem success = null, failure = null;
            _manager = new WebHookManager(_storeMock.Object, _loggerMock.Object, delays, _options, _httpClient, onWebHookSuccess: item =>
            {
                success = item;
                done.Set();
            }, onWebHookFailure: item =>
            {
                failure = item;
                done.Set();
            });
            _handlerMock.Handler = handler;
            NotificationDictionary notification = new NotificationDictionary("a1", data: null);

            // Act
            int actual = await _manager.NotifyAsync(TestUser, new[] { notification });
            done.WaitOne();

            // Assert
            _storeMock.Verify();
            if (expected >= 0)
            {
                Assert.Equal(expected, success.Offset);
            }
            else
            {
                Assert.Equal(Math.Abs(expected), failure.Offset);
            }
        }
示例#23
0
        public async Task VerifyWebHookAsync_Succeeds_EchoResponse()
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) =>
            {
                NameValueCollection query = req.RequestUri.ParseQueryString();
                _response.Content = new StringContent(query["echo"]);
                return Task.FromResult(_response);
            };
            WebHook webHook = CreateWebHook();

            // Act
            await _manager.VerifyWebHookAsync(webHook);
        }
示例#24
0
        public async Task VerifyWebHookAsync_Throws_IfNotHttpOrHttpsUri(string webHookUri)
        {
            // Arrange
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            WebHook webHook = CreateWebHook();
            webHook.WebHookUri = new Uri(webHookUri);

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => _manager.VerifyWebHookAsync(webHook));

            // Assert
            Assert.Equal(string.Format(CultureInfo.InvariantCulture, "The WebHook URI must be absolute with a scheme of either 'http' or 'https' but received '{0}'.", webHook.WebHookUri), ex.Message);
        }
示例#25
0
        public async Task VerifyWebHookAsync_Stops_IfNoEchoParameter(string query)
        {
            // Arrange
            bool error = false;
            _response.Content = new StringContent("Hello World");
            _manager = new WebHookManager(_storeMock.Object, _senderMock.Object, _loggerMock.Object, _httpClient);
            _handlerMock.Handler = (req, counter) => 
            {
                error = true;
                return Task.FromResult(_response);
            };
            WebHook webHook = CreateWebHook();
            webHook.WebHookUri = new Uri("http://localhost/hook?" + query);

            // Act
            await _manager.VerifyWebHookAsync(webHook);

            // Assert
            Assert.False(error);
        }