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
37 class HttpMessageHandlerMock : HttpMessageHandler
39 public readonly Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>> Queue =
40 new Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>>();
42 protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
44 var handler = this.Queue.Dequeue();
45 return handler(request);
49 #pragma warning disable 1998 // awaitが無いasyncラムダ式に対する警告を抑制
52 public async Task ExpandUrlAsync_Test()
54 var handler = new HttpMessageHandlerMock();
55 var shortUrl = new ShortUrl(new HttpClient(handler));
57 // http://t.co/hoge1 -> http://example.com/hoge2
58 handler.Queue.Enqueue(async x =>
60 Assert.Equal(HttpMethod.Head, x.Method);
61 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
63 return this.CreateRedirectResponse("http://example.com/hoge2");
66 Assert.Equal(new Uri("http://example.com/hoge2"),
67 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
71 public async Task ExpandUrlAsync_DisableExpandingTest()
73 var handler = new HttpMessageHandlerMock();
74 var shortUrl = new ShortUrl(new HttpClient(handler));
76 shortUrl.DisableExpanding = true;
78 // http://t.co/hoge1 -> http://example.com/hoge2
79 handler.Queue.Enqueue(async x =>
83 return this.CreateRedirectResponse("http://example.com/hoge2");
86 Assert.Equal(new Uri("http://t.co/hoge1"),
87 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
91 public async Task ExpandUrlAsync_RecursiveTest()
93 var handler = new HttpMessageHandlerMock();
94 var shortUrl = new ShortUrl(new HttpClient(handler));
96 // http://t.co/hoge1 -> http://bit.ly/hoge2
97 handler.Queue.Enqueue(async x =>
99 Assert.Equal(HttpMethod.Head, x.Method);
100 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
102 return this.CreateRedirectResponse("http://bit.ly/hoge2");
105 // http://bit.ly/hoge2 -> http://example.com/hoge3
106 handler.Queue.Enqueue(async x =>
108 Assert.Equal(HttpMethod.Head, x.Method);
109 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
111 return this.CreateRedirectResponse("http://example.com/hoge3");
114 Assert.Equal(new Uri("http://example.com/hoge3"),
115 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
119 public async Task ExpandUrlAsync_RecursiveLimitTest()
121 var handler = new HttpMessageHandlerMock();
122 var shortUrl = new ShortUrl(new HttpClient(handler));
124 // http://t.co/hoge1 -> http://bit.ly/hoge2
125 handler.Queue.Enqueue(async x =>
127 Assert.Equal(HttpMethod.Head, x.Method);
128 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
130 return this.CreateRedirectResponse("http://bit.ly/hoge2");
133 // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
134 handler.Queue.Enqueue(async x =>
136 Assert.Equal(HttpMethod.Head, x.Method);
137 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
139 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
142 // http://tinyurl.com/hoge3 -> http://example.com/hoge4
143 handler.Queue.Enqueue(async x =>
147 return this.CreateRedirectResponse("http://example.com/hoge4");
150 Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
151 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
155 public async Task ExpandUrlAsync_HttpErrorTest()
157 var handler = new HttpMessageHandlerMock();
158 var shortUrl = new ShortUrl(new HttpClient(handler));
160 // http://t.co/hoge1 -> 503 Service Unavailable
161 handler.Queue.Enqueue(async x =>
163 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
166 Assert.Equal(new Uri("http://t.co/hoge1"),
167 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
171 public async Task ExpandUrlHtmlAsync_Test()
173 var handler = new HttpMessageHandlerMock();
174 var shortUrl = new ShortUrl(new HttpClient(handler));
176 // http://t.co/hoge1 -> http://example.com/hoge2
177 handler.Queue.Enqueue(async x =>
179 Assert.Equal(HttpMethod.Head, x.Method);
180 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
182 return this.CreateRedirectResponse("http://example.com/hoge2");
185 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
186 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
189 private HttpResponseMessage CreateRedirectResponse(string uriStr)
191 var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
192 response.Headers.Location = new Uri(uriStr);
197 public async Task ShortenUrlAsync_TinyUrlTest()
199 var handler = new HttpMessageHandlerMock();
200 var shortUrl = new ShortUrl(new HttpClient(handler));
202 handler.Queue.Enqueue(async x =>
204 Assert.Equal(HttpMethod.Post, x.Method);
205 Assert.Equal(new Uri("http://tinyurl.com/api-create.php"), x.RequestUri);
206 Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehoge", await x.Content.ReadAsStringAsync());
208 return new HttpResponseMessage(HttpStatusCode.OK)
210 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
214 Assert.Equal(new Uri("http://tinyurl.com/hoge"),
215 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
219 public async Task ShortenUrlAsync_UxnuUrlTest()
221 var handler = new HttpMessageHandlerMock();
222 var shortUrl = new ShortUrl(new HttpClient(handler));
224 handler.Queue.Enqueue(async x =>
226 Assert.Equal(HttpMethod.Get, x.Method);
227 Assert.Equal("http://ux.nu/api/short?format=plain&url=http://example.com/hogehoge",
228 x.RequestUri.ToString());
230 return new HttpResponseMessage(HttpStatusCode.OK)
232 Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
236 Assert.Equal(new Uri("http://ux.nu/hoge"),
237 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));