Skip to content

Commit

Permalink
Merge pull request #280 from opentween/api-connection
Browse files Browse the repository at this point in the history
IApiConnectionLegacyからIApiConnectionへ移行
  • Loading branch information
upsilon authored Dec 12, 2023
2 parents e89e514 + 4152707 commit 188ae1b
Show file tree
Hide file tree
Showing 11 changed files with 1,533 additions and 1,235 deletions.
1,254 changes: 664 additions & 590 deletions OpenTween.Tests/Api/TwitterApiTest.cs

Large diffs are not rendered by default.

41 changes: 28 additions & 13 deletions OpenTween.Tests/Api/TwitterV2/GetTimelineRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Moq;
using OpenTween.Api.DataModel;
Expand All @@ -32,23 +34,36 @@ namespace OpenTween.Api.TwitterV2
public class GetTimelineRequestTest
{
[Fact]
public async Task StatusesMentionsTimeline_Test()
public async Task Send_Test()
{
var mock = new Mock<IApiConnectionLegacy>();
Func<GetRequest, bool> verifyRequest = r =>
{
Assert.Equal(new("/2/users/100/timelines/reverse_chronological", UriKind.Relative), r.RequestUri);
var expectedQuery = new Dictionary<string, string>
{
{ "tweet.fields", "id" },
{ "max_results", "200" },
{ "until_id", "900" },
{ "since_id", "100" },
};
Assert.Equal(expectedQuery, r.Query);
Assert.Equal("/2/users/:id/timelines/reverse_chronological", r.EndpointName);
return true;
};

using var responseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(
JsonUtils.SerializeJsonByDataContract(new TwitterV2TweetIds())
),
};
var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetAsync<TwitterV2TweetIds>(
new Uri("/2/users/100/timelines/reverse_chronological", UriKind.Relative),
new Dictionary<string, string>
{
{ "tweet.fields", "id" },
{ "max_results", "200" },
{ "until_id", "900" },
{ "since_id", "100" },
},
"/2/users/:id/timelines/reverse_chronological"
x.SendAsync(
It.Is<GetRequest>(r => verifyRequest(r))
)
)
.ReturnsAsync(new TwitterV2TweetIds());
.ReturnsAsync(new ApiResponse(responseMessage));

var request = new GetTimelineRequest(userId: 100L)
{
Expand Down
90 changes: 90 additions & 0 deletions OpenTween.Tests/Connection/PostMultipartRequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace OpenTween.Connection
{
public class PostMultipartRequestTest
{
[Fact]
public async Task CreateMessage_Test()
{
using var image = TestUtils.CreateDummyImage();
using var media = new MemoryImageMediaItem(image);

var request = new PostMultipartRequest
{
RequestUri = new("hoge/aaa.json", UriKind.Relative),
Query = new Dictionary<string, string>
{
["aaaa"] = "1111",
["bbbb"] = "2222",
},
Media = new Dictionary<string, IMediaItem>
{
["media1"] = media,
},
};

var baseUri = new Uri("https://example.com/v1/");
using var requestMessage = request.CreateMessage(baseUri);

Assert.Equal(HttpMethod.Post, requestMessage.Method);
Assert.Equal(new("https://example.com/v1/hoge/aaa.json"), requestMessage.RequestUri);

using var requestContent = Assert.IsType<MultipartFormDataContent>(requestMessage.Content);
var boundary = requestContent.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>()
.First(y => y.Name == "boundary").Value;

// 前後のダブルクオーテーションを除去
boundary = boundary.Substring(1, boundary.Length - 2);

var expectedText =
$"--{boundary}\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"Content-Disposition: form-data; name=aaaa\r\n" +
"\r\n" +
"1111\r\n" +
$"--{boundary}\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"Content-Disposition: form-data; name=bbbb\r\n" +
"\r\n" +
"2222\r\n" +
$"--{boundary}\r\n" +
$"Content-Disposition: form-data; name=media1; filename={media.Name}; filename*=utf-8''{media.Name}\r\n" +
"\r\n";

var expected = Encoding.UTF8.GetBytes(expectedText)
.Concat(image.Stream.ToArray())
.Concat(Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n"));

Assert.Equal(expected, await requestContent.ReadAsByteArrayAsync());
}
}
}
70 changes: 70 additions & 0 deletions OpenTween.Tests/Connection/PostRequestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;

namespace OpenTween.Connection
{
public class PostRequestTest
{
[Fact]
public void CreateMessage_Test()
{
var request = new PostRequest
{
RequestUri = new("hoge/aaa.json", UriKind.Relative),
};

var baseUri = new Uri("https://example.com/v1/");
using var requestMessage = request.CreateMessage(baseUri);

Assert.Equal(HttpMethod.Post, requestMessage.Method);
Assert.Equal(new("https://example.com/v1/hoge/aaa.json"), requestMessage.RequestUri);
Assert.Null(requestMessage.Content);
}

[Fact]
public async Task CreateMessage_WithQueryTest()
{
var request = new PostRequest
{
RequestUri = new("hoge/aaa.json", UriKind.Relative),
Query = new Dictionary<string, string>
{
["id"] = "12345",
},
};

var baseUri = new Uri("https://example.com/v1/");
using var requestMessage = request.CreateMessage(baseUri);

Assert.Equal(HttpMethod.Post, requestMessage.Method);
Assert.Equal(new("https://example.com/v1/hoge/aaa.json"), requestMessage.RequestUri);

using var requestContent = Assert.IsType<FormUrlEncodedContent>(requestMessage.Content);
Assert.Equal("id=12345", await requestContent.ReadAsStringAsync());
}
}
}
Loading

0 comments on commit 188ae1b

Please sign in to comment.