1 // OpenTween - Client of Twitter
2 // Copyright (c) 2017 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.Tasks;
32 namespace OpenTween.Api
34 public class BitlyApiTest
37 public async Task ShortenAsync_OAuth2Test()
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);
43 mockHandler.Enqueue(x =>
45 Assert.Equal(HttpMethod.Get, x.Method);
46 Assert.Equal("https://api-ssl.bitly.com/v3/shorten",
47 x.RequestUri.GetLeftPart(UriPartial.Path));
49 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
51 Assert.Equal("http://www.example.com/", query["longUrl"]);
52 Assert.Equal("bit.ly", query["domain"]);
53 Assert.Equal("hogehoge", query["access_token"]);
55 return new HttpResponseMessage(HttpStatusCode.OK)
57 Content = new StringContent("http://bit.ly/foo"),
61 bitly.EndUserAccessToken = "hogehoge";
63 var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly");
64 Assert.Equal("http://bit.ly/foo", result.OriginalString);
66 Assert.Equal(0, mockHandler.QueueCount);
70 public async Task ShortenAsync_LegacyApiKeyTest()
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);
76 mockHandler.Enqueue(x =>
78 Assert.Equal(HttpMethod.Get, x.Method);
79 Assert.Equal("https://api-ssl.bitly.com/v3/shorten",
80 x.RequestUri.GetLeftPart(UriPartial.Path));
82 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
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"]);
89 return new HttpResponseMessage(HttpStatusCode.OK)
91 Content = new StringContent("http://bit.ly/foo"),
95 bitly.EndUserLoginName = "username";
96 bitly.EndUserApiKey = "hogehoge";
98 var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly");
99 Assert.Equal("http://bit.ly/foo", result.OriginalString);
101 Assert.Equal(0, mockHandler.QueueCount);
105 public async Task GetAccessTokenAsync_Test()
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);
111 mockHandler.Enqueue(async x =>
113 Assert.Equal(HttpMethod.Post, x.Method);
114 Assert.Equal("https://api-ssl.bitly.com/oauth/access_token",
115 x.RequestUri.GetLeftPart(UriPartial.Path));
117 Assert.Equal("Basic", x.Headers.Authorization.Scheme);
119 Convert.ToBase64String(Encoding.UTF8.GetBytes("fake_client_id:fake_client_secret")),
120 x.Headers.Authorization.Parameter
123 var body = await x.Content.ReadAsStringAsync();
124 var query = HttpUtility.ParseQueryString(body);
126 Assert.Equal("password", query["grant_type"]);
127 Assert.Equal("hogehoge", query["username"]);
128 Assert.Equal("tetete", query["password"]);
130 return new HttpResponseMessage(HttpStatusCode.OK)
132 Content = new StringContent("""{"access_token": "abcdefg"}"""),
136 var result = await bitly.GetAccessTokenAsync("hogehoge", "tetete");
137 Assert.Equal("abcdefg", result);
139 Assert.Equal(0, mockHandler.QueueCount);
143 public async Task GetAccessTokenAsync_ErrorResponseTest()
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);
149 mockHandler.Enqueue(x =>
151 return new HttpResponseMessage(HttpStatusCode.OK)
153 Content = new StringContent("""{"status_code": "500", "status_txt": "MISSING_ARG_USERNAME"}"""),
157 await Assert.ThrowsAsync<WebApiException>(() => bitly.GetAccessTokenAsync("hogehoge", "tetete"));
159 Assert.Equal(0, mockHandler.QueueCount);
163 public async Task GetAccessTokenAsync_ApiKeyErrorTest()
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);
169 await Assert.ThrowsAsync<WebApiException>(
170 () => bitly.GetAccessTokenAsync("hogehoge", "tetete")