OSDN Git Service

using var を使用する
[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(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                 .ConfigureAwait(false);
65             Assert.Equal("http://bit.ly/foo", result.OriginalString);
66
67             Assert.Equal(0, mockHandler.QueueCount);
68         }
69
70         [Fact]
71         public async Task ShortenAsync_LegacyApiKeyTest()
72         {
73             using var mockHandler = new HttpMessageHandlerMock();
74             using var http = new HttpClient(mockHandler);
75             var bitly = new BitlyApi(http);
76
77             mockHandler.Enqueue(x =>
78             {
79                 Assert.Equal(HttpMethod.Get, x.Method);
80                 Assert.Equal("https://api-ssl.bitly.com/v3/shorten",
81                     x.RequestUri.GetLeftPart(UriPartial.Path));
82
83                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
84
85                 Assert.Equal("http://www.example.com/", query["longUrl"]);
86                 Assert.Equal("bit.ly", query["domain"]);
87                 Assert.Equal("username", query["login"]);
88                 Assert.Equal("hogehoge", query["apiKey"]);
89
90                 return new HttpResponseMessage(HttpStatusCode.OK)
91                 {
92                     Content = new StringContent("http://bit.ly/foo"),
93                 };
94             });
95
96             bitly.EndUserLoginName = "username";
97             bitly.EndUserApiKey = "hogehoge";
98
99             var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly")
100                 .ConfigureAwait(false);
101             Assert.Equal("http://bit.ly/foo", result.OriginalString);
102
103             Assert.Equal(0, mockHandler.QueueCount);
104         }
105
106         [Fact]
107         public async Task GetAccessTokenAsync_Test()
108         {
109             using var mockHandler = new HttpMessageHandlerMock();
110             using var http = new HttpClient(mockHandler);
111             var bitly = new BitlyApi(http);
112
113             mockHandler.Enqueue(async x =>
114             {
115                 Assert.Equal(HttpMethod.Post, x.Method);
116                 Assert.Equal("https://api-ssl.bitly.com/oauth/access_token",
117                     x.RequestUri.GetLeftPart(UriPartial.Path));
118
119                 Assert.Equal("Basic", x.Headers.Authorization.Scheme);
120                 Assert.Equal(ApplicationSettings.BitlyClientId + ":" + ApplicationSettings.BitlyClientSecret,
121                     Encoding.UTF8.GetString(Convert.FromBase64String(x.Headers.Authorization.Parameter)));
122
123                 var body = await x.Content.ReadAsStringAsync()
124                     .ConfigureAwait(false);
125                 var query = HttpUtility.ParseQueryString(body);
126
127                 Assert.Equal("password", query["grant_type"]);
128                 Assert.Equal("hogehoge", query["username"]);
129                 Assert.Equal("tetete", query["password"]);
130
131                 return new HttpResponseMessage(HttpStatusCode.OK)
132                 {
133                     Content = new StringContent("{\"access_token\": \"abcdefg\"}"),
134                 };
135             });
136
137             var result = await bitly.GetAccessTokenAsync("hogehoge", "tetete")
138                 .ConfigureAwait(false);
139             Assert.Equal("abcdefg", result);
140
141             Assert.Equal(0, mockHandler.QueueCount);
142         }
143
144         [Fact]
145         public async Task GetAccessTokenAsync_ErrorResponseTest()
146         {
147             using var mockHandler = new HttpMessageHandlerMock();
148             using var http = new HttpClient(mockHandler);
149             var bitly = new BitlyApi(http);
150
151             mockHandler.Enqueue(x =>
152             {
153                 return new HttpResponseMessage(HttpStatusCode.OK)
154                 {
155                     Content = new StringContent("{\"status_code\": \"500\", \"status_txt\": \"MISSING_ARG_USERNAME\"}"),
156                 };
157             });
158
159             await Assert.ThrowsAsync<WebApiException>(() => bitly.GetAccessTokenAsync("hogehoge", "tetete"))
160                 .ConfigureAwait(false);
161
162             Assert.Equal(0, mockHandler.QueueCount);
163         }
164     }
165 }