Skip to content

Commit

Permalink
Merge pull request #273 from opentween/api-connection
Browse files Browse the repository at this point in the history
IApiConnectionインタフェースを再設計
  • Loading branch information
upsilon authored Dec 8, 2023
2 parents efa4767 + c3e2600 commit c05ddf7
Show file tree
Hide file tree
Showing 36 changed files with 1,045 additions and 352 deletions.
2 changes: 1 addition & 1 deletion OpenTween.Tests/Api/GraphQL/CreateRetweetRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Send_Test()
{
var responseText = File.ReadAllText("Resources/Responses/CreateRetweet.json");

var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand Down
6 changes: 3 additions & 3 deletions OpenTween.Tests/Api/GraphQL/CreateTweetRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Send_Test()
{
var responseText = File.ReadAllText("Resources/Responses/CreateTweet_CircleTweet.json");

var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task Send_ReplyTest()
{
var responseText = File.ReadAllText("Resources/Responses/CreateTweet_CircleTweet.json");

var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand All @@ -92,7 +92,7 @@ public async Task Send_MediaTest()
{
var responseText = File.ReadAllText("Resources/Responses/CreateTweet_CircleTweet.json");

var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand Down
2 changes: 1 addition & 1 deletion OpenTween.Tests/Api/GraphQL/DeleteRetweetRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DeleteRetweetRequestTest
[Fact]
public async Task Send_Test()
{
var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand Down
2 changes: 1 addition & 1 deletion OpenTween.Tests/Api/GraphQL/DeleteTweetRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DeleteTweetRequestTest
[Fact]
public async Task Send_Test()
{
var mock = new Mock<IApiConnection>();
var mock = new Mock<IApiConnectionLegacy>();
mock.Setup(x =>
x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
)
Expand Down
46 changes: 22 additions & 24 deletions OpenTween.Tests/Api/GraphQL/ListLatestTweetsTimelineRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using OpenTween.Api.TwitterV2;
using OpenTween.Connection;
using Xunit;

Expand All @@ -37,21 +31,23 @@ public class ListLatestTweetsTimelineRequestTest
[Fact]
public async Task Send_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/ListLatestTweetsTimeline_SimpleTweet.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/ListLatestTweetsTimeline_SimpleTweet.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/6ClPnsuzQJ1p7-g32GQw9Q/ListLatestTweetsTimeline"), url);
Assert.Equal(2, param.Count);
Assert.Equal("""{"listId":"1675863884757110790","count":20}""", param["variables"]);
Assert.True(param.ContainsKey("features"));
Assert.Equal("ListLatestTweetsTimeline", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/6ClPnsuzQJ1p7-g32GQw9Q/ListLatestTweetsTimeline"), request.RequestUri);
var query = request.Query!;
Assert.Equal(2, query.Count);
Assert.Equal("""{"listId":"1675863884757110790","count":20}""", query["variables"]);
Assert.True(query.ContainsKey("features"));
Assert.Equal("ListLatestTweetsTimeline", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new ListLatestTweetsTimelineRequest(listId: "1675863884757110790")
{
Expand All @@ -69,21 +65,23 @@ public async Task Send_Test()
[Fact]
public async Task Send_RequestCursor_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/ListLatestTweetsTimeline_SimpleTweet.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/ListLatestTweetsTimeline_SimpleTweet.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/6ClPnsuzQJ1p7-g32GQw9Q/ListLatestTweetsTimeline"), url);
Assert.Equal(2, param.Count);
Assert.Equal("""{"listId":"1675863884757110790","count":20,"cursor":"aaa"}""", param["variables"]);
Assert.True(param.ContainsKey("features"));
Assert.Equal("ListLatestTweetsTimeline", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/6ClPnsuzQJ1p7-g32GQw9Q/ListLatestTweetsTimeline"), request.RequestUri);
var query = request.Query!;
Assert.Equal(2, query.Count);
Assert.Equal("""{"listId":"1675863884757110790","count":20,"cursor":"aaa"}""", query["variables"]);
Assert.True(query.ContainsKey("features"));
Assert.Equal("ListLatestTweetsTimeline", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new ListLatestTweetsTimelineRequest(listId: "1675863884757110790")
{
Expand Down
45 changes: 22 additions & 23 deletions OpenTween.Tests/Api/GraphQL/SearchTimelineRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using OpenTween.Connection;
Expand All @@ -36,21 +31,23 @@ public class SearchTimelineRequestTest
[Fact]
public async Task Send_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/SearchTimeline_SimpleTweet.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/SearchTimeline_SimpleTweet.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/lZ0GCEojmtQfiUQa5oJSEw/SearchTimeline"), url);
Assert.Equal(2, param.Count);
Assert.Equal("""{"rawQuery":"#OpenTween","count":20,"product":"Latest"}""", param["variables"]);
Assert.True(param.ContainsKey("features"));
Assert.Equal("SearchTimeline", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/lZ0GCEojmtQfiUQa5oJSEw/SearchTimeline"), request.RequestUri);
var query = request.Query!;
Assert.Equal(2, query.Count);
Assert.Equal("""{"rawQuery":"#OpenTween","count":20,"product":"Latest"}""", query["variables"]);
Assert.True(query.ContainsKey("features"));
Assert.Equal("SearchTimeline", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new SearchTimelineRequest(rawQuery: "#OpenTween")
{
Expand All @@ -68,21 +65,23 @@ public async Task Send_Test()
[Fact]
public async Task Send_RequestCursor_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/SearchTimeline_SimpleTweet.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/SearchTimeline_SimpleTweet.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/lZ0GCEojmtQfiUQa5oJSEw/SearchTimeline"), url);
Assert.Equal(2, param.Count);
Assert.Equal("""{"rawQuery":"#OpenTween","count":20,"product":"Latest","cursor":"aaa"}""", param["variables"]);
Assert.True(param.ContainsKey("features"));
Assert.Equal("SearchTimeline", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/lZ0GCEojmtQfiUQa5oJSEw/SearchTimeline"), request.RequestUri);
var query = request.Query!;
Assert.Equal(2, query.Count);
Assert.Equal("""{"rawQuery":"#OpenTween","count":20,"product":"Latest","cursor":"aaa"}""", query["variables"]);
Assert.True(query.ContainsKey("features"));
Assert.Equal("SearchTimeline", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new SearchTimelineRequest(rawQuery: "#OpenTween")
{
Expand Down
21 changes: 9 additions & 12 deletions OpenTween.Tests/Api/GraphQL/TweetDetailRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using OpenTween.Api.TwitterV2;
using OpenTween.Connection;
using Xunit;

Expand All @@ -37,19 +32,21 @@ public class TweetDetailRequestTest
[Fact]
public async Task Send_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/TweetDetail.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/TweetDetail.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/-Ls3CrSQNo2fRKH6i6Na1A/TweetDetail"), url);
Assert.Contains(@"""focalTweetId"":""1619433164757413894""", param["variables"]);
Assert.Equal("TweetDetail", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/-Ls3CrSQNo2fRKH6i6Na1A/TweetDetail"), request.RequestUri);
var query = request.Query!;
Assert.Contains(@"""focalTweetId"":""1619433164757413894""", query["variables"]);
Assert.Equal("TweetDetail", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new TweetDetailRequest
{
Expand Down
27 changes: 12 additions & 15 deletions OpenTween.Tests/Api/GraphQL/UserByScreenNameRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using OpenTween.Connection;
Expand All @@ -36,19 +31,21 @@ public class UserByScreenNameRequestTest
[Fact]
public async Task Send_Test()
{
using var responseStream = File.OpenRead("Resources/Responses/UserByScreenName.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/UserByScreenName.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.Callback<Uri, IDictionary<string, string>, string>((url, param, endpointName) =>
.Callback<IHttpRequest>(x =>
{
Assert.Equal(new("https://twitter.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName"), url);
Assert.Contains(@"""screen_name"":""opentween""", param["variables"]);
Assert.Equal("UserByScreenName", endpointName);
var request = Assert.IsType<GetRequest>(x);
Assert.Equal(new("https://twitter.com/i/api/graphql/xc8f1g7BYqr6VTzTbvNlGw/UserByScreenName"), request.RequestUri);
var query = request.Query!;
Assert.Contains(@"""screen_name"":""opentween""", query["variables"]);
Assert.Equal("UserByScreenName", request.EndpointName);
})
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new UserByScreenNameRequest
{
Expand All @@ -64,13 +61,13 @@ public async Task Send_Test()
[Fact]
public async Task Send_UserUnavailableTest()
{
using var responseStream = File.OpenRead("Resources/Responses/UserByScreenName_Suspended.json");
using var apiResponse = await TestUtils.CreateApiResponse("Resources/Responses/UserByScreenName_Suspended.json");

var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetStreamAsync(It.IsAny<Uri>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<string>())
x.SendAsync(It.IsAny<IHttpRequest>())
)
.ReturnsAsync(responseStream);
.ReturnsAsync(apiResponse);

var request = new UserByScreenNameRequest
{
Expand Down
Loading

0 comments on commit c05ddf7

Please sign in to comment.