OSDN Git Service

pbs.twimg.com の画像URLのフォーマット変更に対応
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / MetaThumbnailServiceTest.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.Threading;
29 using System.Threading.Tasks;
30 using Moq;
31 using OpenTween.Models;
32 using Xunit;
33 using Xunit.Extensions;
34
35 namespace OpenTween.Thumbnail.Services
36 {
37     public class MetaThumbnailServiceTest
38     {
39         class TestMetaThumbnailService : MetaThumbnailService
40         {
41             public string FakeHtml { get; set; } = "";
42
43             public TestMetaThumbnailService(string urlPattern)
44                 : base(null, urlPattern)
45             {
46             }
47
48             protected override Task<string> FetchImageUrlAsync(string url, CancellationToken token)
49                 => Task.FromResult(this.FakeHtml);
50         }
51
52         [Fact]
53         public async Task OGPMetaTest()
54         {
55             var service = new TestMetaThumbnailService(@"http://example.com/.+");
56
57             service.FakeHtml = @"
58 <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN' 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'>
59 <html xmlns='http://www.w3.org/1999/xhtml'>
60 <head>
61   <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
62   <meta property='og:image' content='http://img.example.com/abcd'/>
63   <title>hogehoge</title>
64 </head>
65 <body>
66   <p>hogehoge</p>
67 </body>
68 </html>
69 ";
70             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
71
72             Assert.NotNull(thumbinfo);
73             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
74             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
75             Assert.Null(thumbinfo.TooltipText);
76         }
77
78         [Fact]
79         public async Task TwitterMetaTest()
80         {
81             var service = new TestMetaThumbnailService(@"http://example.com/.+");
82
83             service.FakeHtml = @"
84 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
85
86 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
87 <meta name='twitter:image' content='http://img.example.com/abcd'>
88 <title>hogehoge</title>
89
90 <p>hogehoge
91 ";
92             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
93
94             Assert.NotNull(thumbinfo);
95             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
96             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
97             Assert.Null(thumbinfo.TooltipText);
98         }
99
100         [Fact]
101         public async Task InvalidMetaTest()
102         {
103             var service = new TestMetaThumbnailService(@"http://example.com/.+");
104
105             service.FakeHtml = @"
106 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
107
108 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
109 <meta name='twitter:image' value='http://img.example.com/abcd'>
110 <title>hogehoge</title>
111
112 <p>hogehoge
113 ";
114             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
115
116             Assert.NotNull(thumbinfo);
117             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
118             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
119             Assert.Null(thumbinfo.TooltipText);
120         }
121
122         [Fact]
123         public async Task ReverseMetaTest()
124         {
125             var service = new TestMetaThumbnailService(@"http://example.com/.+");
126
127             service.FakeHtml = @"
128 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
129
130 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
131 <meta content='http://img.example.com/abcd' name='twitter:image'>
132 <title>hogehoge</title>
133
134 <p>hogehoge
135 ";
136             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
137
138             Assert.NotNull(thumbinfo);
139             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
140             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
141             Assert.Null(thumbinfo.TooltipText);
142         }
143
144         [Fact]
145         public async Task BadMetaTest()
146         {
147             var service = new TestMetaThumbnailService(@"http://example.com/.+");
148
149             service.FakeHtml = @"
150 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
151
152 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
153 <meta name='og:image' content=''>
154 <meta content='http://img.example.com/abcd' name='twitter:image'>
155 <title>hogehoge</title>
156
157 <p>hogehoge
158 ";
159             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
160
161             Assert.NotNull(thumbinfo);
162             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
163             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
164             Assert.Null(thumbinfo.TooltipText);
165         }
166
167         [Fact]
168         public async Task BadMetaOneLineTest()
169         {
170             var service = new TestMetaThumbnailService(@"http://example.com/.+");
171
172             service.FakeHtml = @"
173 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
174
175 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
176 <meta name='og:image' content=''><meta content='http://img.example.com/abcd' name='twitter:image'>
177 <title>hogehoge</title>
178
179 <p>hogehoge
180 ";
181             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
182
183             Assert.NotNull(thumbinfo);
184             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
185             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
186             Assert.Null(thumbinfo.TooltipText);
187         }
188
189         [Fact]
190         public async Task ReverseMetaOneLineTest()
191         {
192             var service = new TestMetaThumbnailService(@"http://example.com/.+");
193
194             service.FakeHtml = @"
195 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
196
197 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
198 <meta content='' name='twitter:title'><meta content='http://img.example.com/abcd' name='twitter:image'>
199 <title>hogehoge</title>
200
201 <p>hogehoge
202 ";
203             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
204
205             Assert.NotNull(thumbinfo);
206             Assert.Equal("http://example.com/abcd", thumbinfo!.MediaPageUrl);
207             Assert.Equal("http://img.example.com/abcd", thumbinfo.ThumbnailImageUrl);
208             Assert.Null(thumbinfo.TooltipText);
209         }
210
211         [Fact]
212         public async Task NoMetaTest()
213         {
214             var service = new TestMetaThumbnailService(@"http://example.com/.+");
215
216             service.FakeHtml = @"
217 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
218
219 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
220 <title>hogehoge</title>
221
222 <p>hogehoge
223 ";
224             var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
225
226             Assert.Null(thumbinfo);
227         }
228     }
229 }