1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Net.Http;
27 using System.Runtime.Serialization;
29 using System.Threading.Tasks;
33 namespace OpenTween.Connection
35 public class LazyJsonTest
38 public async Task LoadJsonAsync_Test()
40 var body = Encoding.UTF8.GetBytes("\"hogehoge\"");
41 using (var bodyStream = new MemoryStream(body))
42 using (var response = new HttpResponseMessage())
44 response.Content = new StreamContent(bodyStream);
46 using (var lazyJson = new LazyJson<string>(response))
48 // この時点ではまだレスポンスボディは読まれない
49 Assert.Equal(0, bodyStream.Position);
51 var result = await lazyJson.LoadJsonAsync()
52 .ConfigureAwait(false);
54 Assert.Equal("hogehoge", result);
60 public async Task LoadJsonAsync_InvalidJsonTest()
62 var body = Encoding.UTF8.GetBytes("### Invalid JSON ###");
63 using (var bodyStream = new MemoryStream(body))
64 using (var response = new HttpResponseMessage())
66 response.Content = new StreamContent(bodyStream);
68 using (var lazyJson = new LazyJson<string>(response))
70 // この時点ではまだレスポンスボディは読まれない
71 Assert.Equal(0, bodyStream.Position);
73 var exception = await Assert.ThrowsAnyAsync<WebApiException>(() => lazyJson.LoadJsonAsync())
74 .ConfigureAwait(false);
76 Assert.IsType<SerializationException>(exception.InnerException);
82 public async Task IgnoreResponse_Test()
84 using (var bodyStream = new InvalidStream())
85 using (var response = new HttpResponseMessage())
87 // IgnoreResponse() によってレスポンスの Stream が読まれずに破棄されることをテストするため、
88 // 読み込みが行われると IOException が発生する InvalidStream クラスを bodyStream に使用している
89 response.Content = new StreamContent(bodyStream);
91 using (var lazyJson = new LazyJson<string>(response))
94 await Task.FromResult(lazyJson)
96 .ConfigureAwait(false);
98 Assert.True(bodyStream.IsDisposed);
103 class InvalidStream : Stream
105 public bool IsDisposed { get; private set; } = false;
107 public override bool CanRead => true;
108 public override bool CanSeek => false;
109 public override bool CanWrite => false;
110 public override long Length => 100L;
111 public override long Position
114 set { throw new NotSupportedException(); }
117 public override void Flush()
119 throw new NotSupportedException();
122 public override int Read(byte[] buffer, int offset, int count)
124 throw new IOException();
127 public override long Seek(long offset, SeekOrigin origin)
129 throw new NotSupportedException();
132 public override void SetLength(long value)
134 throw new NotSupportedException();
137 public override void Write(byte[] buffer, int offset, int count)
139 throw new NotSupportedException();
142 protected override void Dispose(bool disposing)
144 base.Dispose(disposing);
145 this.IsDisposed = true;