OSDN Git Service

IApiConnection, IHttpRequest, ApiResponseで構成する新しいTwitterApiConnectionを実装
[opentween/open-tween.git] / OpenTween.Tests / Connection / ApiResponseTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 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.Net;
23 using System.Net.Http;
24 using System.Runtime.Serialization;
25 using System.Threading.Tasks;
26 using System.Xml.Linq;
27 using OpenTween.Api;
28 using Xunit;
29
30 namespace OpenTween.Connection
31 {
32     public class ApiResponseTest
33     {
34         [Fact]
35         public async Task ReadAsBytes_Test()
36         {
37             using var responseContent = new ByteArrayContent(new byte[] { 1, 2, 3 });
38             using var responseMessage = new HttpResponseMessage
39             {
40                 StatusCode = HttpStatusCode.OK,
41                 Content = responseContent,
42             };
43             using var response = new ApiResponse(responseMessage);
44
45             Assert.Equal(new byte[] { 1, 2, 3 }, await response.ReadAsBytes());
46         }
47
48         [DataContract]
49         public struct TestJson
50         {
51             [DataMember(Name = "foo")]
52             public int Foo { get; set; }
53         }
54
55         [Fact]
56         public async Task ReadAsJson_Test()
57         {
58             using var responseContent = new StringContent("""{"foo":123}""");
59             using var responseMessage = new HttpResponseMessage
60             {
61                 StatusCode = HttpStatusCode.OK,
62                 Content = responseContent,
63             };
64             using var response = new ApiResponse(responseMessage);
65
66             Assert.Equal(new() { Foo = 123 }, await response.ReadAsJson<TestJson>());
67         }
68
69         [Fact]
70         public async Task ReadAsJson_InvalidJsonTest()
71         {
72             using var responseContent = new StringContent("### Invalid JSON Response ###");
73             using var responseMessage = new HttpResponseMessage
74             {
75                 StatusCode = HttpStatusCode.OK,
76                 Content = responseContent,
77             };
78             using var response = new ApiResponse(responseMessage);
79
80             var ex = await Assert.ThrowsAsync<TwitterApiException>(
81                 () => response.ReadAsJson<TestJson>()
82             );
83             Assert.Equal("### Invalid JSON Response ###", ex.ResponseText);
84         }
85
86         [Fact]
87         public async Task ReadAsJsonXml_Test()
88         {
89             using var responseContent = new StringContent("""{"foo":123}""");
90             using var responseMessage = new HttpResponseMessage
91             {
92                 StatusCode = HttpStatusCode.OK,
93                 Content = responseContent,
94             };
95             using var response = new ApiResponse(responseMessage);
96
97             var rootElm = await response.ReadAsJsonXml();
98             var xmlString = rootElm.ToString(SaveOptions.DisableFormatting);
99             Assert.Equal("""<root type="object"><foo type="number">123</foo></root>""", xmlString);
100         }
101
102         [Fact]
103         public async Task ReadAsJsonXml_InvalidJsonTest()
104         {
105             using var responseContent = new StringContent("### Invalid JSON Response ###");
106             using var responseMessage = new HttpResponseMessage
107             {
108                 StatusCode = HttpStatusCode.OK,
109                 Content = responseContent,
110             };
111             using var response = new ApiResponse(responseMessage);
112
113             var ex = await Assert.ThrowsAsync<TwitterApiException>(
114                 () => response.ReadAsJsonXml()
115             );
116             Assert.Equal("### Invalid JSON Response ###", ex.ResponseText);
117         }
118     }
119 }