OSDN Git Service

テストフレームワークを Xunit.NET に移行
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / ImgAzyobuziNetTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Net;
26 using System.Text;
27 using Xunit;
28 using Xunit.Extensions;
29
30 namespace OpenTween.Thumbnail.Services
31 {
32     public class ImgAzyobuziNetTest
33     {
34         class TestImgAzyobuziNet : ImgAzyobuziNet
35         {
36             public TestImgAzyobuziNet()
37                 : this(new[] { "http://img.azyobuzi.net/api/" })
38             {
39             }
40
41             public TestImgAzyobuziNet(string[] apiHosts)
42                 : base(autoupdate: false)
43             {
44                 this.ApiHosts = apiHosts;
45                 this.LoadRegex();
46             }
47
48             public string GetApiBase()
49             {
50                 return this.ApiBase;
51             }
52
53             protected override byte[] FetchRegex(string apiBase)
54             {
55                 if (apiBase == "http://down.example.com/api/")
56                     throw new WebException();
57
58                 if (apiBase == "http://error.example.com/api/")
59                     return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
60
61                 if (apiBase == "http://invalid.example.com/api/")
62                     return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
63
64                 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
65             }
66         }
67
68         [Fact]
69         public void HostFallbackTest()
70         {
71             var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
72             service.LoadRegex();
73             Assert.Equal("http://avail1.example.com/api/", service.GetApiBase());
74
75             service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
76             service.LoadRegex();
77             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
78
79             service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
80             service.LoadRegex();
81             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
82
83             service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
84             service.LoadRegex();
85             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
86
87             service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
88             service.LoadRegex();
89             Assert.Null(service.GetApiBase());
90         }
91
92         [Fact]
93         public void ServerOutageTest()
94         {
95             var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
96
97             service.LoadRegex();
98             Assert.Null(service.GetApiBase());
99
100             var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
101             Assert.Null(thumbinfo);
102         }
103
104         [Fact]
105         public void MatchTest()
106         {
107             var service = new TestImgAzyobuziNet();
108             var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
109
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);
114         }
115
116         [Fact]
117         public void NotMatchTest()
118         {
119             var service = new TestImgAzyobuziNet();
120             var thumbinfo = service.GetThumbnailInfo("http://hogehoge.com/abcd", null);
121
122             Assert.Null(thumbinfo);
123         }
124     }
125 }