1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
28 using Xunit.Extensions;
30 namespace OpenTween.Thumbnail.Services
32 public class ImgAzyobuziNetTest
34 class TestImgAzyobuziNet : ImgAzyobuziNet
36 public TestImgAzyobuziNet()
37 : this(new[] { "http://img.azyobuzi.net/api/" })
41 public TestImgAzyobuziNet(string[] apiHosts)
42 : base(autoupdate: false)
44 this.ApiHosts = apiHosts;
48 public string GetApiBase()
53 protected override byte[] FetchRegex(string apiBase)
55 if (apiBase == "http://down.example.com/api/")
56 throw new WebException();
58 if (apiBase == "http://error.example.com/api/")
59 return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
61 if (apiBase == "http://invalid.example.com/api/")
62 return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
64 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
69 public void HostFallbackTest()
71 var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
73 Assert.Equal("http://avail1.example.com/api/", service.GetApiBase());
75 service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
77 Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
79 service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
81 Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
83 service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
85 Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
87 service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
89 Assert.Null(service.GetApiBase());
93 public void ServerOutageTest()
95 var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
98 Assert.Null(service.GetApiBase());
100 var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
101 Assert.Null(thumbinfo);
105 public void MatchTest()
107 var service = new TestImgAzyobuziNet();
108 var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
110 Assert.NotNull(thumbinfo);
111 Assert.Equal("http://example.com/abcd", thumbinfo.ImageUrl);
112 Assert.Equal("http://img.azyobuzi.net/api/redirect?size=large&uri=http%3A%2F%2Fexample.com%2Fabcd", thumbinfo.ThumbnailUrl);
113 Assert.Null(thumbinfo.TooltipText);
117 public void NotMatchTest()
119 var service = new TestImgAzyobuziNet();
120 var thumbinfo = service.GetThumbnailInfo("http://hogehoge.com/abcd", null);
122 Assert.Null(thumbinfo);