OSDN Git Service

OpenTween.Thumbnail.Services 内のOTWebClientを使用している箇所をHttpClientに置き換え
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / TinamiTest.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.Reflection;
26 using System.Runtime.InteropServices;
27 using System.Text;
28 using System.Text.RegularExpressions;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Xml.Linq;
32 using NSubstitute;
33 using Xunit;
34 using Xunit.Extensions;
35
36 namespace OpenTween.Thumbnail.Services
37 {
38     public class TinamiTest
39     {
40         class TestTinami : Tinami
41         {
42             public string FakeXml { get; set; }
43
44             public TestTinami(string pattern, string replacement)
45                 : base(pattern, replacement)
46             {
47             }
48
49             protected override Task<XDocument> FetchContentInfoApiAsync(string url, CancellationToken token)
50             {
51                 Assert.True(Regex.IsMatch(url, @"http://api\.tinami\.com/content/info\?cont_id=.+&api_key=.+"));
52
53                 return Task.FromResult(XDocument.Parse(this.FakeXml));
54             }
55         }
56
57         public TinamiTest()
58         {
59             this.MyCommonSetup();
60         }
61
62         public void MyCommonSetup()
63         {
64             var mockAssembly = Substitute.For<_Assembly>();
65             mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
66             MyCommon.EntryAssembly = mockAssembly;
67
68             MyCommon.fileVersion = "1.0.0.0";
69         }
70
71         [Fact]
72         public async Task ApiTest()
73         {
74             var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
75                 "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
76
77             service.FakeXml = @"<?xml version='1.0' encoding='utf-8' ?>
78 <rsp stat='ok'>
79   <content type='illust' issupport='1' iscollection='0'>
80     <title>ほげほげ</title>
81     <description>説明</description>
82     <thumbnails>
83       <thumbnail_150x150 url='http://img.tinami.com/hogehoge_150.gif' width='112' height='120'/>
84     </thumbnails>
85     <image>
86       <url>http://img.tinami.com/hogehoge_full.gif</url>
87       <width>640</width>
88       <height>480</height>
89     </image>
90   </content>
91 </rsp>";
92             var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", null, CancellationToken.None);
93
94             Assert.NotNull(thumbinfo);
95             Assert.Equal("http://www.tinami.com/view/12345", thumbinfo.ImageUrl);
96             Assert.Equal("http://img.tinami.com/hogehoge_150.gif", thumbinfo.ThumbnailUrl);
97             Assert.Equal("説明", thumbinfo.TooltipText);
98         }
99
100         [Fact]
101         public async Task ApiErrorTest()
102         {
103             var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
104                 "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
105
106             service.FakeXml = @"<?xml version='1.0' encoding='utf-8'?>
107 <rsp stat='user_only'>
108   <err msg='この作品は登録ユーザー限定の作品です。'/>
109 </rsp>";
110             var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", null, CancellationToken.None);
111
112             Assert.Null(thumbinfo);
113         }
114     }
115 }