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.Reflection;
26 using System.Runtime.InteropServices;
28 using System.Threading;
29 using System.Threading.Tasks;
31 using OpenTween.Models;
33 using Xunit.Extensions;
35 namespace OpenTween.Thumbnail.Services
37 public class MetaThumbnailServiceTest
39 class TestMetaThumbnailService : MetaThumbnailService
41 public string FakeHtml { get; set; } = "";
43 public TestMetaThumbnailService(string urlPattern)
44 : base(null, urlPattern)
48 protected override Task<string> FetchImageUrlAsync(string url, CancellationToken token)
49 => Task.FromResult(this.FakeHtml);
53 public async Task OGPMetaTest()
55 var service = new TestMetaThumbnailService(@"http://example.com/.+");
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'>
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>
70 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
79 public async Task TwitterMetaTest()
81 var service = new TestMetaThumbnailService(@"http://example.com/.+");
84 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
92 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
101 public async Task InvalidMetaTest()
103 var service = new TestMetaThumbnailService(@"http://example.com/.+");
105 service.FakeHtml = @"
106 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
114 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
123 public async Task ReverseMetaTest()
125 var service = new TestMetaThumbnailService(@"http://example.com/.+");
127 service.FakeHtml = @"
128 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
136 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
145 public async Task BadMetaTest()
147 var service = new TestMetaThumbnailService(@"http://example.com/.+");
149 service.FakeHtml = @"
150 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
159 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
168 public async Task BadMetaOneLineTest()
170 var service = new TestMetaThumbnailService(@"http://example.com/.+");
172 service.FakeHtml = @"
173 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
181 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
190 public async Task ReverseMetaOneLineTest()
192 var service = new TestMetaThumbnailService(@"http://example.com/.+");
194 service.FakeHtml = @"
195 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
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>
203 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
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);
212 public async Task NoMetaTest()
214 var service = new TestMetaThumbnailService(@"http://example.com/.+");
216 service.FakeHtml = @"
217 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
219 <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
220 <title>hogehoge</title>
224 var thumbinfo = await service.GetThumbnailInfoAsync("http://example.com/abcd", new PostClass(), CancellationToken.None);
226 Assert.Null(thumbinfo);