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;
25 using System.Net.Http;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
29 using System.Threading.Tasks;
31 using OpenTween.Api.DataModel;
32 using OpenTween.Connection;
35 namespace OpenTween.Api
37 public class TwitterApiTest
39 public TwitterApiTest()
44 public void MyCommonSetup()
46 var mockAssembly = new Mock<_Assembly>();
47 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
49 MyCommon.EntryAssembly = mockAssembly.Object;
53 public void Initialize_Test()
55 using (var twitterApi = new TwitterApi())
57 Assert.Null(twitterApi.apiConnection);
59 twitterApi.Initialize("*** AccessToken ***", "*** AccessSecret ***");
61 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
63 var apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
64 Assert.Equal("*** AccessToken ***", apiConnection.AccessToken);
65 Assert.Equal("*** AccessSecret ***", apiConnection.AccessSecret);
67 // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される
68 twitterApi.Initialize("*** AccessToken2 ***", "*** AccessSecret2 ***");
70 var oldApiConnection = apiConnection;
71 Assert.True(oldApiConnection.IsDisposed);
73 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
75 apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
76 Assert.Equal("*** AccessToken2 ***", apiConnection.AccessToken);
77 Assert.Equal("*** AccessSecret2 ***", apiConnection.AccessSecret);
82 public async Task StatusesShow_Test()
84 using (var twitterApi = new TwitterApi())
86 var mock = new Mock<IApiConnection>();
88 x.GetAsync<TwitterStatus>(
89 new Uri("statuses/show.json", UriKind.Relative),
90 new Dictionary<string, string> { { "id", "100" }, { "include_entities", "true" } })
92 .ReturnsAsync(new TwitterStatus { Id = 100L });
94 twitterApi.apiConnection = mock.Object;
96 await twitterApi.StatusesShow(statusId: 100L)
97 .ConfigureAwait(false);
104 public async Task FavoritesCreate_Test()
106 using (var twitterApi = new TwitterApi())
108 var mock = new Mock<IApiConnection>();
110 x.PostLazyAsync<TwitterStatus>(
111 new Uri("favorites/create.json", UriKind.Relative),
112 new Dictionary<string, string> { { "id", "100" } })
114 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
116 twitterApi.apiConnection = mock.Object;
118 await twitterApi.FavoritesCreate(statusId: 100L)
120 .ConfigureAwait(false);
127 public async Task FavoritesDestroy_Test()
129 using (var twitterApi = new TwitterApi())
131 var mock = new Mock<IApiConnection>();
133 x.PostLazyAsync<TwitterStatus>(
134 new Uri("favorites/destroy.json", UriKind.Relative),
135 new Dictionary<string, string> { { "id", "100" } })
137 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
139 twitterApi.apiConnection = mock.Object;
141 await twitterApi.FavoritesDestroy(statusId: 100L)
143 .ConfigureAwait(false);