}
[Fact]
- public async Task GetAsync_Test()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- apiConnection.Http = http;
-
- mockHandler.Enqueue(x =>
- {
- Assert.Equal(HttpMethod.Get, x.Method);
- Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
- x.RequestUri.GetLeftPart(UriPartial.Path));
-
- var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
-
- Assert.Equal("1111", query["aaaa"]);
- Assert.Equal("2222", query["bbbb"]);
-
- return new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent("\"hogehoge\""),
- };
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
- var param = new Dictionary<string, string>
- {
- ["aaaa"] = "1111",
- ["bbbb"] = "2222",
- };
-
- var result = await apiConnection.GetAsync<string>(endpoint, param, endpointName: "/hoge/tetete");
- Assert.Equal("hogehoge", result);
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task GetAsync_AbsoluteUriTest()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- apiConnection.Http = http;
-
- mockHandler.Enqueue(x =>
- {
- Assert.Equal(HttpMethod.Get, x.Method);
- Assert.Equal("http://example.com/hoge/tetete.json",
- x.RequestUri.GetLeftPart(UriPartial.Path));
-
- var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
-
- Assert.Equal("1111", query["aaaa"]);
- Assert.Equal("2222", query["bbbb"]);
-
- return new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent("\"hogehoge\""),
- };
- });
-
- var endpoint = new Uri("http://example.com/hoge/tetete.json", UriKind.Absolute);
- var param = new Dictionary<string, string>
- {
- ["aaaa"] = "1111",
- ["bbbb"] = "2222",
- };
-
- await apiConnection.GetAsync<string>(endpoint, param, endpointName: "/hoge/tetete");
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
public async Task SendAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
}
}
- public async Task<T> GetAsync<T>(Uri uri, IDictionary<string, string>? param, string? endpointName)
- {
- var request = new GetRequest
- {
- RequestUri = uri,
- Query = param,
- EndpointName = endpointName,
- };
-
- using var response = await this.SendAsync(request)
- .ConfigureAwait(false);
-
- return await response.ReadAsJson<T>()
- .ConfigureAwait(false);
- }
-
/// <summary>
/// 指定されたエンドポイントがレートリミット規制中であれば例外を発生させる
/// </summary>