OSDN Git Service

C#7.0で追加されたthrow式の構文を使用する
[opentween/open-tween.git] / OpenTween.Tests / Connection / LazyJsonTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Net.Http;
27 using System.Runtime.Serialization;
28 using System.Text;
29 using System.Threading.Tasks;
30 using OpenTween.Api;
31 using Xunit;
32
33 namespace OpenTween.Connection
34 {
35     public class LazyJsonTest
36     {
37         [Fact]
38         public async Task LoadJsonAsync_Test()
39         {
40             var body = Encoding.UTF8.GetBytes("\"hogehoge\"");
41             using (var bodyStream = new MemoryStream(body))
42             using (var response = new HttpResponseMessage())
43             {
44                 response.Content = new StreamContent(bodyStream);
45
46                 using (var lazyJson = new LazyJson<string>(response))
47                 {
48                     // この時点ではまだレスポンスボディは読まれない
49                     Assert.Equal(0, bodyStream.Position);
50
51                     var result = await lazyJson.LoadJsonAsync()
52                         .ConfigureAwait(false);
53
54                     Assert.Equal("hogehoge", result);
55                 }
56             }
57         }
58
59         [Fact]
60         public async Task LoadJsonAsync_InvalidJsonTest()
61         {
62             var body = Encoding.UTF8.GetBytes("### Invalid JSON ###");
63             using (var bodyStream = new MemoryStream(body))
64             using (var response = new HttpResponseMessage())
65             {
66                 response.Content = new StreamContent(bodyStream);
67
68                 using (var lazyJson = new LazyJson<string>(response))
69                 {
70                     // この時点ではまだレスポンスボディは読まれない
71                     Assert.Equal(0, bodyStream.Position);
72
73                     var exception = await Assert.ThrowsAnyAsync<WebApiException>(() => lazyJson.LoadJsonAsync())
74                         .ConfigureAwait(false);
75
76                     Assert.IsType<SerializationException>(exception.InnerException);
77                 }
78             }
79         }
80
81         [Fact]
82         public async Task IgnoreResponse_Test()
83         {
84             using (var bodyStream = new InvalidStream())
85             using (var response = new HttpResponseMessage())
86             {
87                 // IgnoreResponse() によってレスポンスの Stream が読まれずに破棄されることをテストするため、
88                 // 読み込みが行われると IOException が発生する InvalidStream クラスを bodyStream に使用している
89                 response.Content = new StreamContent(bodyStream);
90
91                 using (var lazyJson = new LazyJson<string>(response))
92                 {
93                     // レスポンスボディを読まずに破棄
94                     await Task.FromResult(lazyJson)
95                         .IgnoreResponse()
96                         .ConfigureAwait(false);
97
98                     Assert.True(bodyStream.IsDisposed);
99                 }
100             }
101         }
102
103         class InvalidStream : Stream
104         {
105             public bool IsDisposed { get; private set; } = false;
106
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
112             {
113                 get { return 0L; }
114                 set { throw new NotSupportedException(); }
115             }
116
117             public override void Flush()
118             {
119                 throw new NotSupportedException();
120             }
121
122             public override int Read(byte[] buffer, int offset, int count)
123             {
124                 throw new IOException();
125             }
126
127             public override long Seek(long offset, SeekOrigin origin)
128             {
129                 throw new NotSupportedException();
130             }
131
132             public override void SetLength(long value)
133             {
134                 throw new NotSupportedException();
135             }
136
137             public override void Write(byte[] buffer, int offset, int count)
138             {
139                 throw new NotSupportedException();
140             }
141
142             protected override void Dispose(bool disposing)
143             {
144                 base.Dispose(disposing);
145                 this.IsDisposed = true;
146             }
147         }
148     }
149 }