OSDN Git Service

pbs.twimg.com の画像URLのフォーマット変更に対応
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / PbsTwimgComTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2020 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.Text;
26 using System.Text.RegularExpressions;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using OpenTween.Models;
30 using Xunit;
31
32 namespace OpenTween.Thumbnail.Services
33 {
34     public class PbsTwimgComTest
35     {
36         [Fact]
37         public void ModernUrlPattern_Test()
38         {
39             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
40             var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
41
42             Assert.True(matches.Success);
43             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
44             Assert.Equal("jpg", matches.Groups["format"].Value);
45         }
46
47         [Fact]
48         public void ModernUrlPattern_UnorderedTest()
49         {
50             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?name=large&format=jpg";
51             var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
52
53             Assert.True(matches.Success);
54             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
55             Assert.Equal("jpg", matches.Groups["format"].Value);
56         }
57
58         [Fact]
59         public void ModernUrlPattern_LegacyUrlTest()
60         {
61             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg:large";
62             var matches = PbsTwimgCom.ModernUrlPattern.Match(mediaUrl);
63
64             Assert.False(matches.Success);
65         }
66
67         [Fact]
68         public void LegacyUrlPattern_Test()
69         {
70             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg";
71             var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
72
73             Assert.True(matches.Success);
74             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
75             Assert.Equal("jpg", matches.Groups["format"].Value);
76         }
77
78         [Fact]
79         public void LegacyUrlPattern_SizeNameTest()
80         {
81             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg:large";
82             var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
83
84             Assert.True(matches.Success);
85             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr", matches.Groups["base_url"].Value);
86             Assert.Equal("jpg", matches.Groups["format"].Value);
87         }
88
89         [Fact]
90         public void LegacyUrlPattern_ModernUrlTest()
91         {
92             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
93             var matches = PbsTwimgCom.LegacyUrlPattern.Match(mediaUrl);
94
95             Assert.False(matches.Success);
96         }
97
98         [Fact]
99         public async Task GetThumbnailInfoAsync_ModernUrlTest()
100         {
101             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large";
102
103             var service = new PbsTwimgCom();
104             var thumb = await service.GetThumbnailInfoAsync(mediaUrl, new PostClass(), CancellationToken.None)
105                 .ConfigureAwait(false);
106
107             Assert.NotNull(thumb);
108             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large", thumb!.ThumbnailImageUrl);
109             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=orig", thumb!.FullSizeImageUrl);
110         }
111
112         [Fact]
113         public async Task GetThumbnailInfoAsync_LegacyUrlTest()
114         {
115             var mediaUrl = "https://pbs.twimg.com/media/DYlFv51VwAUdqWr.jpg";
116
117             var service = new PbsTwimgCom();
118             var thumb = await service.GetThumbnailInfoAsync(mediaUrl, new PostClass(), CancellationToken.None)
119                 .ConfigureAwait(false);
120
121             Assert.NotNull(thumb);
122             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=large", thumb!.ThumbnailImageUrl);
123             Assert.Equal("https://pbs.twimg.com/media/DYlFv51VwAUdqWr?format=jpg&name=orig", thumb!.FullSizeImageUrl);
124         }
125     }
126 }