From 4955bc803b2f436fdb7cd83cfe5ce76e9fe63644 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Sat, 29 Apr 2017 12:36:16 +0900 Subject: [PATCH] =?utf8?q?BitlyApi=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=AE?= =?utf8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?utf8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Api/BitlyApiTest.cs | 173 +++++++++++++++++++++++++++++++++ OpenTween.Tests/OpenTween.Tests.csproj | 1 + 2 files changed, 174 insertions(+) create mode 100644 OpenTween.Tests/Api/BitlyApiTest.cs diff --git a/OpenTween.Tests/Api/BitlyApiTest.cs b/OpenTween.Tests/Api/BitlyApiTest.cs new file mode 100644 index 00000000..1703e15a --- /dev/null +++ b/OpenTween.Tests/Api/BitlyApiTest.cs @@ -0,0 +1,173 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2017 kim_upsilon (@kim_upsilon) +// All rights reserved. +// +// This file is part of OpenTween. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. If not, see , or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using Xunit; + +namespace OpenTween.Api +{ + public class BitlyApiTest + { + [Fact] + public async Task ShortenAsync_OAuth2Test() + { + using (var mockHandler = new HttpMessageHandlerMock()) + using (var http = new HttpClient(mockHandler)) + { + var bitly = new BitlyApi(http); + + mockHandler.Enqueue(x => + { + Assert.Equal(HttpMethod.Get, x.Method); + Assert.Equal("https://api-ssl.bitly.com/v3/shorten", + x.RequestUri.GetLeftPart(UriPartial.Path)); + + var query = HttpUtility.ParseQueryString(x.RequestUri.Query); + + Assert.Equal("http://www.example.com/", query["longUrl"]); + Assert.Equal("bit.ly", query["domain"]); + Assert.Equal("hogehoge", query["access_token"]); + + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("http://bit.ly/foo"), + }; + }); + + bitly.EndUserAccessToken = "hogehoge"; + + var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly") + .ConfigureAwait(false); + Assert.Equal("http://bit.ly/foo", result.OriginalString); + + Assert.Equal(0, mockHandler.QueueCount); + } + } + + [Fact] + public async Task ShortenAsync_LegacyApiKeyTest() + { + using (var mockHandler = new HttpMessageHandlerMock()) + using (var http = new HttpClient(mockHandler)) + { + var bitly = new BitlyApi(http); + + mockHandler.Enqueue(x => + { + Assert.Equal(HttpMethod.Get, x.Method); + Assert.Equal("https://api-ssl.bitly.com/v3/shorten", + x.RequestUri.GetLeftPart(UriPartial.Path)); + + var query = HttpUtility.ParseQueryString(x.RequestUri.Query); + + Assert.Equal("http://www.example.com/", query["longUrl"]); + Assert.Equal("bit.ly", query["domain"]); + Assert.Equal("username", query["login"]); + Assert.Equal("hogehoge", query["apiKey"]); + + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("http://bit.ly/foo"), + }; + }); + + bitly.EndUserLoginName = "username"; + bitly.EndUserApiKey = "hogehoge"; + + var result = await bitly.ShortenAsync(new Uri("http://www.example.com/"), "bit.ly") + .ConfigureAwait(false); + Assert.Equal("http://bit.ly/foo", result.OriginalString); + + Assert.Equal(0, mockHandler.QueueCount); + } + } + + [Fact] + public async Task GetAccessTokenAsync_Test() + { + using (var mockHandler = new HttpMessageHandlerMock()) + using (var http = new HttpClient(mockHandler)) + { + var bitly = new BitlyApi(http); + + mockHandler.Enqueue(async x => + { + Assert.Equal(HttpMethod.Post, x.Method); + Assert.Equal("https://api-ssl.bitly.com/oauth/access_token", + x.RequestUri.GetLeftPart(UriPartial.Path)); + + Assert.Equal("Basic", x.Headers.Authorization.Scheme); + Assert.Equal(ApplicationSettings.BitlyClientId + ":" + ApplicationSettings.BitlyClientSecret, + Encoding.UTF8.GetString(Convert.FromBase64String(x.Headers.Authorization.Parameter))); + + var body = await x.Content.ReadAsStringAsync() + .ConfigureAwait(false); + var query = HttpUtility.ParseQueryString(body); + + Assert.Equal("password", query["grant_type"]); + Assert.Equal("hogehoge", query["username"]); + Assert.Equal("tetete", query["password"]); + + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("{\"access_token\": \"abcdefg\"}"), + }; + }); + + var result = await bitly.GetAccessTokenAsync("hogehoge", "tetete") + .ConfigureAwait(false); + Assert.Equal("abcdefg", result); + + Assert.Equal(0, mockHandler.QueueCount); + } + } + + [Fact] + public async Task GetAccessTokenAsync_ErrorResponseTest() + { + using (var mockHandler = new HttpMessageHandlerMock()) + using (var http = new HttpClient(mockHandler)) + { + var bitly = new BitlyApi(http); + + mockHandler.Enqueue(x => + { + return new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent("{\"status_code\": \"500\", \"status_txt\": \"MISSING_ARG_USERNAME\"}"), + }; + }); + + await Assert.ThrowsAsync(() => bitly.GetAccessTokenAsync("hogehoge", "tetete")) + .ConfigureAwait(false); + + Assert.Equal(0, mockHandler.QueueCount); + } + } + } +} diff --git a/OpenTween.Tests/OpenTween.Tests.csproj b/OpenTween.Tests/OpenTween.Tests.csproj index 16e28b04..e99b467a 100644 --- a/OpenTween.Tests/OpenTween.Tests.csproj +++ b/OpenTween.Tests/OpenTween.Tests.csproj @@ -73,6 +73,7 @@ + -- 2.11.0