if (apiBase == "http://down.example.com/api/")
throw new WebException();
+ if (apiBase == "http://error.example.com/api/")
+ return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
+
+ if (apiBase == "http://invalid.example.com/api/")
+ return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
+
return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
}
}
[Test]
public void HostFallbackTest()
{
- var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
+ var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
+ service.LoadRegex();
+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail1.example.com/api/"));
+
+ service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
+ service.LoadRegex();
+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+ service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
service.LoadRegex();
Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+
+ service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
+ service.LoadRegex();
+ Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+
+ service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
+ service.LoadRegex();
+ Assert.That(service.GetApiBase(), Is.Null);
+ }
+
+ [Test]
+ public void ServerOutageTest()
+ {
+ var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
+
+ service.LoadRegex();
+ Assert.That(service.GetApiBase(), Is.Null);
+
+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
+ Assert.That(thumbinfo, Is.Null);
}
[Test]
};
protected string ApiBase;
- protected IEnumerable<Regex> UrlRegex = new Regex[] {};
+ protected IEnumerable<Regex> UrlRegex = null;
protected Timer UpdateTimer;
private object LockObj = new object();
#endif
}
}
+
+ // どのサーバーも使用できない場合
+ lock (this.LockObj)
+ {
+ this.UrlRegex = null;
+ this.ApiBase = null;
+ }
}
public bool LoadRegex(string apiBase)
{
var xElm = XElement.Load(jsonReader);
+ if (xElm.Element("error") != null)
+ return false;
+
lock (this.LockObj)
{
this.UrlRegex = xElm.Elements("item")
return true;
}
- catch (WebException) { }
+ catch (WebException) { } // サーバーが2xx以外のステータスコードを返した場合
+ catch (XmlException) { } // サーバーが不正なJSONを返した場合
return false;
}
{
lock (this.LockObj)
{
+ if (this.UrlRegex == null)
+ return null;
+
foreach (var regex in this.UrlRegex)
{
if (regex.IsMatch(url))