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;
25 using System.Net.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
29 using System.Threading;
30 using System.Threading.Tasks;
32 using OpenTween.Models;
34 using Xunit.Extensions;
36 namespace OpenTween.Thumbnail.Services
38 public class ImgAzyobuziNetTest
40 class TestImgAzyobuziNet : ImgAzyobuziNet
42 public TestImgAzyobuziNet()
43 : this(new[] { "http://img.azyobuzi.net/api/" })
47 public TestImgAzyobuziNet(string[] apiHosts)
48 : base(null, autoupdate: false)
50 this.ApiHosts = apiHosts;
51 this.LoadRegexAsync().Wait();
54 public string GetApiBase()
57 protected override Task<byte[]> FetchRegexAsync(string apiBase)
61 if (apiBase == "http://down.example.com/api/")
62 throw new HttpRequestException();
64 if (apiBase == "http://error.example.com/api/")
65 return Encoding.UTF8.GetBytes("{\"error\": {\"code\": 5001}}");
67 if (apiBase == "http://invalid.example.com/api/")
68 return Encoding.UTF8.GetBytes("<<<INVALID JSON>>>");
70 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
76 public async Task HostFallbackTest()
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());
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());
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());
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());
94 service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
95 await service.LoadRegexAsync();
96 Assert.Null(service.GetApiBase());
100 public async Task ServerOutageTest()
102 var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/" });
104 await service.LoadRegexAsync();
105 Assert.Null(service.GetApiBase());
107 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
108 Assert.Null(thumbinfo);
112 public async Task MatchTest()
114 var service = new TestImgAzyobuziNet();
115 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
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);
124 public async Task NotMatchTest()
126 var service = new TestImgAzyobuziNet();
127 var thumbinfo = await service.GetThumbnailInfoAsync("http://hogehoge.com/abcd", null, CancellationToken.None);
129 Assert.Null(thumbinfo);
133 public async Task DisabledInDM_Test()
135 var service = new TestImgAzyobuziNet();
136 service.DisabledInDM = true;
138 var post = new PostClass
140 TextFromApi = "http://example.com/abcd",
144 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", post, CancellationToken.None);
146 Assert.Null(thumbinfo);
150 public async Task Enabled_FalseTest()
152 var service = new TestImgAzyobuziNet();
153 service.Enabled = false;
155 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", null, CancellationToken.None);
157 Assert.Null(thumbinfo);