OSDN Git Service

statuses/show/:id.json => statuses/show.json?id=:id
[opentween/open-tween.git] / OpenTween.Tests / Api / TwitterApiTest.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.Linq;
25 using System.Net.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Text;
29 using System.Threading.Tasks;
30 using Moq;
31 using OpenTween.Api.DataModel;
32 using OpenTween.Connection;
33 using Xunit;
34
35 namespace OpenTween.Api
36 {
37     public class TwitterApiTest
38     {
39         public TwitterApiTest()
40         {
41             this.MyCommonSetup();
42         }
43
44         public void MyCommonSetup()
45         {
46             var mockAssembly = new Mock<_Assembly>();
47             mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
48
49             MyCommon.EntryAssembly = mockAssembly.Object;
50         }
51
52         [Fact]
53         public void Initialize_Test()
54         {
55             using (var twitterApi = new TwitterApi())
56             {
57                 Assert.Null(twitterApi.apiConnection);
58
59                 twitterApi.Initialize("*** AccessToken ***", "*** AccessSecret ***");
60
61                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
62
63                 var apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
64                 Assert.Equal("*** AccessToken ***", apiConnection.AccessToken);
65                 Assert.Equal("*** AccessSecret ***", apiConnection.AccessSecret);
66
67                 // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される
68                 twitterApi.Initialize("*** AccessToken2 ***", "*** AccessSecret2 ***");
69
70                 var oldApiConnection = apiConnection;
71                 Assert.True(oldApiConnection.IsDisposed);
72
73                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
74
75                 apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
76                 Assert.Equal("*** AccessToken2 ***", apiConnection.AccessToken);
77                 Assert.Equal("*** AccessSecret2 ***", apiConnection.AccessSecret);
78             }
79         }
80
81         [Fact]
82         public async Task StatusesShow_Test()
83         {
84             using (var twitterApi = new TwitterApi())
85             {
86                 var mock = new Mock<IApiConnection>();
87                 mock.Setup(x =>
88                     x.GetAsync<TwitterStatus>(
89                         new Uri("statuses/show.json", UriKind.Relative),
90                         new Dictionary<string, string> { { "id", "100" }, { "include_entities", "true" } })
91                 )
92                 .ReturnsAsync(new TwitterStatus { Id = 100L });
93
94                 twitterApi.apiConnection = mock.Object;
95
96                 await twitterApi.StatusesShow(statusId: 100L)
97                     .ConfigureAwait(false);
98
99                 mock.VerifyAll();
100             }
101         }
102
103         [Fact]
104         public async Task FavoritesCreate_Test()
105         {
106             using (var twitterApi = new TwitterApi())
107             {
108                 var mock = new Mock<IApiConnection>();
109                 mock.Setup(x =>
110                     x.PostLazyAsync<TwitterStatus>(
111                         new Uri("favorites/create.json", UriKind.Relative),
112                         new Dictionary<string, string> { { "id", "100" } })
113                 )
114                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
115
116                 twitterApi.apiConnection = mock.Object;
117
118                 await twitterApi.FavoritesCreate(statusId: 100L)
119                     .IgnoreResponse()
120                     .ConfigureAwait(false);
121
122                 mock.VerifyAll();
123             }
124         }
125
126         [Fact]
127         public async Task FavoritesDestroy_Test()
128         {
129             using (var twitterApi = new TwitterApi())
130             {
131                 var mock = new Mock<IApiConnection>();
132                 mock.Setup(x =>
133                     x.PostLazyAsync<TwitterStatus>(
134                         new Uri("favorites/destroy.json", UriKind.Relative),
135                         new Dictionary<string, string> { { "id", "100" } })
136                 )
137                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
138
139                 twitterApi.apiConnection = mock.Object;
140
141                 await twitterApi.FavoritesDestroy(statusId: 100L)
142                     .IgnoreResponse()
143                     .ConfigureAwait(false);
144
145                 mock.VerifyAll();
146             }
147         }
148     }
149 }