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_DisableExpandingTest()
64 var handler = new HttpMessageHandlerMock();
65 using (var http = new HttpClient(handler))
67 var shortUrl = new ShortUrl(http);
69 shortUrl.DisableExpanding = true;
71 // http://t.co/hoge1 -> http://example.com/hoge2
76 return this.CreateRedirectResponse("http://example.com/hoge2");
79 Assert.Equal(new Uri("http://t.co/hoge1"),
80 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
82 Assert.Equal(1, handler.QueueCount);
87 public async Task ExpandUrlAsync_RecursiveTest()
89 var handler = new HttpMessageHandlerMock();
90 using (var http = new HttpClient(handler))
92 var shortUrl = new ShortUrl(http);
94 // http://t.co/hoge1 -> http://bit.ly/hoge2
97 Assert.Equal(HttpMethod.Head, x.Method);
98 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
100 return this.CreateRedirectResponse("http://bit.ly/hoge2");
103 // http://bit.ly/hoge2 -> http://example.com/hoge3
106 Assert.Equal(HttpMethod.Head, x.Method);
107 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
109 return this.CreateRedirectResponse("http://example.com/hoge3");
112 Assert.Equal(new Uri("http://example.com/hoge3"),
113 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
115 Assert.Equal(0, handler.QueueCount);
120 public async Task ExpandUrlAsync_RecursiveLimitTest()
122 var handler = new HttpMessageHandlerMock();
123 using (var http = new HttpClient(handler))
125 var shortUrl = new ShortUrl(http);
127 // http://t.co/hoge1 -> http://bit.ly/hoge2
130 Assert.Equal(HttpMethod.Head, x.Method);
131 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
133 return this.CreateRedirectResponse("http://bit.ly/hoge2");
136 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
139 Assert.Equal(HttpMethod.Head, x.Method);
140 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
142 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
145 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
150 return this.CreateRedirectResponse("http://example.com/hoge4");
153 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
154 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
156 Assert.Equal(1, handler.QueueCount);
161 public async Task ExpandUrlAsync_RelativeUriTest()
163 var handler = new HttpMessageHandlerMock();
164 using (var http = new HttpClient(handler))
166 var shortUrl = new ShortUrl(http);
172 return this.CreateRedirectResponse("");
175 // 相対 URI に対しては何も行わない
176 Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
177 await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));
179 Assert.Equal(1, handler.QueueCount);
184 public async Task ExpandUrlStrAsync_Test()
186 var handler = new HttpMessageHandlerMock();
187 using (var http = new HttpClient(handler))
189 var shortUrl = new ShortUrl(http);
191 // http://t.co/hoge1 -> http://example.com/hoge2
194 Assert.Equal(HttpMethod.Head, x.Method);
195 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
197 return this.CreateRedirectResponse("http://example.com/hoge2");
200 Assert.Equal("http://example.com/hoge2",
201 await shortUrl.ExpandUrlStrAsync("http://t.co/hoge1"));
203 Assert.Equal(0, handler.QueueCount);
208 public async Task ExpandUrlStrAsync_SchemeLessUrlTest()
210 var handler = new HttpMessageHandlerMock();
211 using (var http = new HttpClient(handler))
213 var shortUrl = new ShortUrl(http);
215 // http://t.co/hoge1 -> http://example.com/hoge2
218 Assert.Equal(HttpMethod.Head, x.Method);
219 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
221 return this.CreateRedirectResponse("http://example.com/hoge2");
225 Assert.Equal("http://example.com/hoge2",
226 await shortUrl.ExpandUrlStrAsync("t.co/hoge1"));
228 Assert.Equal(0, handler.QueueCount);
233 public async Task ExpandUrlStrAsync_InvalidUrlTest()
235 var handler = new HttpMessageHandlerMock();
236 using (var http = new HttpClient(handler))
238 var shortUrl = new ShortUrl(http);
244 return this.CreateRedirectResponse("http://example.com/hoge2");
248 Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlStrAsync("..hogehoge.."));
250 Assert.Equal(1, handler.QueueCount);
255 public async Task ExpandUrlAsync_HttpErrorTest()
257 var handler = new HttpMessageHandlerMock();
258 using (var http = new HttpClient(handler))
260 var shortUrl = new ShortUrl(http);
262 // http://t.co/hoge1 -> 503 Service Unavailable
265 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
268 Assert.Equal(new Uri("http://t.co/hoge1"),
269 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
271 Assert.Equal(0, handler.QueueCount);
276 public async Task ExpandUrlHtmlAsync_Test()
278 var handler = new HttpMessageHandlerMock();
279 using (var http = new HttpClient(handler))
281 var shortUrl = new ShortUrl(http);
283 // http://t.co/hoge1 -> http://example.com/hoge2
286 Assert.Equal(HttpMethod.Head, x.Method);
287 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
289 return this.CreateRedirectResponse("http://example.com/hoge2");
292 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
293 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
295 Assert.Equal(0, handler.QueueCount);
299 private HttpResponseMessage CreateRedirectResponse(string uriStr)
301 var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
302 response.Headers.Location = new Uri(uriStr);
307 public async Task ShortenUrlAsync_TinyUrlTest()
309 var handler = new HttpMessageHandlerMock();
310 using (var http = new HttpClient(handler))
312 var shortUrl = new ShortUrl(http);
314 handler.Enqueue(async x =>
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());
320 return new HttpResponseMessage(HttpStatusCode.OK)
322 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
326 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
327 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
329 Assert.Equal(0, handler.QueueCount);
334 public async Task ShortenUrlAsync_UxnuUrlTest()
336 var handler = new HttpMessageHandlerMock();
337 using (var http = new HttpClient(handler))
339 var shortUrl = new ShortUrl(http);
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());
347 return new HttpResponseMessage(HttpStatusCode.OK)
349 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
353 Assert.Equal(new Uri("http://ux.nu/hoge"),
354 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
356 Assert.Equal(0, handler.QueueCount);