public async Task CheckWebhookCall(EventHandlerConfig config, MessageData messageData, string expectedUri, string expectedContent)
        {
            var mockHttpHandler = new MockHttpMessageHandler();
            var mockWebHookRequestWithCallback = mockHttpHandler.When(HttpMethod.Post, expectedUri)
                                                 .WithContentType("application/json", expectedContent)
                                                 .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var httpClient = mockHttpHandler.ToHttpClient();

            var mockAuthHandler = new Mock <IAcquireTokenHandler>();
            var mockBigBrother  = new Mock <IBigBrother>();

            var mockHandlerFactory = new Mock <IEventHandlerFactory>();

            mockHandlerFactory.Setup(s => s.CreateWebhookHandler(config.CallbackConfig.Name)).Returns(
                new GenericWebhookHandler(
                    mockAuthHandler.Object,
                    new RequestBuilder(),
                    mockBigBrother.Object,
                    httpClient,
                    config.CallbackConfig));

            var webhookResponseHandler = new WebhookResponseHandler(
                mockHandlerFactory.Object,
                mockAuthHandler.Object,
                new RequestBuilder(),
                mockBigBrother.Object,
                httpClient,
                config);

            await webhookResponseHandler.Call(messageData);

            mockAuthHandler.Verify(e => e.GetToken(It.IsAny <HttpClient>()), Times.Exactly(1));
            Assert.Equal(1, mockHttpHandler.GetMatchCount(mockWebHookRequestWithCallback));
        }
        public async Task BadCheckMultiRouteSelection(EventHandlerConfig config, MessageData messageData, string expectedWebHookUri, string expectedContent)
        {
            var mockHttpHandler = new MockHttpMessageHandler();
            var multiRouteCall  = mockHttpHandler.When(HttpMethod.Post, expectedWebHookUri)
                                  .WithContentType("application/json; charset=utf-8", expectedContent)
                                  .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var httpClient = mockHttpHandler.ToHttpClient();

            var mockAuthHandler = new Mock <IAcquireTokenHandler>();
            var mockBigBrother  = new Mock <IBigBrother>();

            var mockHandlerFactory = new Mock <IEventHandlerFactory>();

            mockHandlerFactory.Setup(s => s.CreateWebhookHandler(config.CallbackConfig.Name)).Returns(
                new GenericWebhookHandler(
                    mockAuthHandler.Object,
                    new RequestBuilder(),
                    mockBigBrother.Object,
                    httpClient,
                    config.CallbackConfig));

            var webhookResponseHandler = new WebhookResponseHandler(
                mockHandlerFactory.Object,
                mockAuthHandler.Object,
                new RequestBuilder(),
                mockBigBrother.Object,
                httpClient,
                config);

            await Assert.ThrowsAsync <Exception>(async() => await webhookResponseHandler.Call(messageData));
        }
