1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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;
26 using System.Net.Http;
28 using System.Threading;
29 using System.Threading.Tasks;
31 using Xunit.Extensions;
35 public class ShortUrlTest
38 public async Task ExpandUrlAsync_Test()
40 var handler = new HttpMessageHandlerMock();
41 using (var http = new HttpClient(handler))
43 var shortUrl = new ShortUrl(http);
45 // http://t.co/hoge1 -> http://example.com/hoge2
48 Assert.Equal(HttpMethod.Head, x.Method);
49 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
51 return this.CreateRedirectResponse("http://example.com/hoge2");
54 Assert.Equal(new Uri("http://example.com/hoge2"),
55 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
57 Assert.Equal(0, handler.QueueCount);
62 public async Task ExpandUrlAsync_IrregularUrlTest()
64 var handler = new HttpMessageHandlerMock();
65 using (var http = new HttpClient(handler))
67 var shortUrl = new ShortUrl(http);
69 // https://www.flickr.com/photo.gne?short=hoge -> /photos/foo/11111/
72 Assert.Equal(HttpMethod.Head, x.Method);
73 Assert.Equal(new Uri("https://www.flickr.com/photo.gne?short=hoge"), x.RequestUri);
75 return this.CreateRedirectResponse("/photos/foo/11111/", UriKind.Relative);
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")));
81 Assert.Equal(0, handler.QueueCount);
86 public async Task ExpandUrlAsync_DisableExpandingTest()
88 var handler = new HttpMessageHandlerMock();
89 using (var http = new HttpClient(handler))
91 var shortUrl = new ShortUrl(http);
93 shortUrl.DisableExpanding = true;
95 // http://t.co/hoge1 -> http://example.com/hoge2
100 return this.CreateRedirectResponse("http://example.com/hoge2");
103 Assert.Equal(new Uri("http://t.co/hoge1"),
104 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
106 Assert.Equal(1, handler.QueueCount);
111 public async Task ExpandUrlAsync_RecursiveTest()
113 var handler = new HttpMessageHandlerMock();
114 using (var http = new HttpClient(handler))
116 var shortUrl = new ShortUrl(http);
118 // http://t.co/hoge1 -> http://bit.ly/hoge2
121 Assert.Equal(HttpMethod.Head, x.Method);
122 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
124 return this.CreateRedirectResponse("http://bit.ly/hoge2");
127 // http://bit.ly/hoge2 -> http://example.com/hoge3
130 Assert.Equal(HttpMethod.Head, x.Method);
131 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
133 return this.CreateRedirectResponse("http://example.com/hoge3");
136 Assert.Equal(new Uri("http://example.com/hoge3"),
137 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
139 Assert.Equal(0, handler.QueueCount);
144 public async Task ExpandUrlAsync_RecursiveLimitTest()
146 var handler = new HttpMessageHandlerMock();
147 using (var http = new HttpClient(handler))
149 var shortUrl = new ShortUrl(http);
151 // http://t.co/hoge1 -> http://bit.ly/hoge2
154 Assert.Equal(HttpMethod.Head, x.Method);
155 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
157 return this.CreateRedirectResponse("http://bit.ly/hoge2");
160 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
163 Assert.Equal(HttpMethod.Head, x.Method);
164 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
166 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
169 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
174 return this.CreateRedirectResponse("http://example.com/hoge4");
177 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
178 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
180 Assert.Equal(1, handler.QueueCount);
185 public async Task ExpandUrlAsync_RelativeUriTest()
187 var handler = new HttpMessageHandlerMock();
188 using (var http = new HttpClient(handler))
190 var shortUrl = new ShortUrl(http);
196 return this.CreateRedirectResponse("");
199 // 相対 URI に対しては何も行わない
200 Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
201 await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));
203 Assert.Equal(1, handler.QueueCount);
208 public async Task ExpandUrlAsync_RelativeRedirectTest()
210 var handler = new HttpMessageHandlerMock();
211 using (var http = new HttpClient(handler))
213 var shortUrl = new ShortUrl(http);
215 // Location に相対 URL を指定したリダイレクト (テストに使う URL は適当)
216 // https://t.co/hogehoge -> /tetetete
219 Assert.Equal(HttpMethod.Head, x.Method);
220 Assert.Equal(new Uri("https://t.co/hogehoge"), x.RequestUri);
222 return this.CreateRedirectResponse("/tetetete", UriKind.Relative);
225 // https://t.co/tetetete -> http://example.com/tetetete
228 Assert.Equal(HttpMethod.Head, x.Method);
229 Assert.Equal(new Uri("https://t.co/tetetete"), x.RequestUri);
231 return this.CreateRedirectResponse("http://example.com/tetetete");
234 Assert.Equal(new Uri("http://example.com/tetetete"),
235 await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hogehoge")));
237 Assert.Equal(0, handler.QueueCount);
242 public async Task ExpandUrlAsync_String_Test()
244 var handler = new HttpMessageHandlerMock();
245 using (var http = new HttpClient(handler))
247 var shortUrl = new ShortUrl(http);
249 // http://t.co/hoge1 -> http://example.com/hoge2
252 Assert.Equal(HttpMethod.Head, x.Method);
253 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
255 return this.CreateRedirectResponse("http://example.com/hoge2");
258 Assert.Equal("http://example.com/hoge2",
259 await shortUrl.ExpandUrlAsync("http://t.co/hoge1"));
261 Assert.Equal(0, handler.QueueCount);
266 public async Task ExpandUrlAsync_String_SchemeLessUrlTest()
268 var handler = new HttpMessageHandlerMock();
269 using (var http = new HttpClient(handler))
271 var shortUrl = new ShortUrl(http);
273 // http://t.co/hoge1 -> http://example.com/hoge2
276 Assert.Equal(HttpMethod.Head, x.Method);
277 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
279 return this.CreateRedirectResponse("http://example.com/hoge2");
283 Assert.Equal("http://example.com/hoge2",
284 await shortUrl.ExpandUrlAsync("t.co/hoge1"));
286 Assert.Equal(0, handler.QueueCount);
291 public async Task ExpandUrlAsync_String_InvalidUrlTest()
293 var handler = new HttpMessageHandlerMock();
294 using (var http = new HttpClient(handler))
296 var shortUrl = new ShortUrl(http);
302 return this.CreateRedirectResponse("http://example.com/hoge2");
306 Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlAsync("..hogehoge.."));
308 Assert.Equal(1, handler.QueueCount);
313 public async Task ExpandUrlAsync_HttpErrorTest()
315 var handler = new HttpMessageHandlerMock();
316 using (var http = new HttpClient(handler))
318 var shortUrl = new ShortUrl(http);
320 // http://t.co/hoge1 -> 503 Service Unavailable
323 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
326 Assert.Equal(new Uri("http://t.co/hoge1"),
327 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
329 Assert.Equal(0, handler.QueueCount);
334 public async Task ExpandUrlHtmlAsync_Test()
336 var handler = new HttpMessageHandlerMock();
337 using (var http = new HttpClient(handler))
339 var shortUrl = new ShortUrl(http);
341 // http://t.co/hoge1 -> http://example.com/hoge2
344 Assert.Equal(HttpMethod.Head, x.Method);
345 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
347 return this.CreateRedirectResponse("http://example.com/hoge2");
350 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
351 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
353 Assert.Equal(0, handler.QueueCount);
358 public async Task ExpandUrlHtmlAsync_RelativeUriTest()
360 var handler = new HttpMessageHandlerMock();
361 using (var http = new HttpClient(handler))
363 var shortUrl = new ShortUrl(http);
369 return this.CreateRedirectResponse("http://example.com/hoge");
372 Assert.Equal("<a href=\"./hoge\">hogehoge</a>",
373 await shortUrl.ExpandUrlHtmlAsync("<a href=\"./hoge\">hogehoge</a>"));
375 Assert.Equal(1, handler.QueueCount);
379 private HttpResponseMessage CreateRedirectResponse(string uriStr)
381 return this.CreateRedirectResponse(uriStr, UriKind.Absolute);
384 private HttpResponseMessage CreateRedirectResponse(string uriStr, UriKind uriKind)
386 var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
387 response.Headers.Location = new Uri(uriStr, uriKind);
392 public async Task ShortenUrlAsync_TinyUrlTest()
394 var handler = new HttpMessageHandlerMock();
395 using (var http = new HttpClient(handler))
397 var shortUrl = new ShortUrl(http);
399 handler.Enqueue(async x =>
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());
405 return new HttpResponseMessage(HttpStatusCode.OK)
407 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
411 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
412 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
414 Assert.Equal(0, handler.QueueCount);
419 public async Task ShortenUrlAsync_UxnuUrlTest()
421 var handler = new HttpMessageHandlerMock();
422 using (var http = new HttpClient(handler))
424 var shortUrl = new ShortUrl(http);
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);
432 return new HttpResponseMessage(HttpStatusCode.OK)
434 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
438 Assert.Equal(new Uri("http://ux.nu/hoge"),
439 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
441 Assert.Equal(0, handler.QueueCount);