OSDN Git Service

HttpClientを使用してShortUrlクラスの実装を修正
[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         class HttpMessageHandlerMock : HttpMessageHandler
38         {
39             public readonly Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>> Queue =
40                 new Queue<Func<HttpRequestMessage, Task<HttpResponseMessage>>>();
41
42             protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
43             {
44                 var handler = this.Queue.Dequeue();
45                 return handler(request);
46             }
47         }
48
49 #pragma warning disable 1998 // awaitが無いasyncラムダ式に対する警告を抑制
50
51         [Fact]
52         public async Task ExpandUrlAsync_Test()
53         {
54             var handler = new HttpMessageHandlerMock();
55             var shortUrl = new ShortUrl(new HttpClient(handler));
56
57             // http://t.co/hoge1 -> http://example.com/hoge2
58             handler.Queue.Enqueue(async x =>
59             {
60                 Assert.Equal(HttpMethod.Head, x.Method);
61                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
62
63                 return this.CreateRedirectResponse("http://example.com/hoge2");
64             });
65
66             Assert.Equal(new Uri("http://example.com/hoge2"),
67                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
68         }
69
70         [Fact]
71         public async Task ExpandUrlAsync_DisableExpandingTest()
72         {
73             var handler = new HttpMessageHandlerMock();
74             var shortUrl = new ShortUrl(new HttpClient(handler));
75
76             shortUrl.DisableExpanding = true;
77
78             // http://t.co/hoge1 -> http://example.com/hoge2
79             handler.Queue.Enqueue(async x =>
80             {
81                 // このリクエストは実行されないはず
82                 Assert.True(false);
83                 return this.CreateRedirectResponse("http://example.com/hoge2");
84             });
85
86             Assert.Equal(new Uri("http://t.co/hoge1"),
87                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
88         }
89
90         [Fact]
91         public async Task ExpandUrlAsync_RecursiveTest()
92         {
93             var handler = new HttpMessageHandlerMock();
94             var shortUrl = new ShortUrl(new HttpClient(handler));
95
96             // http://t.co/hoge1 -> http://bit.ly/hoge2
97             handler.Queue.Enqueue(async x =>
98             {
99                 Assert.Equal(HttpMethod.Head, x.Method);
100                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
101
102                 return this.CreateRedirectResponse("http://bit.ly/hoge2");
103             });
104
105             // http://bit.ly/hoge2 -> http://example.com/hoge3
106             handler.Queue.Enqueue(async x =>
107             {
108                 Assert.Equal(HttpMethod.Head, x.Method);
109                 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
110
111                 return this.CreateRedirectResponse("http://example.com/hoge3");
112             });
113
114             Assert.Equal(new Uri("http://example.com/hoge3"),
115                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
116         }
117
118         [Fact]
119         public async Task ExpandUrlAsync_RecursiveLimitTest()
120         {
121             var handler = new HttpMessageHandlerMock();
122             var shortUrl = new ShortUrl(new HttpClient(handler));
123
124             // http://t.co/hoge1 -> http://bit.ly/hoge2
125             handler.Queue.Enqueue(async x =>
126             {
127                 Assert.Equal(HttpMethod.Head, x.Method);
128                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
129
130                 return this.CreateRedirectResponse("http://bit.ly/hoge2");
131             });
132
133             // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
134             handler.Queue.Enqueue(async x =>
135             {
136                 Assert.Equal(HttpMethod.Head, x.Method);
137                 Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
138
139                 return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
140             });
141
142             // http://tinyurl.com/hoge3 -> http://example.com/hoge4
143             handler.Queue.Enqueue(async x =>
144             {
145                 // このリクエストは実行されないはず
146                 Assert.True(false);
147                 return this.CreateRedirectResponse("http://example.com/hoge4");
148             });
149
150             Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
151                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
152         }
153
154         [Fact]
155         public async Task ExpandUrlAsync_HttpErrorTest()
156         {
157             var handler = new HttpMessageHandlerMock();
158             var shortUrl = new ShortUrl(new HttpClient(handler));
159
160             // http://t.co/hoge1 -> 503 Service Unavailable
161             handler.Queue.Enqueue(async x =>
162             {
163                 return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
164             });
165
166             Assert.Equal(new Uri("http://t.co/hoge1"),
167                 await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
168         }
169
170         [Fact]
171         public async Task ExpandUrlHtmlAsync_Test()
172         {
173             var handler = new HttpMessageHandlerMock();
174             var shortUrl = new ShortUrl(new HttpClient(handler));
175
176             // http://t.co/hoge1 -> http://example.com/hoge2
177             handler.Queue.Enqueue(async x =>
178             {
179                 Assert.Equal(HttpMethod.Head, x.Method);
180                 Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
181
182                 return this.CreateRedirectResponse("http://example.com/hoge2");
183             });
184
185             Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
186                 await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
187         }
188
189         private HttpResponseMessage CreateRedirectResponse(string uriStr)
190         {
191             var response = new HttpResponseMessage(HttpStatusCode.TemporaryRedirect);
192             response.Headers.Location = new Uri(uriStr);
193             return response;
194         }
195
196         [Fact]
197         public async Task ShortenUrlAsync_TinyUrlTest()
198         {
199             var handler = new HttpMessageHandlerMock();
200             var shortUrl = new ShortUrl(new HttpClient(handler));
201
202             handler.Queue.Enqueue(async x =>
203             {
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());
207
208                 return new HttpResponseMessage(HttpStatusCode.OK)
209                 {
210                     Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
211                 };
212             });
213
214             Assert.Equal(new Uri("http://tinyurl.com/hoge"),
215                 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));
216         }
217
218         [Fact]
219         public async Task ShortenUrlAsync_UxnuUrlTest()
220         {
221             var handler = new HttpMessageHandlerMock();
222             var shortUrl = new ShortUrl(new HttpClient(handler));
223
224             handler.Queue.Enqueue(async x =>
225             {
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());
229
230                 return new HttpResponseMessage(HttpStatusCode.OK)
231                 {
232                     Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
233                 };
234             });
235
236             Assert.Equal(new Uri("http://ux.nu/hoge"),
237                 await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));
238         }
239     }
240 }