1 // OpenTween - Client of Twitter
2 // Copyright (c) 2023 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.Net.Http;
24 using System.Runtime.Serialization;
25 using System.Threading.Tasks;
26 using System.Xml.Linq;
30 namespace OpenTween.Connection
32 public class ApiResponseTest
35 public async Task ReadAsBytes_Test()
37 using var responseContent = new ByteArrayContent(new byte[] { 1, 2, 3 });
38 using var responseMessage = new HttpResponseMessage
40 StatusCode = HttpStatusCode.OK,
41 Content = responseContent,
43 using var response = new ApiResponse(responseMessage);
45 Assert.Equal(new byte[] { 1, 2, 3 }, await response.ReadAsBytes());
49 public struct TestJson
51 [DataMember(Name = "foo")]
52 public int Foo { get; set; }
56 public async Task ReadAsJson_Test()
58 using var responseContent = new StringContent("""{"foo":123}""");
59 using var responseMessage = new HttpResponseMessage
61 StatusCode = HttpStatusCode.OK,
62 Content = responseContent,
64 using var response = new ApiResponse(responseMessage);
66 Assert.Equal(new() { Foo = 123 }, await response.ReadAsJson<TestJson>());
70 public async Task ReadAsJson_InvalidJsonTest()
72 using var responseContent = new StringContent("### Invalid JSON Response ###");
73 using var responseMessage = new HttpResponseMessage
75 StatusCode = HttpStatusCode.OK,
76 Content = responseContent,
78 using var response = new ApiResponse(responseMessage);
80 var ex = await Assert.ThrowsAsync<TwitterApiException>(
81 () => response.ReadAsJson<TestJson>()
83 Assert.Equal("### Invalid JSON Response ###", ex.ResponseText);
87 public async Task ReadAsJsonXml_Test()
89 using var responseContent = new StringContent("""{"foo":123}""");
90 using var responseMessage = new HttpResponseMessage
92 StatusCode = HttpStatusCode.OK,
93 Content = responseContent,
95 using var response = new ApiResponse(responseMessage);
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);
103 public async Task ReadAsJsonXml_InvalidJsonTest()
105 using var responseContent = new StringContent("### Invalid JSON Response ###");
106 using var responseMessage = new HttpResponseMessage
108 StatusCode = HttpStatusCode.OK,
109 Content = responseContent,
111 using var response = new ApiResponse(responseMessage);
113 var ex = await Assert.ThrowsAsync<TwitterApiException>(
114 () => response.ReadAsJsonXml()
116 Assert.Equal("### Invalid JSON Response ###", ex.ResponseText);