OSDN Git Service

テストコード内で ConfigureAwait(false) を使用しない (xUnit1030)
[opentween/open-tween.git] / OpenTween.Tests / Api / BitlyApiTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2017 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.Tasks;
29 using System.Web;
30 using Xunit;
31
32 namespace OpenTween.Api
33 {
34     public class BitlyApiTest
35     {
36         [Fact]
37         public async Task ShortenAsync_OAuth2Test()
38         {
39             using var mockHandler = new HttpMessageHandlerMock();
40             using var http = new HttpClient(mockHandler);
41             var bitly = new BitlyApi(ApiKey.Create("fake_client_id"), ApiKey.Create("fake_client_secret"), http);
42
43             mockHandler.Enqueue(x =>
44             {
45                 Assert.Equal(HttpMethod.Get, x.Method);
46                 Assert.Equal("https://api-ssl.bitly.com/v3/shorten",
47                     x.RequestUri.GetLeftPart(UriPartial.Path));
48
49                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
50
51                 Assert.Equal("http://www.example.com/", query["longUrl"]);
52                 Assert.Equal("bit.ly", query["domain"]);
53                 Assert.Equal("hogehoge", query["access_token"]);
54
55                 return new HttpResponseMessage(HttpStatusCode.OK)
56                 {
57                     Content = new StringContent("http://bit.ly/foo"),
58                 };
59             });
60
61             bitly.EndUserAccessToken = "hogehoge";
62
63             var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly");
64             Assert.Equal("http://bit.ly/foo", result.OriginalString);
65
66             Assert.Equal(0, mockHandler.QueueCount);
67         }
68
69         [Fact]
70         public async Task ShortenAsync_LegacyApiKeyTest()
71         {
72             using var mockHandler = new HttpMessageHandlerMock();
73             using var http = new HttpClient(mockHandler);
74             var bitly = new BitlyApi(ApiKey.Create("fake_client_id"), ApiKey.Create("fake_client_secret"), http);
75
76             mockHandler.Enqueue(x =>
77             {
78                 Assert.Equal(HttpMethod.Get, x.Method);
79                 Assert.Equal("https://api-ssl.bitly.com/v3/shorten",
80                     x.RequestUri.GetLeftPart(UriPartial.Path));
81
82                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
83
84                 Assert.Equal("http://www.example.com/", query["longUrl"]);
85                 Assert.Equal("bit.ly", query["domain"]);
86                 Assert.Equal("username", query["login"]);
87                 Assert.Equal("hogehoge", query["apiKey"]);
88
89                 return new HttpResponseMessage(HttpStatusCode.OK)
90                 {
91                     Content = new StringContent("http://bit.ly/foo"),
92                 };
93             });
94
95             bitly.EndUserLoginName = "username";
96             bitly.EndUserApiKey = "hogehoge";
97
98             var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly");
99             Assert.Equal("http://bit.ly/foo", result.OriginalString);
100
101             Assert.Equal(0, mockHandler.QueueCount);
102         }
103
104         [Fact]
105         public async Task GetAccessTokenAsync_Test()
106         {
107             using var mockHandler = new HttpMessageHandlerMock();
108             using var http = new HttpClient(mockHandler);
109             var bitly = new BitlyApi(ApiKey.Create("fake_client_id"), ApiKey.Create("fake_client_secret"), http);
110
111             mockHandler.Enqueue(async x =>
112             {
113                 Assert.Equal(HttpMethod.Post, x.Method);
114                 Assert.Equal("https://api-ssl.bitly.com/oauth/access_token",
115                     x.RequestUri.GetLeftPart(UriPartial.Path));
116
117                 Assert.Equal("Basic", x.Headers.Authorization.Scheme);
118                 Assert.Equal(
119                     Convert.ToBase64String(Encoding.UTF8.GetBytes("fake_client_id:fake_client_secret")),
120                     x.Headers.Authorization.Parameter
121                 );
122
123                 var body = await x.Content.ReadAsStringAsync();
124                 var query = HttpUtility.ParseQueryString(body);
125
126                 Assert.Equal("password", query["grant_type"]);
127                 Assert.Equal("hogehoge", query["username"]);
128                 Assert.Equal("tetete", query["password"]);
129
130                 return new HttpResponseMessage(HttpStatusCode.OK)
131                 {
132                     Content = new StringContent("""{"access_token": "abcdefg"}"""),
133                 };
134             });
135
136             var result = await bitly.GetAccessTokenAsync("hogehoge", "tetete");
137             Assert.Equal("abcdefg", result);
138
139             Assert.Equal(0, mockHandler.QueueCount);
140         }
141
142         [Fact]
143         public async Task GetAccessTokenAsync_ErrorResponseTest()
144         {
145             using var mockHandler = new HttpMessageHandlerMock();
146             using var http = new HttpClient(mockHandler);
147             var bitly = new BitlyApi(ApiKey.Create("fake_client_id"), ApiKey.Create("fake_client_secret"), http);
148
149             mockHandler.Enqueue(x =>
150             {
151                 return new HttpResponseMessage(HttpStatusCode.OK)
152                 {
153                     Content = new StringContent("""{"status_code": "500", "status_txt": "MISSING_ARG_USERNAME"}"""),
154                 };
155             });
156
157             await Assert.ThrowsAsync<WebApiException>(() => bitly.GetAccessTokenAsync("hogehoge", "tetete"));
158
159             Assert.Equal(0, mockHandler.QueueCount);
160         }
161
162         [Fact]
163         public async Task GetAccessTokenAsync_ApiKeyErrorTest()
164         {
165             using var mockHandler = new HttpMessageHandlerMock();
166             using var http = new HttpClient(mockHandler);
167             var bitly = new BitlyApi(ApiKey.Create("%e%INVALID_API_KEY"), ApiKey.Create("%e%INVALID_API_KEY"), http);
168
169             await Assert.ThrowsAsync<WebApiException>(
170                 () => bitly.GetAccessTokenAsync("hogehoge", "tetete")
171             );
172         }
173     }
174 }