public async Task ChecksHttpGetVerb(WebhookConfig config, HttpMethod httpMethod, string payload, HttpStatusCode expectedResponseCode, string expectedResponseBody)
        {
            var mockHttp = new MockHttpMessageHandler();
            var request  = mockHttp.When(httpMethod, config.Uri)
                           .Respond(expectedResponseCode, "application/json", expectedResponseBody);

            var mockBigBrother = new Mock <IBigBrother>();
            var httpClients    = new Dictionary <string, HttpClient> {
                { new Uri(config.Uri).Host, mockHttp.ToHttpClient() }
            };

            var httpClientBuilder = new HttpClientFactory(httpClients);
            var requestBuilder    = new RequestBuilder();
            var requestLogger     = new RequestLogger(mockBigBrother.Object);

            var genericWebhookHandler = new GenericWebhookHandler(
                httpClientBuilder,
                new Mock <IAuthenticationHandlerFactory>().Object,
                requestBuilder,
                requestLogger,
                mockBigBrother.Object,
                config);

            await genericWebhookHandler.CallAsync(new MessageData(payload, "TestType", "subA", "service") { CorrelationId = Guid.NewGuid().ToString() }, new Dictionary <string, object>(), _cancellationToken);

            Assert.Equal(1, mockHttp.GetMatchCount(request));
        }
        public async Task ExecuteHappyPathRawContract()
        {
            var(messageData, metaData) = EventHandlerTestHelper.CreateMessageDataPayload();

            var config = new WebhookConfig
            {
                Uri                  = "http://localhost/webhook",
                HttpMethod           = HttpMethod.Put,
                EventType            = "Event1",
                AuthenticationConfig = new AuthenticationConfig(),
                WebhookRequestRules  = new List <WebhookRequestRule>
                {
                    new WebhookRequestRule
                    {
                        Source = new ParserLocation
                        {
                            Path = "OrderCode"
                        },
                        Destination = new ParserLocation
                        {
                            Location = Location.Uri
                        }
                    }
                }
            };

            var mockHttp       = new MockHttpMessageHandler();
            var webhookRequest = mockHttp.When(HttpMethod.Put, $"{config.Uri}/{metaData["OrderCode"]}")
                                 .WithContentType("application/json", messageData.Payload)
                                 .Respond(HttpStatusCode.OK, "application/json", string.Empty);

            var mockBigBrother = new Mock <IBigBrother>();
            var httpClients    = new Dictionary <string, HttpClient> {
                { new Uri(config.Uri).Host, mockHttp.ToHttpClient() }
            };

            var httpClientBuilder = new HttpClientFactory(httpClients);
            var requestBuilder    = new RequestBuilder();
            var requestLogger     = new RequestLogger(mockBigBrother.Object);

            var genericWebhookHandler = new GenericWebhookHandler(
                httpClientBuilder,
                new Mock <IAuthenticationHandlerFactory>().Object,
                requestBuilder,
                requestLogger,
                mockBigBrother.Object,
                config);

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

            Assert.Equal(1, mockHttp.GetMatchCount(webhookRequest));
        }
Exemplo n.º 3
0
        public async Task ChecksHttpGetVerb(WebhookConfig config, HttpMethod httpMethod, string payload, HttpStatusCode expectedResponseCode, string expectedResponseBody)
        {
            var mockHttp = new MockHttpMessageHandler();
            var request  = mockHttp.When(httpMethod, config.Uri)
                           .Respond(expectedResponseCode, "application/json", expectedResponseBody);

            var genericWebhookHandler = new GenericWebhookHandler(
                new Mock <IAcquireTokenHandler>().Object,
                new RequestBuilder(),
                new Mock <IBigBrother>().Object,
                mockHttp.ToHttpClient(),
                config);

            await genericWebhookHandler.Call(new MessageData { Payload = payload });

            Assert.Equal(1, mockHttp.GetMatchCount(request));
        }
        public async Task ExecuteHappyPath()
        {
            var(messageData, metaData) = EventHandlerTestHelper.CreateMessageDataPayload();

            var config = new WebhookConfig
            {
                Uri                  = "http://localhost/webhook",
                HttpVerb             = HttpVerb.Put,
                AuthenticationConfig = new AuthenticationConfig(),
                WebhookRequestRules  = new List <WebhookRequestRule>
                {
                    new WebhookRequestRule
                    {
                        Source = new ParserLocation
                        {
                            Path = "OrderCode"
                        },
                        Destination = new ParserLocation
                        {
                            Location = Location.Uri
                        }
                    }
                }
            };

            var mockHttp       = new MockHttpMessageHandler();
            var webhookRequest = mockHttp.When(HttpMethod.Put, $"{config.Uri}/{metaData["OrderCode"]}")
                                 .WithContentType("application/json", messageData.Payload)
                                 .Respond(HttpStatusCode.OK, "application/json", string.Empty);

            var genericWebhookHandler = new GenericWebhookHandler(
                new Mock <IAcquireTokenHandler>().Object,
                new RequestBuilder(),
                new Mock <IBigBrother>().Object,
                mockHttp.ToHttpClient(),
                config);

            await genericWebhookHandler.Call(messageData);

            Assert.Equal(1, mockHttp.GetMatchCount(webhookRequest));
        }
        public async Task ExecuteDeliveryFailurePath()
        {
            var(messageData, metaData) = EventHandlerTestHelper.CreateMessageDataPayload();

            messageData.IsDlq = true;

            var config = new WebhookConfig
            {
                Uri                   = "http://localhost/webhook",
                HttpMethod            = HttpMethod.Put,
                EventType             = "Event1",
                PayloadTransformation = PayloadContractTypeEnum.WrapperContract,
                AuthenticationConfig  = new AuthenticationConfig(),
                WebhookRequestRules   = new List <WebhookRequestRule>
                {
                    new WebhookRequestRule
                    {
                        Source = new ParserLocation
                        {
                            Path = "OrderCode"
                        },
                        Destination = new ParserLocation
                        {
                            Location = Location.Uri
                        }
                    }
                }
            };

            var mockHttp = new MockHttpMessageHandler();

            var webhookRequest = mockHttp.Expect(HttpMethod.Put, $"{config.Uri}/{metaData["OrderCode"]}")
                                 .With((m) =>
            {
                var str = m.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                using (var sr = new StringReader(str))
                {
                    using (var jr = new JsonTextReader(sr))
                    {
                        var wrapperObj = JsonSerializer.CreateDefault().Deserialize <WrapperPayloadContract>(jr);
                        return(JToken.DeepEquals(wrapperObj.Payload, JObject.Parse(messageData.Payload)) && wrapperObj.CallbackType == CallbackTypeEnum.DeliveryFailure);
                    }
                }
            })
                                 .Respond(HttpStatusCode.OK, "application/json", string.Empty);

            var mockBigBrother = new Mock <IBigBrother>();
            var httpClients    = new Dictionary <string, HttpClient> {
                { new Uri(config.Uri).Host, mockHttp.ToHttpClient() }
            };

            var httpClientBuilder = new HttpClientFactory(httpClients);
            var requestBuilder    = new RequestBuilder();
            var requestLogger     = new RequestLogger(mockBigBrother.Object);

            var genericWebhookHandler = new GenericWebhookHandler(
                httpClientBuilder,
                new Mock <IAuthenticationHandlerFactory>().Object,
                requestBuilder,
                requestLogger,
                mockBigBrother.Object,
                config);

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

            Assert.Equal(1, mockHttp.GetMatchCount(webhookRequest));
        }