Beispiel #1
0
        public async Task LoadJsonAsync_InvalidJsonTest()
        {
            var body = Encoding.UTF8.GetBytes("### Invalid JSON ###");

            using var bodyStream = new MemoryStream(body);
            using var response   = new HttpResponseMessage();
            response.Content     = new StreamContent(bodyStream);

            using var lazyJson = new LazyJson <string>(response);

            // この時点ではまだレスポンスボディは読まれない
            Assert.Equal(0, bodyStream.Position);

            var exception = await Assert.ThrowsAnyAsync <WebApiException>(() => lazyJson.LoadJsonAsync())
                            .ConfigureAwait(false);

            Assert.IsType <SerializationException>(exception.InnerException);
        }
Beispiel #2
0
        public async Task IgnoreResponse_Test()
        {
            using var bodyStream = new InvalidStream();
            using var response   = new HttpResponseMessage();

            // IgnoreResponse() によってレスポンスの Stream が読まれずに破棄されることをテストするため、
            // 読み込みが行われると IOException が発生する InvalidStream クラスを bodyStream に使用している
            response.Content = new StreamContent(bodyStream);

            using var lazyJson = new LazyJson <string>(response);

            // レスポンスボディを読まずに破棄
            await Task.FromResult(lazyJson)
            .IgnoreResponse()
            .ConfigureAwait(false);

            Assert.True(bodyStream.IsDisposed);
        }
Beispiel #3
0
        public async Task LoadJsonAsync_Test()
        {
            var body = Encoding.UTF8.GetBytes("\"hogehoge\"");

            using var bodyStream = new MemoryStream(body);
            using var response   = new HttpResponseMessage();
            response.Content     = new StreamContent(bodyStream);

            using var lazyJson = new LazyJson <string>(response);

            // この時点ではまだレスポンスボディは読まれない
            Assert.Equal(0, bodyStream.Position);

            var result = await lazyJson.LoadJsonAsync()
                         .ConfigureAwait(false);

            Assert.Equal("hogehoge", result);
        }