From d1565b44e4fa335bcf6f072c681cf101a922ff7c Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 30 Jun 2020 21:46:01 +0100 Subject: [PATCH] Update HttpClientFixture.cs --- .../Http/HttpClientFixture.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index d04c98de1..8fcc5f2bc 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Net; using System.Threading; using FluentAssertions; @@ -25,8 +26,57 @@ namespace NzbDrone.Common.Test.Http where TDispatcher : IHttpDispatcher { private static string[] _httpBinHosts = new[] { "eu.httpbin.org", "httpbin.org" }; + private int _httpBinSleep; private static int _httpBinRandom; private string _httpBinHost; + private string _httpBinHost2; + + [OneTimeSetUp] + public void FixtureSetUp() + { + var candidates = new[] { "httpbin.servarr.com", "eu.httpbin.org", /*"httpbin.org",*/ "www.httpbin.org" }; + + // httpbin.org is broken right now, occassionally redirecting to https if it's unavailable. + _httpBinHosts = candidates.Where(IsTestSiteAvailable).ToArray(); + + TestLogger.Info($"{candidates.Length} TestSites available."); + + _httpBinSleep = _httpBinHosts.Count() < 2 ? 100 : 10; + } + + private bool IsTestSiteAvailable(string site) + { + try + { + var req = WebRequest.Create($"http://{site}/get") as HttpWebRequest; + var res = req.GetResponse() as HttpWebResponse; + if (res.StatusCode != HttpStatusCode.OK) + { + return false; + } + + try + { + req = WebRequest.Create($"http://{site}/status/429") as HttpWebRequest; + res = req.GetResponse() as HttpWebResponse; + } + catch (WebException ex) + { + res = ex.Response as HttpWebResponse; + } + + if (res == null || res.StatusCode != (HttpStatusCode)429) + { + return false; + } + + return true; + } + catch + { + return false; + } + } [SetUp] public void SetUp()