OSDN Git Service

ライセンス表示の誤記を修正
[opentween/open-tween.git] / OpenTween.Tests / ShortUrlTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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.Net;
26 using System.Net.Http;
27 using System.Text;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using Xunit;
31 using Xunit.Extensions;
32
33 #pragma warning disable 1998 // awaitが無いasyncラムダ式に対する警告を抑制
34
35 namespace OpenTween
36 {
37     public class ShortUrlTest
38     {
39         [Fact]
40         public async Task ExpandUrlAsync_Test()
41         {
42             var handler = new HttpMessageHandlerMock();
43             var shortUrl = new ShortUrl(new HttpClient(handler));
44
45             // http://t.co/hoge1 -> http://example.com/hoge2
46             handler.Queue.Enqueue(async x =>
47             {
48                 Assert.Equal(HttpMethod.Head, x.Method);
49                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
50
51                 return this.CreateRedirectResponse("http://example.com/hoge2");
52             });
53
54             Assert.Equal(new Uri("http://example.com/hoge2"),
55                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
56         }
57
58         [Fact]
59         public async Task ExpandUrlAsync_DisableExpandingTest()
60         {
61             var handler = new HttpMessageHandlerMock();
62             var shortUrl = new ShortUrl(new HttpClient(handler));
63
64             shortUrl.DisableExpanding = true;
65
66             // http://t.co/hoge1 -> http://example.com/hoge2
67             handler.Queue.Enqueue(async x =>
68             {
69                 // このリクエストは実行されないはず
70                 Assert.True(false);
71                 return this.CreateRedirectResponse("http://example.com/hoge2");
72             });
73
74             Assert.Equal(new Uri("http://t.co/hoge1"),
75                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
76         }
77
78         [Fact]
79         public async Task ExpandUrlAsync_RecursiveTest()
80         {
81             var handler = new HttpMessageHandlerMock();
82             var shortUrl = new ShortUrl(new HttpClient(handler));
83
84             // http://t.co/hoge1 -> http://bit.ly/hoge2
85             handler.Queue.Enqueue(async x =>
86             {
87                 Assert.Equal(HttpMethod.Head, x.Method);
88                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
89
90                 return this.CreateRedirectResponse("http://bit.ly/hoge2");
91             });
92
93             // http://bit.ly/hoge2 -> http://example.com/hoge3
94             handler.Queue.Enqueue(async x =>
95             {
96                 Assert.Equal(HttpMethod.Head, x.Method);
97                 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
98
99                 return this.CreateRedirectResponse("http://example.com/hoge3");
100             });
101
102             Assert.Equal(new Uri("http://example.com/hoge3"),
103                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
104         }
105
106         [Fact]
107         public async Task ExpandUrlAsync_RecursiveLimitTest()
108         {
109             var handler = new HttpMessageHandlerMock();
110             var shortUrl = new ShortUrl(new HttpClient(handler));
111
112             // http://t.co/hoge1 -> http://bit.ly/hoge2
113             handler.Queue.Enqueue(async x =>
114             {
115                 Assert.Equal(HttpMethod.Head, x.Method);
116                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
117
118                 return this.CreateRedirectResponse("http://bit.ly/hoge2");
119             });
120
121             // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
122             handler.Queue.Enqueue(async x =>
123             {
124                 Assert.Equal(HttpMethod.Head, x.Method);
125                 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
126
127                 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
128             });
129
130             // http://tinyurl.com/hoge3 -> http://example.com/hoge4
131             handler.Queue.Enqueue(async x =>
132             {
133                 // このリクエストは実行されないはず
134                 Assert.True(false);
135                 return this.CreateRedirectResponse("http://example.com/hoge4");
136             });
137
138             Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
139                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
140         }
141
142         [Fact]
143         public async Task ExpandUrlStrAsync_Test()
144         {
145             var handler = new HttpMessageHandlerMock();
146             var shortUrl = new ShortUrl(new HttpClient(handler));
147
148             // http://t.co/hoge1 -> http://example.com/hoge2
149             handler.Queue.Enqueue(async x =>
150             {
151                 Assert.Equal(HttpMethod.Head, x.Method);
152                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
153
154                 return this.CreateRedirectResponse("http://example.com/hoge2");
155             });
156
157             Assert.Equal("http://example.com/hoge2",
158                 await shortUrl.ExpandUrlStrAsync("http://t.co/hoge1"));
159         }
160
161         [Fact]
162         public async Task ExpandUrlStrAsync_SchemeLessUrlTest()
163         {
164             var handler = new HttpMessageHandlerMock();
165             var shortUrl = new ShortUrl(new HttpClient(handler));
166
167             // http://t.co/hoge1 -> http://example.com/hoge2
168             handler.Queue.Enqueue(async x =>
169             {
170                 Assert.Equal(HttpMethod.Head, x.Method);
171                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
172
173                 return this.CreateRedirectResponse("http://example.com/hoge2");
174             });
175
176             // スキームが省略されたURL
177             Assert.Equal("http://example.com/hoge2",
178                 await shortUrl.ExpandUrlStrAsync("t.co/hoge1"));
179         }
180
181         [Fact]
182         public async Task ExpandUrlStrAsync_InvalidUrlTest()
183         {
184             var handler = new HttpMessageHandlerMock();
185             var shortUrl = new ShortUrl(new HttpClient(handler));
186
187             handler.Queue.Enqueue(async x =>
188             {
189                 // リクエストは送信されないはず
190                 Assert.True(false);
191                 return this.CreateRedirectResponse("http://example.com/hoge2");
192             });
193
194             // 不正なURL
195             Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlStrAsync("..hogehoge.."));
196         }
197
198         [Fact]
199         public async Task ExpandUrlAsync_HttpErrorTest()
200         {
201             var handler = new HttpMessageHandlerMock();
202             var shortUrl = new ShortUrl(new HttpClient(handler));
203
204             // http://t.co/hoge1 -> 503 Service Unavailable
205             handler.Queue.Enqueue(async x =>
206             {
207                 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
208             });
209
210             Assert.Equal(new Uri("http://t.co/hoge1"),
211                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
212         }
213
214         [Fact]
215         public async Task ExpandUrlHtmlAsync_Test()
216         {
217             var handler = new HttpMessageHandlerMock();
218             var shortUrl = new ShortUrl(new HttpClient(handler));
219
220             // http://t.co/hoge1 -> http://example.com/hoge2
221             handler.Queue.Enqueue(async x =>
222             {
223                 Assert.Equal(HttpMethod.Head, x.Method);
224                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
225
226                 return this.CreateRedirectResponse("http://example.com/hoge2");
227             });
228
229             Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
230                 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
231         }
232
233         private HttpResponseMessage CreateRedirectResponse(string uriStr)
234         {
235             var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
236             response.Headers.Location = new Uri(uriStr);
237             return response;
238         }
239
240         [Fact]
241         public async Task ShortenUrlAsync_TinyUrlTest()
242         {
243             var handler = new HttpMessageHandlerMock();
244             var shortUrl = new ShortUrl(new HttpClient(handler));
245
246             handler.Queue.Enqueue(async x =>
247             {
248                 Assert.Equal(HttpMethod.Post, x.Method);
249                 Assert.Equal(new Uri("http://tinyurl.com/api-create.php"), x.RequestUri);
250                 Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehoge", await x.Content.ReadAsStringAsync());
251
252                 return new HttpResponseMessage(HttpStatusCode.OK)
253                 {
254                     Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
255                 };
256             });
257
258             Assert.Equal(new Uri("http://tinyurl.com/hoge"),
259                 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
260         }
261
262         [Fact]
263         public async Task ShortenUrlAsync_UxnuUrlTest()
264         {
265             var handler = new HttpMessageHandlerMock();
266             var shortUrl = new ShortUrl(new HttpClient(handler));
267
268             handler.Queue.Enqueue(async x =>
269             {
270                 Assert.Equal(HttpMethod.Get, x.Method);
271                 Assert.Equal("http://ux.nu/api/short?format=plain&url=http://example.com/hogehoge",
272                     x.RequestUri.ToString());
273
274                 return new HttpResponseMessage(HttpStatusCode.OK)
275                 {
276                     Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
277                 };
278             });
279
280             Assert.Equal(new Uri("http://ux.nu/hoge"),
281                 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
282         }
283     }
284 }