OSDN Git Service

OAuthUtilityでのタイムスタンプの出力にDateTimeUtcを使用する
[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 namespace OpenTween
34 {
35     public class ShortUrlTest
36     {
37         [Fact]
38         public async Task ExpandUrlAsync_Test()
39         {
40             var handler = new HttpMessageHandlerMock();
41             using (var http = new HttpClient(handler))
42             {
43                 var shortUrl = new ShortUrl(http);
44
45                 // http://t.co/hoge1 -> http://example.com/hoge2
46                 handler.Enqueue(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                 Assert.Equal(0, handler.QueueCount);
58             }
59         }
60
61         [Fact]
62         public async Task ExpandUrlAsync_IrregularUrlTest()
63         {
64             var handler = new HttpMessageHandlerMock();
65             using (var http = new HttpClient(handler))
66             {
67                 var shortUrl = new ShortUrl(http);
68
69                 // https://www.flickr.com/photo.gne?short=hoge -> /photos/foo/11111/
70                 handler.Enqueue(x =>
71                 {
72                     Assert.Equal(HttpMethod.Head, x.Method);
73                     Assert.Equal(new Uri("https://www.flickr.com/photo.gne?short=hoge"), x.RequestUri);
74
75                     return this.CreateRedirectResponse("/photos/foo/11111/", UriKind.Relative);
76                 });
77
78                 Assert.Equal(new Uri("https://www.flickr.com/photos/foo/11111/"),
79                     await shortUrl.ExpandUrlAsync(new Uri("https://www.flickr.com/photo.gne?short=hoge")));
80
81                 Assert.Equal(0, handler.QueueCount);
82             }
83         }
84
85         [Fact]
86         public async Task ExpandUrlAsync_DisableExpandingTest()
87         {
88             var handler = new HttpMessageHandlerMock();
89             using (var http = new HttpClient(handler))
90             {
91                 var shortUrl = new ShortUrl(http);
92
93                 shortUrl.DisableExpanding = true;
94
95                 // http://t.co/hoge1 -> http://example.com/hoge2
96                 handler.Enqueue(x =>
97                 {
98                     // このリクエストは実行されないはず
99                     Assert.True(false);
100                     return this.CreateRedirectResponse("http://example.com/hoge2");
101                 });
102
103                 Assert.Equal(new Uri("http://t.co/hoge1"),
104                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
105
106                 Assert.Equal(1, handler.QueueCount);
107             }
108         }
109
110         [Fact]
111         public async Task ExpandUrlAsync_RecursiveTest()
112         {
113             var handler = new HttpMessageHandlerMock();
114             using (var http = new HttpClient(handler))
115             {
116                 var shortUrl = new ShortUrl(http);
117
118                 // http://t.co/hoge1 -> http://bit.ly/hoge2
119                 handler.Enqueue(x =>
120                 {
121                     Assert.Equal(HttpMethod.Head, x.Method);
122                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
123
124                     return this.CreateRedirectResponse("http://bit.ly/hoge2");
125                 });
126
127                 // http://bit.ly/hoge2 -> http://example.com/hoge3
128                 handler.Enqueue(x =>
129                 {
130                     Assert.Equal(HttpMethod.Head, x.Method);
131                     Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
132
133                     return this.CreateRedirectResponse("http://example.com/hoge3");
134                 });
135
136                 Assert.Equal(new Uri("http://example.com/hoge3"),
137                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
138
139                 Assert.Equal(0, handler.QueueCount);
140             }
141         }
142
143         [Fact]
144         public async Task ExpandUrlAsync_RecursiveLimitTest()
145         {
146             var handler = new HttpMessageHandlerMock();
147             using (var http = new HttpClient(handler))
148             {
149                 var shortUrl = new ShortUrl(http);
150
151                 // http://t.co/hoge1 -> http://bit.ly/hoge2
152                 handler.Enqueue(x =>
153                 {
154                     Assert.Equal(HttpMethod.Head, x.Method);
155                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
156
157                     return this.CreateRedirectResponse("http://bit.ly/hoge2");
158                 });
159
160                 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
161                 handler.Enqueue(x =>
162                 {
163                     Assert.Equal(HttpMethod.Head, x.Method);
164                     Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
165
166                     return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
167                 });
168
169                 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
170                 handler.Enqueue(x =>
171                 {
172                     // このリクエストは実行されないはず
173                     Assert.True(false);
174                     return this.CreateRedirectResponse("http://example.com/hoge4");
175                 });
176
177                 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
178                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
179
180                 Assert.Equal(1, handler.QueueCount);
181             }
182         }
183
184         [Fact]
185         public async Task ExpandUrlAsync_RelativeUriTest()
186         {
187             var handler = new HttpMessageHandlerMock();
188             using (var http = new HttpClient(handler))
189             {
190                 var shortUrl = new ShortUrl(http);
191
192                 handler.Enqueue(x =>
193                 {
194                     // このリクエストは実行されないはず
195                     Assert.True(false);
196                     return this.CreateRedirectResponse("");
197                 });
198
199                 // 相対 URI に対しては何も行わない
200                 Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
201                     await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));
202
203                 Assert.Equal(1, handler.QueueCount);
204             }
205         }
206
207         [Fact]
208         public async Task ExpandUrlAsync_RelativeRedirectTest()
209         {
210             var handler = new HttpMessageHandlerMock();
211             using (var http = new HttpClient(handler))
212             {
213                 var shortUrl = new ShortUrl(http);
214
215                 // Location に相対 URL を指定したリダイレクト (テストに使う URL は適当)
216                 // https://t.co/hogehoge -> /tetetete
217                 handler.Enqueue(x =>
218                 {
219                     Assert.Equal(HttpMethod.Head, x.Method);
220                     Assert.Equal(new Uri("https://t.co/hogehoge"), x.RequestUri);
221
222                     return this.CreateRedirectResponse("/tetetete", UriKind.Relative);
223                 });
224
225                 // https://t.co/tetetete -> http://example.com/tetetete
226                 handler.Enqueue(x =>
227                 {
228                     Assert.Equal(HttpMethod.Head, x.Method);
229                     Assert.Equal(new Uri("https://t.co/tetetete"), x.RequestUri);
230
231                     return this.CreateRedirectResponse("http://example.com/tetetete");
232                 });
233
234                 Assert.Equal(new Uri("http://example.com/tetetete"),
235                     await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hogehoge")));
236
237                 Assert.Equal(0, handler.QueueCount);
238             }
239         }
240
241         [Fact]
242         public async Task ExpandUrlAsync_String_Test()
243         {
244             var handler = new HttpMessageHandlerMock();
245             using (var http = new HttpClient(handler))
246             {
247                 var shortUrl = new ShortUrl(http);
248
249                 // http://t.co/hoge1 -> http://example.com/hoge2
250                 handler.Enqueue(x =>
251                 {
252                     Assert.Equal(HttpMethod.Head, x.Method);
253                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
254
255                     return this.CreateRedirectResponse("http://example.com/hoge2");
256                 });
257
258                 Assert.Equal("http://example.com/hoge2",
259                     await shortUrl.ExpandUrlAsync("http://t.co/hoge1"));
260
261                 Assert.Equal(0, handler.QueueCount);
262             }
263         }
264
265         [Fact]
266         public async Task ExpandUrlAsync_String_SchemeLessUrlTest()
267         {
268             var handler = new HttpMessageHandlerMock();
269             using (var http = new HttpClient(handler))
270             {
271                 var shortUrl = new ShortUrl(http);
272
273                 // http://t.co/hoge1 -> http://example.com/hoge2
274                 handler.Enqueue(x =>
275                 {
276                     Assert.Equal(HttpMethod.Head, x.Method);
277                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
278
279                     return this.CreateRedirectResponse("http://example.com/hoge2");
280                 });
281
282                 // スキームが省略されたURL
283                 Assert.Equal("http://example.com/hoge2",
284                     await shortUrl.ExpandUrlAsync("t.co/hoge1"));
285
286                 Assert.Equal(0, handler.QueueCount);
287             }
288         }
289
290         [Fact]
291         public async Task ExpandUrlAsync_String_InvalidUrlTest()
292         {
293             var handler = new HttpMessageHandlerMock();
294             using (var http = new HttpClient(handler))
295             {
296                 var shortUrl = new ShortUrl(http);
297
298                 handler.Enqueue(x =>
299                 {
300                     // リクエストは送信されないはず
301                     Assert.True(false);
302                     return this.CreateRedirectResponse("http://example.com/hoge2");
303                 });
304
305                 // 不正なURL
306                 Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlAsync("..hogehoge.."));
307
308                 Assert.Equal(1, handler.QueueCount);
309             }
310         }
311
312         [Fact]
313         public async Task ExpandUrlAsync_HttpErrorTest()
314         {
315             var handler = new HttpMessageHandlerMock();
316             using (var http = new HttpClient(handler))
317             {
318                 var shortUrl = new ShortUrl(http);
319
320                 // http://t.co/hoge1 -> 503 Service Unavailable
321                 handler.Enqueue(x =>
322                 {
323                     return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
324                 });
325
326                 Assert.Equal(new Uri("http://t.co/hoge1"),
327                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
328
329                 Assert.Equal(0, handler.QueueCount);
330             }
331         }
332
333         [Fact]
334         public async Task ExpandUrlHtmlAsync_Test()
335         {
336             var handler = new HttpMessageHandlerMock();
337             using (var http = new HttpClient(handler))
338             {
339                 var shortUrl = new ShortUrl(http);
340
341                 // http://t.co/hoge1 -> http://example.com/hoge2
342                 handler.Enqueue(x =>
343                 {
344                     Assert.Equal(HttpMethod.Head, x.Method);
345                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
346
347                     return this.CreateRedirectResponse("http://example.com/hoge2");
348                 });
349
350                 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
351                     await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
352
353                 Assert.Equal(0, handler.QueueCount);
354             }
355         }
356
357         [Fact]
358         public async Task ExpandUrlHtmlAsync_RelativeUriTest()
359         {
360             var handler = new HttpMessageHandlerMock();
361             using (var http = new HttpClient(handler))
362             {
363                 var shortUrl = new ShortUrl(http);
364
365                 handler.Enqueue(x =>
366                 {
367                     // リクエストは送信されないはず
368                     Assert.True(false);
369                     return this.CreateRedirectResponse("http://example.com/hoge");
370                 });
371
372                 Assert.Equal("<a href=\"./hoge\">hogehoge</a>",
373                     await shortUrl.ExpandUrlHtmlAsync("<a href=\"./hoge\">hogehoge</a>"));
374
375                 Assert.Equal(1, handler.QueueCount);
376             }
377         }
378
379         private HttpResponseMessage CreateRedirectResponse(string uriStr)
380         {
381             return this.CreateRedirectResponse(uriStr, UriKind.Absolute);
382         }
383
384         private HttpResponseMessage CreateRedirectResponse(string uriStr, UriKind uriKind)
385         {
386             var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
387             response.Headers.Location = new Uri(uriStr, uriKind);
388             return response;
389         }
390
391         [Fact]
392         public async Task ShortenUrlAsync_TinyUrlTest()
393         {
394             var handler = new HttpMessageHandlerMock();
395             using (var http = new HttpClient(handler))
396             {
397                 var shortUrl = new ShortUrl(http);
398
399                 handler.Enqueue(async x =>
400                 {
401                     Assert.Equal(HttpMethod.Post, x.Method);
402                     Assert.Equal(new Uri("http://tinyurl.com/api-create.php"), x.RequestUri);
403                     Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehoge", await x.Content.ReadAsStringAsync());
404
405                     return new HttpResponseMessage(HttpStatusCode.OK)
406                     {
407                         Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
408                     };
409                 });
410
411                 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
412                     await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
413
414                 Assert.Equal(0, handler.QueueCount);
415             }
416         }
417
418         [Fact]
419         public async Task ShortenUrlAsync_UxnuUrlTest()
420         {
421             var handler = new HttpMessageHandlerMock();
422             using (var http = new HttpClient(handler))
423             {
424                 var shortUrl = new ShortUrl(http);
425
426                 handler.Enqueue(x =>
427                 {
428                     Assert.Equal(HttpMethod.Get, x.Method);
429                     Assert.Equal("http://ux.nu/api/short?format=plain&url=http:%2F%2Fexample.com%2Fhogehoge",
430                         x.RequestUri.AbsoluteUri);
431
432                     return new HttpResponseMessage(HttpStatusCode.OK)
433                     {
434                         Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
435                     };
436                 });
437
438                 Assert.Equal(new Uri("http://ux.nu/hoge"),
439                     await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
440
441                 Assert.Equal(0, handler.QueueCount);
442             }
443         }
444     }
445 }