示例#3
0
        public async Task CheckCallbackCall(SubscriberConfiguration config, MessageData messageData, string expectedWebHookUri, string expectedCallbackUri, string expectedContent)
        {
            var mockHttpHandler = new MockHttpMessageHandler();

            mockHttpHandler.When(HttpMethod.Post, expectedWebHookUri)
            .WithContentType("application/json; charset=utf-8", expectedContent)
            .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var mockWebHookRequest = mockHttpHandler.When(HttpMethod.Put, expectedCallbackUri)
                                     .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var httpClients = new Dictionary <string, HttpClient>
            {
                { new Uri(config.Uri).Host, mockHttpHandler.ToHttpClient() },
                { new Uri(config.Callback.Uri).Host, mockHttpHandler.ToHttpClient() }
            };

            var mockAuthenticationTokenHandler = new Mock <IAuthenticationHandler>();

            mockAuthenticationTokenHandler.Setup(s => s.GetTokenAsync(It.IsAny <CancellationToken>())).ReturnsAsync(Guid.NewGuid().ToString);

            var mockAuthHandlerFactory = new Mock <IAuthenticationHandlerFactory>();

            mockAuthHandlerFactory.Setup(s => s.GetAsync(It.IsAny <WebhookConfig>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => mockAuthenticationTokenHandler.Object);

            var httpClientBuilder = new HttpClientFactory(httpClients);
            var mockBigBrother    = new Mock <IBigBrother>();

            var mockHandlerFactory = new Mock <IEventHandlerFactory>();

            mockHandlerFactory.Setup(s => s.CreateWebhookHandler(config.Callback.Name)).Returns(
                new GenericWebhookHandler(
                    httpClientBuilder,
                    new Mock <IAuthenticationHandlerFactory>().Object,
                    new RequestBuilder(),
                    new RequestLogger(mockBigBrother.Object),
                    mockBigBrother.Object,
                    config.Callback));

            var webhookResponseHandler = new WebhookResponseHandler(
                mockHandlerFactory.Object,
                httpClientBuilder,
                new RequestBuilder(),
                mockAuthHandlerFactory.Object,
                new RequestLogger(mockBigBrother.Object),
                mockBigBrother.Object,
                config);

            await webhookResponseHandler.CallAsync(messageData, new Dictionary <string, object>(), _cancellationToken);

            mockAuthHandlerFactory.Verify(e => e.GetAsync(It.IsAny <WebhookConfig>(), _cancellationToken), Times.Once);
            mockHandlerFactory.Verify(e => e.CreateWebhookHandler(It.IsAny <string>()), Times.AtMostOnce);

            Assert.Equal(1, mockHttpHandler.GetMatchCount(mockWebHookRequest));
        }
示例#4
0
        public async Task GoodCheckMultiRouteSelection(SubscriberConfiguration config, MessageData messageData, string expectedWebHookUri, string expectedContent)
        {
            var mockHttpHandler = new MockHttpMessageHandler();
            var multiRouteCall  = mockHttpHandler.When(HttpMethod.Post, expectedWebHookUri)
                                  .WithContentType("application/json; charset=utf-8", expectedContent)
                                  .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var httpClients = new Dictionary <string, HttpClient>
            {
                { new Uri(config.Callback.Uri).Host, mockHttpHandler.ToHttpClient() }
            };

            //for each route in the path query, we create a mock http client in the factory
            foreach (var rules in config.WebhookRequestRules.Where(r => r.Routes.Any()))
            {
                foreach (var route in rules.Routes)
                {
                    httpClients.Add(new Uri(route.Uri).Host, mockHttpHandler.ToHttpClient());
                }
            }

            var mockAuthHandlerFactory = new Mock <IAuthenticationHandlerFactory>();
            var httpClientBuilder      = new HttpClientFactory(httpClients);
            var mockBigBrother         = new Mock <IBigBrother>();

            var mockHandlerFactory = new Mock <IEventHandlerFactory>();

            mockHandlerFactory.Setup(s => s.CreateWebhookHandler(config.Callback.Name)).Returns(
                new GenericWebhookHandler(
                    httpClientBuilder,
                    new Mock <IAuthenticationHandlerFactory>().Object,
                    new RequestBuilder(),
                    new RequestLogger(mockBigBrother.Object),
                    mockBigBrother.Object,
                    config.Callback));

            var webhookResponseHandler = new WebhookResponseHandler(
                mockHandlerFactory.Object,
                httpClientBuilder,
                new RequestBuilder(),
                new Mock <IAuthenticationHandlerFactory>().Object,
                new RequestLogger(mockBigBrother.Object),
                mockBigBrother.Object,
                config);

            await webhookResponseHandler.CallAsync(messageData, new Dictionary <string, object>(), _cancellationToken);

            mockAuthHandlerFactory.Verify(e => e.GetAsync(It.IsAny <WebhookConfig>(), _cancellationToken), Times.AtMostOnce);
            mockHandlerFactory.Verify(e => e.CreateWebhookHandler(It.IsAny <string>()), Times.AtMostOnce);

            Assert.Equal(1, mockHttpHandler.GetMatchCount(multiRouteCall));
        }
示例#5
0
        public async Task BadCheckMultiRouteSelection(SubscriberConfiguration config, MessageData messageData, string expectedWebHookUri, string expectedContent)
        {
            var mockHttpHandler = new MockHttpMessageHandler();

            mockHttpHandler.When(HttpMethod.Post, expectedWebHookUri)
            .WithContentType("application/json; charset=utf-8", expectedContent)
            .Respond(HttpStatusCode.OK, "application/json", "{\"msg\":\"Hello World\"}");

            var httpClients = new Dictionary <string, HttpClient>
            {
                { new Uri(config.Uri).Host, mockHttpHandler.ToHttpClient() },
                { new Uri(config.Callback.Uri).Host, mockHttpHandler.ToHttpClient() }
            };

            var httpClientBuilder = new HttpClientFactory(httpClients);
            var mockBigBrother    = new Mock <IBigBrother>();

            var mockHandlerFactory = new Mock <IEventHandlerFactory>();

            mockHandlerFactory.Setup(s => s.CreateWebhookHandler(config.Callback.Name)).Returns(
                new GenericWebhookHandler(
                    httpClientBuilder,
                    new Mock <IAuthenticationHandlerFactory>().Object,
                    new RequestBuilder(),
                    new RequestLogger(mockBigBrother.Object),
                    mockBigBrother.Object,
                    config.Callback));

            var webhookResponseHandler = new WebhookResponseHandler(
                mockHandlerFactory.Object,
                httpClientBuilder,
                new RequestBuilder(),
                new Mock <IAuthenticationHandlerFactory>().Object,
                new RequestLogger(mockBigBrother.Object),
                mockBigBrother.Object,
                config);

            await Assert.ThrowsAsync <Exception>(async() => await webhookResponseHandler.CallAsync(messageData, new Dictionary <string, object>(), _cancellationToken));
        }