OSDN Git Service

media_idsの連結をstring.Join()を使った処理に書き換え
[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_DisableExpandingTest()
63         {
64             var handler = new HttpMessageHandlerMock();
65             using (var http = new HttpClient(handler))
66             {
67                 var shortUrl = new ShortUrl(http);
68
69                 shortUrl.DisableExpanding = true;
70
71                 // http://t.co/hoge1 -> http://example.com/hoge2
72                 handler.Enqueue(x =>
73                 {
74                     // このリクエストは実行されないはず
75                     Assert.True(false);
76                     return this.CreateRedirectResponse("http://example.com/hoge2");
77                 });
78
79                 Assert.Equal(new Uri("http://t.co/hoge1"),
80                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
81
82                 Assert.Equal(1, handler.QueueCount);
83             }
84         }
85
86         [Fact]
87         public async Task ExpandUrlAsync_RecursiveTest()
88         {
89             var handler = new HttpMessageHandlerMock();
90             using (var http = new HttpClient(handler))
91             {
92                 var shortUrl = new ShortUrl(http);
93
94                 // http://t.co/hoge1 -> http://bit.ly/hoge2
95                 handler.Enqueue(x =>
96                 {
97                     Assert.Equal(HttpMethod.Head, x.Method);
98                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
99
100                     return this.CreateRedirectResponse("http://bit.ly/hoge2");
101                 });
102
103                 // http://bit.ly/hoge2 -> http://example.com/hoge3
104                 handler.Enqueue(x =>
105                 {
106                     Assert.Equal(HttpMethod.Head, x.Method);
107                     Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
108
109                     return this.CreateRedirectResponse("http://example.com/hoge3");
110                 });
111
112                 Assert.Equal(new Uri("http://example.com/hoge3"),
113                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
114
115                 Assert.Equal(0, handler.QueueCount);
116             }
117         }
118
119         [Fact]
120         public async Task ExpandUrlAsync_RecursiveLimitTest()
121         {
122             var handler = new HttpMessageHandlerMock();
123             using (var http = new HttpClient(handler))
124             {
125                 var shortUrl = new ShortUrl(http);
126
127                 // http://t.co/hoge1 -> http://bit.ly/hoge2
128                 handler.Enqueue(x =>
129                 {
130                     Assert.Equal(HttpMethod.Head, x.Method);
131                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
132
133                     return this.CreateRedirectResponse("http://bit.ly/hoge2");
134                 });
135
136                 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
137                 handler.Enqueue(x =>
138                 {
139                     Assert.Equal(HttpMethod.Head, x.Method);
140                     Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
141
142                     return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
143                 });
144
145                 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
146                 handler.Enqueue(x =>
147                 {
148                     // このリクエストは実行されないはず
149                     Assert.True(false);
150                     return this.CreateRedirectResponse("http://example.com/hoge4");
151                 });
152
153                 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
154                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
155
156                 Assert.Equal(1, handler.QueueCount);
157             }
158         }
159
160         [Fact]
161         public async Task ExpandUrlAsync_RelativeUriTest()
162         {
163             var handler = new HttpMessageHandlerMock();
164             using (var http = new HttpClient(handler))
165             {
166                 var shortUrl = new ShortUrl(http);
167
168                 handler.Enqueue(x =>
169                 {
170                     // このリクエストは実行されないはず
171                     Assert.True(false);
172                     return this.CreateRedirectResponse("");
173                 });
174
175                 // 相対 URI に対しては何も行わない
176                 Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
177                     await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));
178
179                 Assert.Equal(1, handler.QueueCount);
180             }
181         }
182
183         [Fact]
184         public async Task ExpandUrlStrAsync_Test()
185         {
186             var handler = new HttpMessageHandlerMock();
187             using (var http = new HttpClient(handler))
188             {
189                 var shortUrl = new ShortUrl(http);
190
191                 // http://t.co/hoge1 -> http://example.com/hoge2
192                 handler.Enqueue(x =>
193                 {
194                     Assert.Equal(HttpMethod.Head, x.Method);
195                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
196
197                     return this.CreateRedirectResponse("http://example.com/hoge2");
198                 });
199
200                 Assert.Equal("http://example.com/hoge2",
201                     await shortUrl.ExpandUrlStrAsync("http://t.co/hoge1"));
202
203                 Assert.Equal(0, handler.QueueCount);
204             }
205         }
206
207         [Fact]
208         public async Task ExpandUrlStrAsync_SchemeLessUrlTest()
209         {
210             var handler = new HttpMessageHandlerMock();
211             using (var http = new HttpClient(handler))
212             {
213                 var shortUrl = new ShortUrl(http);
214
215                 // http://t.co/hoge1 -> http://example.com/hoge2
216                 handler.Enqueue(x =>
217                 {
218                     Assert.Equal(HttpMethod.Head, x.Method);
219                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
220
221                     return this.CreateRedirectResponse("http://example.com/hoge2");
222                 });
223
224                 // スキームが省略されたURL
225                 Assert.Equal("http://example.com/hoge2",
226                     await shortUrl.ExpandUrlStrAsync("t.co/hoge1"));
227
228                 Assert.Equal(0, handler.QueueCount);
229             }
230         }
231
232         [Fact]
233         public async Task ExpandUrlStrAsync_InvalidUrlTest()
234         {
235             var handler = new HttpMessageHandlerMock();
236             using (var http = new HttpClient(handler))
237             {
238                 var shortUrl = new ShortUrl(http);
239
240                 handler.Enqueue(x =>
241                 {
242                     // リクエストは送信されないはず
243                     Assert.True(false);
244                     return this.CreateRedirectResponse("http://example.com/hoge2");
245                 });
246
247                 // 不正なURL
248                 Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlStrAsync("..hogehoge.."));
249
250                 Assert.Equal(1, handler.QueueCount);
251             }
252         }
253
254         [Fact]
255         public async Task ExpandUrlAsync_HttpErrorTest()
256         {
257             var handler = new HttpMessageHandlerMock();
258             using (var http = new HttpClient(handler))
259             {
260                 var shortUrl = new ShortUrl(http);
261
262                 // http://t.co/hoge1 -> 503 Service Unavailable
263                 handler.Enqueue(x =>
264                 {
265                     return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
266                 });
267
268                 Assert.Equal(new Uri("http://t.co/hoge1"),
269                     await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
270
271                 Assert.Equal(0, handler.QueueCount);
272             }
273         }
274
275         [Fact]
276         public async Task ExpandUrlHtmlAsync_Test()
277         {
278             var handler = new HttpMessageHandlerMock();
279             using (var http = new HttpClient(handler))
280             {
281                 var shortUrl = new ShortUrl(http);
282
283                 // http://t.co/hoge1 -> http://example.com/hoge2
284                 handler.Enqueue(x =>
285                 {
286                     Assert.Equal(HttpMethod.Head, x.Method);
287                     Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
288
289                     return this.CreateRedirectResponse("http://example.com/hoge2");
290                 });
291
292                 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
293                     await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
294
295                 Assert.Equal(0, handler.QueueCount);
296             }
297         }
298
299         private HttpResponseMessage CreateRedirectResponse(string uriStr)
300         {
301             var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
302             response.Headers.Location = new Uri(uriStr);
303             return response;
304         }
305
306         [Fact]
307         public async Task ShortenUrlAsync_TinyUrlTest()
308         {
309             var handler = new HttpMessageHandlerMock();
310             using (var http = new HttpClient(handler))
311             {
312                 var shortUrl = new ShortUrl(http);
313
314                 handler.Enqueue(async x =>
315                 {
316                     Assert.Equal(HttpMethod.Post, x.Method);
317                     Assert.Equal(new Uri("http://tinyurl.com/api-create.php"), x.RequestUri);
318                     Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehoge", await x.Content.ReadAsStringAsync());
319
320                     return new HttpResponseMessage(HttpStatusCode.OK)
321                     {
322                         Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
323                     };
324                 });
325
326                 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
327                     await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
328
329                 Assert.Equal(0, handler.QueueCount);
330             }
331         }
332
333         [Fact]
334         public async Task ShortenUrlAsync_UxnuUrlTest()
335         {
336             var handler = new HttpMessageHandlerMock();
337             using (var http = new HttpClient(handler))
338             {
339                 var shortUrl = new ShortUrl(http);
340
341                 handler.Enqueue(x =>
342                 {
343                     Assert.Equal(HttpMethod.Get, x.Method);
344                     Assert.Equal("http://ux.nu/api/short?format=plain&url=http://example.com/hogehoge",
345                         x.RequestUri.ToString());
346
347                     return new HttpResponseMessage(HttpStatusCode.OK)
348                     {
349                         Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
350                     };
351                 });
352
353                 Assert.Equal(new Uri("http://ux.nu/hoge"),
354                     await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
355
356                 Assert.Equal(0, handler.QueueCount);
357             }
358         }
359     }
360 }