OSDN Git Service

903e03521752b3e16a76670ed0091d4bb4dbdebf
[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.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Text;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using Moq;
32 using OpenTween.Models;
33 using Xunit;
34 using Xunit.Extensions;
35
36 namespace OpenTween.Thumbnail.Services
37 {
38     public class ImgAzyobuziNetTest
39     {
40         class TestImgAzyobuziNet : ImgAzyobuziNet
41         {
42             public TestImgAzyobuziNet()
43                 : this(new[] { "http://img.azyobuzi.net/api/" })
44             {
45             }
46
47             public TestImgAzyobuziNet(string[] apiHosts)
48                 : base(null, autoupdate: false)
49             {
50                 this.ApiHosts = apiHosts;
51                 this.LoadRegexAsync().Wait();
52             }
53
54             public string GetApiBase()
55                 => this.ApiBase;
56
57             protected override Task<byte[]> FetchRegexAsync(string apiBase)
58             {
59                 return Task.Run(() =>
60                 {
61                     if (apiBase == "http://down.example.com/api/")
62                         throw new HttpRequestException();
63
64                     if (apiBase == "http://error.example.com/api/")
65                         return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
66
67                     if (apiBase == "http://invalid.example.com/api/")
68                         return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
69
70                     return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
71                 });
72             }
73         }
74
75         [Fact]
76         public async Task HostFallbackTest()
77         {
78             var service = new TestImgAzyobuziNet(new[] { "http://avail1.example.com/api/", "http://avail2.example.com/api/" });
79             await service.LoadRegexAsync();
80             Assert.Equal("http://avail1.example.com/api/", service.GetApiBase());
81
82             service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
83             await service.LoadRegexAsync();
84             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
85
86             service = new TestImgAzyobuziNet(new[] { "http://error.example.com/api/", "http://avail.example.com/api/" });
87             await service.LoadRegexAsync();
88             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
89
90             service = new TestImgAzyobuziNet(new[] { "http://invalid.example.com/api/", "http://avail.example.com/api/" });
91             await service.LoadRegexAsync();
92             Assert.Equal("http://avail.example.com/api/", service.GetApiBase());
93
94             service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
95             await service.LoadRegexAsync();
96             Assert.Null(service.GetApiBase());
97         }
98
99         [Fact]
100         public async Task ServerOutageTest()
101         {
102             var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
103
104             await service.LoadRegexAsync();
105             Assert.Null(service.GetApiBase());
106
107             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
108             Assert.Null(thumbinfo);
109         }
110
111         [Fact]
112         public async Task MatchTest()
113         {
114             var service = new TestImgAzyobuziNet();
115             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
116
117             Assert.NotNull(thumbinfo);
118             Assert.Equal("http://example.com/abcd", thumbinfo.MediaPageUrl);
119             Assert.Equal("http://img.azyobuzi.net/api/redirect?size=large&uri=http%3A%2F%2Fexample.com%2Fabcd", thumbinfo.ThumbnailImageUrl);
120             Assert.Null(thumbinfo.TooltipText);
121         }
122
123         [Fact]
124         public async Task NotMatchTest()
125         {
126             var service = new TestImgAzyobuziNet();
127             var thumbinfo = await service.GetThumbnailInfoAsync("http://hogehoge.com/abcd", null, CancellationToken.None);
128
129             Assert.Null(thumbinfo);
130         }
131
132         [Fact]
133         public async Task DisabledInDM_Test()
134         {
135             var service = new TestImgAzyobuziNet();
136             service.DisabledInDM = true;
137
138             var post = new PostClass
139             {
140                 TextFromApi = "http://example.com/abcd",
141                 IsDm = true,
142             };
143
144             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", post, CancellationToken.None);
145
146             Assert.Null(thumbinfo);
147         }
148
149         [Fact]
150         public async Task Enabled_FalseTest()
151         {
152             var service = new TestImgAzyobuziNet();
153             service.Enabled = false;
154
155             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
156
157             Assert.Null(thumbinfo);
158         }
159     }
160 }