);
Assert.Equal("### Invalid JSON Response ###", ex.ResponseText);
}
+
+ [Fact]
+ public async Task ReadAsLazyJson_Test()
+ {
+ using var responseContent = new StringContent("""{"foo":123}""");
+ using var responseMessage = new HttpResponseMessage
+ {
+ StatusCode = HttpStatusCode.OK,
+ Content = responseContent,
+ };
+ using var response = new ApiResponse(responseMessage);
+
+ using var lazyJson = response.ReadAsLazyJson<TestJson>();
+ Assert.Equal(new() { Foo = 123 }, await lazyJson.LoadJsonAsync());
+ }
+
+ [Fact]
+ public async Task ReadAsLazyJson_DisposeTest()
+ {
+ using var responseContent = new StringContent("""{"foo":123}""");
+ using var responseMessage = new HttpResponseMessage
+ {
+ StatusCode = HttpStatusCode.OK,
+ Content = responseContent,
+ };
+ using var response = new ApiResponse(responseMessage);
+ using var lazyJson = response.ReadAsLazyJson<TestJson>();
+ response.Dispose(); // ApiResponse を先に破棄しても LazyJson に影響しないことをテストする
+
+ Assert.Equal(new() { Foo = 123 }, await lazyJson.LoadJsonAsync());
+ }
}
}
public bool IsDisposed { get; private set; }
private readonly HttpResponseMessage responseMessage;
+ private bool preventDisposeResponse;
public ApiResponse(HttpResponseMessage responseMessage)
=> this.responseMessage = responseMessage;
if (this.IsDisposed)
return;
- this.responseMessage.Dispose();
+ if (!this.preventDisposeResponse)
+ this.responseMessage.Dispose();
+
this.IsDisposed = true;
}
throw new TwitterApiException("Invalid JSON", ex) { ResponseText = responseText };
}
}
+
+ public LazyJson<T> ReadAsLazyJson<T>()
+ {
+ this.preventDisposeResponse = true;
+
+ return new(this.responseMessage);
+ }
}
}