Esempio n. 1
0
        public async Task LaunchASimpleRequestWithQarantainValuesMustSuccess()
        {
            FakeHTTPHandler handler = new FakeHTTPHandler();

            handler.ReturnMessage        = "responseSuccess";
            handler.ReturnStatusCodeList = new List <HttpStatusCode>()
            {
                HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError, HttpStatusCode.InternalServerError, HttpStatusCode.Accepted
            };

            IDnsServiceExtractor extract = new DnsServiceExtractorFirstLabelConvention(ProtocolType.Tcp);

            FakeDnsSrvQuerier querier = new FakeDnsSrvQuerier();
            var dnsHandler            = new DnsServiceBalancingMessageHandler(extract.FromUri(new Uri("https://api.qarnot.com")), new DnsServiceTargetSelectorReal(querier, new DnsSrvSortResult(), 10), new TargetQuarantinePolicyServerUnavailable(), null);

            // add the fake handle
            dnsHandler.InnerHandler = handler;

            // wrapper used to send the chosen request
            using HandlerWrapper handlerWrapper = new HandlerWrapper();
            handlerWrapper.InnerHandler         = dnsHandler;

            // create the request
            using var requestMessage = new HttpRequestMessage(new HttpMethod("Get"), "https://hello.world.com");

            // get the result
            var result = await handlerWrapper.Send(requestMessage, default(CancellationToken));

            var content = await result.Content.ReadAsStringAsync();

            Assert.AreEqual("responseSuccess", content);
            Assert.AreEqual(HttpStatusCode.Accepted, result.StatusCode);
            Assert.AreEqual(querier.DnsSrvResultEntryList[3].HostName, handler.UrlCall.Host);
        }
Esempio n. 2
0
        public async Task LaunchErrorUseTheOriginalUriIfNoDnsServerWork()
        {
            var             logger  = CreateLoggers.CreateILoggerFromNLog();
            FakeHTTPHandler handler = new FakeHTTPHandler();

            handler.ReturnMessage        = "responseSuccess";
            handler.ReturnStatusCodeList = new List <HttpStatusCode>()
            {
                HttpStatusCode.InternalServerError
            };

            IDnsServiceExtractor extract = new DnsServiceExtractorFirstLabelConvention(ProtocolType.Tcp);

            FakeDnsSrvQuerier querier = new FakeDnsSrvQuerier();
            var dnsHandler            = new DnsServiceBalancingMessageHandler(extract.FromUri(new Uri("https://api.qarnot.com")), new DnsServiceTargetSelectorReal(querier, new DnsSrvSortResult(), 10, logger), new TargetQuarantinePolicyServerUnavailable(new TimeSpan(0, 0, 10)), logger);

            // add the fake handle
            dnsHandler.InnerHandler = handler;

            // wrapper used to send the chosen request
            using HandlerWrapper handlerWrapper = new HandlerWrapper();
            handlerWrapper.InnerHandler         = dnsHandler;

            // create the request
            using var requestMessage1 = new HttpRequestMessage(new HttpMethod("Get"), "https://hello.world.com");

            // get the result
            var result = await handlerWrapper.Send(requestMessage1, default(CancellationToken));

            var content = await result.Content.ReadAsStringAsync();

            Assert.AreEqual(result.StatusCode, HttpStatusCode.InternalServerError);
            Assert.AreEqual("hello.world.com", handler.UrlCall.Host);
            handler.ReturnStatusCodeList = new List <HttpStatusCode>()
            {
                HttpStatusCode.Accepted
            };
            using var requestMessage2 = new HttpRequestMessage(new HttpMethod("Get"), "https://hello.world.com");

            result = await handlerWrapper.Send(requestMessage2, default(CancellationToken));

            content = await result.Content.ReadAsStringAsync();

            Assert.AreEqual("responseSuccess", content);
            Assert.AreEqual(result.StatusCode, HttpStatusCode.Accepted);
            Assert.AreEqual("hello.world.com", handler.UrlCall.Host);
        }
Esempio n. 3
0
        public HandlerWrapper WrapDnsHandler(DelegatingHandler dnsHandler, string successResponse)
        {
            if (dnsHandler == null)
            {
                throw new ArgumentNullException(nameof(dnsHandler));
            }

            FakeHTTPHandler handler = new FakeHTTPHandler();

            handler.ReturnMessage = successResponse;

            // add the fake handle
            dnsHandler.InnerHandler = handler;

            // wrapper used to send the chosen request
            HandlerWrapper handlerWrapper = new HandlerWrapper();

            handlerWrapper.InnerHandler = dnsHandler;
            return(handlerWrapper);
        }