Exemplo n.º 1
0
            public async Task ClonesHttpContent()
            {
                // Arrange
                stream = new MemoryStream(Encoding.UTF8.GetBytes("0123456789"));

                // Act
                var clone = httpContent.Clone(stream);

                // Assert
                var cloneContent = await clone.ReadAsStringAsync();

                Assert.Equal("0123456789", cloneContent);
            }
Exemplo n.º 2
0
            public void HttpContentIsNull_ReturnsNull()
            {
                // Arrange
                httpContent = null;

                // Act
                var clone = httpContent.Clone(stream);

                // Assert
                Assert.Null(clone);
            }
Exemplo n.º 3
0
        private async Task Notify(Publication publication, Subscription subscription, HttpContent content)
        {
            // must clone because sending each payload will cause the http client to
            // dispose it, leading to an exception on subsequent notifications
            var contentClone = await content.Clone();

            var request = new HttpRequestMessage(HttpMethod.Post, subscription.Callback)
            {
                Content = contentClone
            };

            request.Headers.Add("link", $"<{publication.HubLocation} />; rel=\"hub\", <{publication.Topic} />; rel=\"self\""); // required headers

            if (!string.IsNullOrEmpty(subscription.Secret))
            {
                var secret = _cryptoFunctions.Decrypt(subscription.Secret, subscription.Callback.ToString());

                /* Section 8 */
                var hash = _cryptoFunctions.GetHmacSha1Hash(
                    await contentClone.ReadAsByteArrayAsync(),
                    secret);

                request.Headers.Add("X-Hub-Signature", hash);
            }

            var response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                var message = Strings.Error_SubscriberPublishFailed;
                if (response.Content != null)
                {
                    message = $"{(int)response.StatusCode}:{await response.Content.ReadAsStringAsync()}";
                }

                var args = new PublishNotificationFailureEventArgs(subscription, message);
                OnNotifyFailed(args);
            }
        }