}
[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();
}
[Fact]
- public async Task GetStreamAsync_Test()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- using var image = TestUtils.CreateDummyImage();
- 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 ByteArrayContent(image.Stream.ToArray()),
- };
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
- var param = new Dictionary<string, string>
- {
- ["aaaa"] = "1111",
- ["bbbb"] = "2222",
- };
-
- var stream = await apiConnection.GetStreamAsync(endpoint, param);
-
- using (var memoryStream = new MemoryStream())
- {
- // 内容の比較のために MemoryStream にコピー
- await stream.CopyToAsync(memoryStream);
-
- Assert.Equal(image.Stream.ToArray(), memoryStream.ToArray());
- }
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task PostLazyAsync_Test()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- apiConnection.Http = http;
-
- mockHandler.Enqueue(async x =>
- {
- Assert.Equal(HttpMethod.Post, x.Method);
- Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
- x.RequestUri.AbsoluteUri);
-
- var body = await x.Content.ReadAsStringAsync();
- var query = HttpUtility.ParseQueryString(body);
-
- 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.PostLazyAsync<string>(endpoint, param);
-
- Assert.Equal("hogehoge", await result.LoadJsonAsync());
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task PostLazyAsync_MultipartTest()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- apiConnection.HttpUpload = http;
-
- using var image = TestUtils.CreateDummyImage();
- using var media = new MemoryImageMediaItem(image);
-
- mockHandler.Enqueue(async x =>
- {
- Assert.Equal(HttpMethod.Post, x.Method);
- Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
- x.RequestUri.AbsoluteUri);
-
- Assert.IsType<MultipartFormDataContent>(x.Content);
-
- var boundary = x.Content.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 x.Content.ReadAsByteArrayAsync());
-
- 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 mediaParam = new Dictionary<string, IMediaItem>
- {
- ["media1"] = media,
- };
-
- var result = await apiConnection.PostLazyAsync<string>(endpoint, param, mediaParam);
-
- Assert.Equal("hogehoge", await result.LoadJsonAsync());
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task PostLazyAsync_Multipart_NullTest()
- {
- using var mockHandler = new HttpMessageHandlerMock();
- using var http = new HttpClient(mockHandler);
- using var apiConnection = new TwitterApiConnection();
- apiConnection.HttpUpload = http;
-
- mockHandler.Enqueue(async x =>
- {
- Assert.Equal(HttpMethod.Post, x.Method);
- Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
- x.RequestUri.AbsoluteUri);
-
- Assert.IsType<MultipartFormDataContent>(x.Content);
-
- var boundary = x.Content.Headers.ContentType.Parameters.Cast<NameValueHeaderValue>()
- .First(y => y.Name == "boundary").Value;
-
- // 前後のダブルクオーテーションを除去
- boundary = boundary.Substring(1, boundary.Length - 2);
-
- var expectedText =
- $"--{boundary}\r\n" +
- $"\r\n--{boundary}--\r\n";
-
- var expected = Encoding.UTF8.GetBytes(expectedText);
-
- Assert.Equal(expected, await x.Content.ReadAsByteArrayAsync());
-
- return new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent("\"hogehoge\""),
- };
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
-
- var result = await apiConnection.PostLazyAsync<string>(endpoint, param: null, media: null);
-
- Assert.Equal("hogehoge", await result.LoadJsonAsync());
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task DeleteAsync_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.Delete, x.Method);
- Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
- x.RequestUri.AbsoluteUri);
-
- return new HttpResponseMessage(HttpStatusCode.NoContent);
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
-
- await apiConnection.DeleteAsync(endpoint);
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
public async Task HandleTimeout_SuccessTest()
{
static async Task<int> AsyncFunc(CancellationToken token)