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;
33 #pragma warning disable 1998 // awaitが無いasyncラムダ式に対する警告を抑制
37 public class ShortUrlTest
40 public async Task ExpandUrlAsync_Test()
42 var handler = new HttpMessageHandlerMock();
43 var shortUrl = new ShortUrl(new HttpClient(handler));
45 // http://t.co/hoge1 -> http://example.com/hoge2
46 handler.Queue.Enqueue(async x =>
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")));
59 public async Task ExpandUrlAsync_DisableExpandingTest()
61 var handler = new HttpMessageHandlerMock();
62 var shortUrl = new ShortUrl(new HttpClient(handler));
64 shortUrl.DisableExpanding = true;
66 // http://t.co/hoge1 -> http://example.com/hoge2
67 handler.Queue.Enqueue(async x =>
71 return this.CreateRedirectResponse("http://example.com/hoge2");
74 Assert.Equal(new Uri("http://t.co/hoge1"),
75 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
79 public async Task ExpandUrlAsync_RecursiveTest()
81 var handler = new HttpMessageHandlerMock();
82 var shortUrl = new ShortUrl(new HttpClient(handler));
84 // http://t.co/hoge1 -> http://bit.ly/hoge2
85 handler.Queue.Enqueue(async x =>
87 Assert.Equal(HttpMethod.Head, x.Method);
88 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
90 return this.CreateRedirectResponse("http://bit.ly/hoge2");
93 // http://bit.ly/hoge2 -> http://example.com/hoge3
94 handler.Queue.Enqueue(async x =>
96 Assert.Equal(HttpMethod.Head, x.Method);
97 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
99 return this.CreateRedirectResponse("http://example.com/hoge3");
102 Assert.Equal(new Uri("http://example.com/hoge3"),
103 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
107 public async Task ExpandUrlAsync_RecursiveLimitTest()
109 var handler = new HttpMessageHandlerMock();
110 var shortUrl = new ShortUrl(new HttpClient(handler));
112 // http://t.co/hoge1 -> http://bit.ly/hoge2
113 handler.Queue.Enqueue(async x =>
115 Assert.Equal(HttpMethod.Head, x.Method);
116 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
118 return this.CreateRedirectResponse("http://bit.ly/hoge2");
121 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
122 handler.Queue.Enqueue(async x =>
124 Assert.Equal(HttpMethod.Head, x.Method);
125 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
127 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
130 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
131 handler.Queue.Enqueue(async x =>
135 return this.CreateRedirectResponse("http://example.com/hoge4");
138 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
139 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
143 public async Task ExpandUrlStrAsync_Test()
145 var handler = new HttpMessageHandlerMock();
146 var shortUrl = new ShortUrl(new HttpClient(handler));
148 // http://t.co/hoge1 -> http://example.com/hoge2
149 handler.Queue.Enqueue(async x =>
151 Assert.Equal(HttpMethod.Head, x.Method);
152 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
154 return this.CreateRedirectResponse("http://example.com/hoge2");
157 Assert.Equal("http://example.com/hoge2",
158 await shortUrl.ExpandUrlStrAsync("http://t.co/hoge1"));
162 public async Task ExpandUrlStrAsync_SchemeLessUrlTest()
164 var handler = new HttpMessageHandlerMock();
165 var shortUrl = new ShortUrl(new HttpClient(handler));
167 // http://t.co/hoge1 -> http://example.com/hoge2
168 handler.Queue.Enqueue(async x =>
170 Assert.Equal(HttpMethod.Head, x.Method);
171 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
173 return this.CreateRedirectResponse("http://example.com/hoge2");
177 Assert.Equal("http://example.com/hoge2",
178 await shortUrl.ExpandUrlStrAsync("t.co/hoge1"));
182 public async Task ExpandUrlStrAsync_InvalidUrlTest()
184 var handler = new HttpMessageHandlerMock();
185 var shortUrl = new ShortUrl(new HttpClient(handler));
187 handler.Queue.Enqueue(async x =>
191 return this.CreateRedirectResponse("http://example.com/hoge2");
195 Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlStrAsync("..hogehoge.."));
199 public async Task ExpandUrlAsync_HttpErrorTest()
201 var handler = new HttpMessageHandlerMock();
202 var shortUrl = new ShortUrl(new HttpClient(handler));
204 // http://t.co/hoge1 -> 503 Service Unavailable
205 handler.Queue.Enqueue(async x =>
207 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
210 Assert.Equal(new Uri("http://t.co/hoge1"),
211 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
215 public async Task ExpandUrlHtmlAsync_Test()
217 var handler = new HttpMessageHandlerMock();
218 var shortUrl = new ShortUrl(new HttpClient(handler));
220 // http://t.co/hoge1 -> http://example.com/hoge2
221 handler.Queue.Enqueue(async x =>
223 Assert.Equal(HttpMethod.Head, x.Method);
224 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
226 return this.CreateRedirectResponse("http://example.com/hoge2");
229 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
230 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
233 private HttpResponseMessage CreateRedirectResponse(string uriStr)
235 var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
236 response.Headers.Location = new Uri(uriStr);
241 public async Task ShortenUrlAsync_TinyUrlTest()
243 var handler = new HttpMessageHandlerMock();
244 var shortUrl = new ShortUrl(new HttpClient(handler));
246 handler.Queue.Enqueue(async x =>
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());
252 return new HttpResponseMessage(HttpStatusCode.OK)
254 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
258 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
259 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
263 public async Task ShortenUrlAsync_UxnuUrlTest()
265 var handler = new HttpMessageHandlerMock();
266 var shortUrl = new ShortUrl(new HttpClient(handler));
268 handler.Queue.Enqueue(async x =>
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());
274 return new HttpResponseMessage(HttpStatusCode.OK)
276 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
280 Assert.Equal(new Uri("http://ux.nu/hoge"),
281 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));