}
[Fact]
- public async Task PostJsonAsync_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);
-
- Assert.Equal("application/json; charset=utf-8", x.Content.Headers.ContentType.ToString());
-
- var body = await x.Content.ReadAsStringAsync();
-
- Assert.Equal("""{"aaaa": 1111}""", body);
-
- return new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent(@"{""ok"":true}"),
- };
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
-
- var response = await apiConnection.PostJsonAsync(endpoint, """{"aaaa": 1111}""");
-
- Assert.Equal(@"{""ok"":true}", response);
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
- public async Task PostJsonAsync_T_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);
-
- Assert.Equal("application/json; charset=utf-8", x.Content.Headers.ContentType.ToString());
-
- var body = await x.Content.ReadAsStringAsync();
-
- Assert.Equal("""{"aaaa": 1111}""", body);
-
- return new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent("\"hogehoge\""),
- };
- });
-
- var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
- var response = await apiConnection.PostJsonAsync<string>(endpoint, """{"aaaa": 1111}""");
- var result = await response.LoadJsonAsync();
-
- Assert.Equal("hogehoge", result);
-
- Assert.Equal(0, mockHandler.QueueCount);
- }
-
- [Fact]
public async Task DeleteAsync_Test()
{
using var mockHandler = new HttpMessageHandlerMock();
}
}
- public async Task<string> PostJsonAsync(Uri uri, string json)
- {
- var request = new PostJsonRequest
- {
- RequestUri = uri,
- JsonString = json,
- };
-
- using var response = await this.SendAsync(request)
- .ConfigureAwait(false);
-
- return await response.ReadAsString()
- .ConfigureAwait(false);
- }
-
- public async Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json)
- {
- var request = new PostJsonRequest
- {
- RequestUri = uri,
- JsonString = json,
- };
-
- using var response = await this.SendAsync(request)
- .ConfigureAwait(false);
-
- return response.ReadAsLazyJson<T>();
- }
-
public async Task DeleteAsync(Uri uri)
{
var requestUri = new Uri(RestApiBase, uri);