Skip to content

Commit

Permalink
Merge pull request #275 from opentween/api-connection
Browse files Browse the repository at this point in the history
ITwitterCredentialとアクセス手段ごとの具象クラスを追加
  • Loading branch information
upsilon authored Dec 10, 2023
2 parents d44e636 + 043b57e commit 02b8f6b
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 258 deletions.
123 changes: 60 additions & 63 deletions OpenTween.Tests/Api/TwitterApiTest.cs

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task GetAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task GetAsync_AbsoluteUriTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task SendAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -175,7 +175,7 @@ public async Task SendAsync_UpdateRateLimitTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -219,7 +219,7 @@ public async Task SendAsync_ErrorStatusTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -251,7 +251,7 @@ public async Task SendAsync_ErrorJsonTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down Expand Up @@ -285,7 +285,7 @@ public async Task GetStreamAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
using var image = TestUtils.CreateDummyImage();
apiConnection.Http = http;

Expand Down Expand Up @@ -331,7 +331,7 @@ public async Task PostLazyAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(async x =>
Expand Down Expand Up @@ -371,7 +371,7 @@ public async Task PostLazyAsync_MultipartTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.HttpUpload = http;

using var image = TestUtils.CreateDummyImage();
Expand Down Expand Up @@ -441,7 +441,7 @@ public async Task PostLazyAsync_Multipart_NullTest()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.HttpUpload = http;

mockHandler.Enqueue(async x =>
Expand Down Expand Up @@ -486,7 +486,7 @@ public async Task PostJsonAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(async x =>
Expand Down Expand Up @@ -520,7 +520,7 @@ public async Task PostJsonAsync_T_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(async x =>
Expand Down Expand Up @@ -555,7 +555,7 @@ public async Task DeleteAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
using var http = new HttpClient(mockHandler);
using var apiConnection = new TwitterApiConnection(ApiKey.Create(""), ApiKey.Create(""), "", "");
using var apiConnection = new TwitterApiConnection();
apiConnection.Http = http;

mockHandler.Enqueue(x =>
Expand Down
47 changes: 47 additions & 0 deletions OpenTween.Tests/Connection/TwitterCredentialCookieTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~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 <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System.Net.Http;
using Xunit;

namespace OpenTween.Connection
{
public class TwitterCredentialCookieTest
{
[Fact]
public void CreateHttpHandler_Test()
{
var appToken = new TwitterAppToken
{
AuthType = APIAuthType.TwitterComCookie,
TwitterComCookie = "aaa=bbb",
};
var credential = new TwitterCredentialCookie(appToken);

using var innerHandler = new HttpClientHandler();
using var handler = credential.CreateHttpHandler(innerHandler);

var cookieHandler = Assert.IsType<TwitterComCookieHandler>(handler);
Assert.Equal("aaa=bbb", cookieHandler.RawCookie);
Assert.Same(innerHandler, cookieHandler.InnerHandler);
}
}
}
51 changes: 51 additions & 0 deletions OpenTween.Tests/Connection/TwitterCredentialOAuth1Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// OpenTween - Client of Twitter
// Copyright (c) 2023 kim_upsilon (@kim_upsilon) <https://upsilo.net/~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 <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System.Net.Http;
using Xunit;

namespace OpenTween.Connection
{
public class TwitterCredentialOAuth1Test
{
[Fact]
public void CreateHttpHandler_Test()
{
var appToken = new TwitterAppToken
{
AuthType = APIAuthType.OAuth1,
OAuth1CustomConsumerKey = ApiKey.Create("consumer_key"),
OAuth1CustomConsumerSecret = ApiKey.Create("consumer_secret"),
};
var credential = new TwitterCredentialOAuth1(appToken, "access_token", "access_secret");

using var innerHandler = new HttpClientHandler();
using var handler = credential.CreateHttpHandler(innerHandler);

var oauthHandler = Assert.IsType<OAuthHandler>(handler);
Assert.Equal("consumer_key", oauthHandler.ConsumerKey.Value);
Assert.Equal("consumer_secret", oauthHandler.ConsumerSecret.Value);
Assert.Equal("access_token", oauthHandler.AccessToken);
Assert.Equal("access_secret", oauthHandler.AccessSecret);
Assert.Same(innerHandler, oauthHandler.InnerHandler);
}
}
}
Loading

0 comments on commit 02b8f6b

Please sign in to comment.