OSDN Git Service

HttpTwitter.CreateListMembersメソッドをTwitterApiクラスに置き換え
[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.IO;
25 using System.Linq;
26 using System.Net.Http;
27 using System.Reflection;
28 using System.Runtime.InteropServices;
29 using System.Text;
30 using System.Threading.Tasks;
31 using Moq;
32 using OpenTween.Api.DataModel;
33 using OpenTween.Connection;
34 using Xunit;
35
36 namespace OpenTween.Api
37 {
38     public class TwitterApiTest
39     {
40         public TwitterApiTest()
41         {
42             this.MyCommonSetup();
43         }
44
45         public void MyCommonSetup()
46         {
47             var mockAssembly = new Mock<_Assembly>();
48             mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
49
50             MyCommon.EntryAssembly = mockAssembly.Object;
51         }
52
53         [Fact]
54         public void Initialize_Test()
55         {
56             using (var twitterApi = new TwitterApi())
57             {
58                 Assert.Null(twitterApi.apiConnection);
59
60                 twitterApi.Initialize("*** AccessToken ***", "*** AccessSecret ***", userId: 100L, screenName: "hogehoge");
61
62                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
63
64                 var apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
65                 Assert.Equal("*** AccessToken ***", apiConnection.AccessToken);
66                 Assert.Equal("*** AccessSecret ***", apiConnection.AccessSecret);
67
68                 Assert.Equal(100L, twitterApi.CurrentUserId);
69                 Assert.Equal("hogehoge", twitterApi.CurrentScreenName);
70
71                 // 複数回 Initialize を実行した場合は新たに TwitterApiConnection が生成される
72                 twitterApi.Initialize("*** AccessToken2 ***", "*** AccessSecret2 ***", userId: 200L, screenName: "foobar");
73
74                 var oldApiConnection = apiConnection;
75                 Assert.True(oldApiConnection.IsDisposed);
76
77                 Assert.IsType<TwitterApiConnection>(twitterApi.apiConnection);
78
79                 apiConnection = (TwitterApiConnection)twitterApi.apiConnection;
80                 Assert.Equal("*** AccessToken2 ***", apiConnection.AccessToken);
81                 Assert.Equal("*** AccessSecret2 ***", apiConnection.AccessSecret);
82
83                 Assert.Equal(200L, twitterApi.CurrentUserId);
84                 Assert.Equal("foobar", twitterApi.CurrentScreenName);
85             }
86         }
87
88         [Fact]
89         public async Task StatusesHomeTimeline_Test()
90         {
91             using (var twitterApi = new TwitterApi())
92             {
93                 var mock = new Mock<IApiConnection>();
94                 mock.Setup(x =>
95                     x.GetAsync<TwitterStatus[]>(
96                         new Uri("statuses/home_timeline.json", UriKind.Relative),
97                         new Dictionary<string, string> {
98                             { "include_entities", "true" },
99                             { "include_ext_alt_text", "true" },
100                             { "count", "200" },
101                             { "max_id", "900" },
102                             { "since_id", "100" },
103                         },
104                         "/statuses/home_timeline")
105                 )
106                 .ReturnsAsync(new TwitterStatus[0]);
107
108                 twitterApi.apiConnection = mock.Object;
109
110                 await twitterApi.StatusesHomeTimeline(200, maxId: 900L, sinceId: 100L)
111                     .ConfigureAwait(false);
112
113                 mock.VerifyAll();
114             }
115         }
116
117         [Fact]
118         public async Task StatusesMentionsTimeline_Test()
119         {
120             using (var twitterApi = new TwitterApi())
121             {
122                 var mock = new Mock<IApiConnection>();
123                 mock.Setup(x =>
124                     x.GetAsync<TwitterStatus[]>(
125                         new Uri("statuses/mentions_timeline.json", UriKind.Relative),
126                         new Dictionary<string, string> {
127                             { "include_entities", "true" },
128                             { "include_ext_alt_text", "true" },
129                             { "count", "200" },
130                             { "max_id", "900" },
131                             { "since_id", "100" },
132                         },
133                         "/statuses/mentions_timeline")
134                 )
135                 .ReturnsAsync(new TwitterStatus[0]);
136
137                 twitterApi.apiConnection = mock.Object;
138
139                 await twitterApi.StatusesMentionsTimeline(200, maxId: 900L, sinceId: 100L)
140                     .ConfigureAwait(false);
141
142                 mock.VerifyAll();
143             }
144         }
145
146         [Fact]
147         public async Task StatusesUserTimeline_Test()
148         {
149             using (var twitterApi = new TwitterApi())
150             {
151                 var mock = new Mock<IApiConnection>();
152                 mock.Setup(x =>
153                     x.GetAsync<TwitterStatus[]>(
154                         new Uri("statuses/user_timeline.json", UriKind.Relative),
155                         new Dictionary<string, string> {
156                             { "screen_name", "twitterapi" },
157                             { "include_rts", "true" },
158                             { "include_entities", "true" },
159                             { "include_ext_alt_text", "true" },
160                             { "count", "200" },
161                             { "max_id", "900" },
162                             { "since_id", "100" },
163                         },
164                         "/statuses/user_timeline")
165                 )
166                 .ReturnsAsync(new TwitterStatus[0]);
167
168                 twitterApi.apiConnection = mock.Object;
169
170                 await twitterApi.StatusesUserTimeline("twitterapi", count: 200, maxId: 900L, sinceId: 100L)
171                     .ConfigureAwait(false);
172
173                 mock.VerifyAll();
174             }
175         }
176
177         [Fact]
178         public async Task StatusesShow_Test()
179         {
180             using (var twitterApi = new TwitterApi())
181             {
182                 var mock = new Mock<IApiConnection>();
183                 mock.Setup(x =>
184                     x.GetAsync<TwitterStatus>(
185                         new Uri("statuses/show.json", UriKind.Relative),
186                         new Dictionary<string, string> {
187                             { "id", "100" },
188                             { "include_entities", "true" },
189                             { "include_ext_alt_text", "true" },
190                         },
191                         "/statuses/show/:id")
192                 )
193                 .ReturnsAsync(new TwitterStatus { Id = 100L });
194
195                 twitterApi.apiConnection = mock.Object;
196
197                 await twitterApi.StatusesShow(statusId: 100L)
198                     .ConfigureAwait(false);
199
200                 mock.VerifyAll();
201             }
202         }
203
204         [Fact]
205         public async Task StatusesUpdate_Test()
206         {
207             using (var twitterApi = new TwitterApi())
208             {
209                 var mock = new Mock<IApiConnection>();
210                 mock.Setup(x =>
211                     x.PostLazyAsync<TwitterStatus>(
212                         new Uri("statuses/update.json", UriKind.Relative),
213                         new Dictionary<string, string> {
214                             { "status", "hogehoge" },
215                             { "include_entities", "true" },
216                             { "include_ext_alt_text", "true" },
217                             { "in_reply_to_status_id", "100" },
218                             { "media_ids", "10,20" },
219                         })
220                 )
221                 .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
222
223                 twitterApi.apiConnection = mock.Object;
224
225                 await twitterApi.StatusesUpdate("hogehoge", replyToId: 100L, mediaIds: new[] { 10L, 20L })
226                     .IgnoreResponse()
227                     .ConfigureAwait(false);
228
229                 mock.VerifyAll();
230             }
231         }
232
233         [Fact]
234         public async Task StatusesDestroy_Test()
235         {
236             using (var twitterApi = new TwitterApi())
237             {
238                 var mock = new Mock<IApiConnection>();
239                 mock.Setup(x =>
240                     x.PostLazyAsync<TwitterStatus>(
241                         new Uri("statuses/destroy.json", UriKind.Relative),
242                         new Dictionary<string, string> { { "id", "100" } })
243                 )
244                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
245
246                 twitterApi.apiConnection = mock.Object;
247
248                 await twitterApi.StatusesDestroy(statusId: 100L)
249                     .IgnoreResponse()
250                     .ConfigureAwait(false);
251
252                 mock.VerifyAll();
253             }
254         }
255
256         [Fact]
257         public async Task StatusesRetweet_Test()
258         {
259             using (var twitterApi = new TwitterApi())
260             {
261                 var mock = new Mock<IApiConnection>();
262                 mock.Setup(x =>
263                     x.PostLazyAsync<TwitterStatus>(
264                         new Uri("statuses/retweet.json", UriKind.Relative),
265                         new Dictionary<string, string> {
266                             { "id", "100" },
267                             { "include_entities", "true" },
268                             { "include_ext_alt_text", "true" },
269                         })
270                 )
271                 .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
272
273                 twitterApi.apiConnection = mock.Object;
274
275                 await twitterApi.StatusesRetweet(100L)
276                     .IgnoreResponse()
277                     .ConfigureAwait(false);
278
279                 mock.VerifyAll();
280             }
281         }
282
283         [Fact]
284         public async Task ListsOwnerships_Test()
285         {
286             using (var twitterApi = new TwitterApi())
287             {
288                 var mock = new Mock<IApiConnection>();
289                 mock.Setup(x =>
290                     x.GetAsync<TwitterLists>(
291                         new Uri("lists/ownerships.json", UriKind.Relative),
292                         new Dictionary<string, string> {
293                             { "screen_name", "twitterapi" },
294                             { "cursor", "-1" },
295                         },
296                         "/lists/ownerships")
297                 )
298                 .ReturnsAsync(new TwitterLists());
299
300                 twitterApi.apiConnection = mock.Object;
301
302                 await twitterApi.ListsOwnerships("twitterapi", cursor: -1L)
303                     .ConfigureAwait(false);
304
305                 mock.VerifyAll();
306             }
307         }
308
309         [Fact]
310         public async Task ListsSubscriptions_Test()
311         {
312             using (var twitterApi = new TwitterApi())
313             {
314                 var mock = new Mock<IApiConnection>();
315                 mock.Setup(x =>
316                     x.GetAsync<TwitterLists>(
317                         new Uri("lists/subscriptions.json", UriKind.Relative),
318                         new Dictionary<string, string> {
319                             { "screen_name", "twitterapi" },
320                             { "cursor", "-1" },
321                         },
322                         "/lists/subscriptions")
323                 )
324                 .ReturnsAsync(new TwitterLists());
325
326                 twitterApi.apiConnection = mock.Object;
327
328                 await twitterApi.ListsSubscriptions("twitterapi", cursor: -1L)
329                     .ConfigureAwait(false);
330
331                 mock.VerifyAll();
332             }
333         }
334
335         [Fact]
336         public async Task ListsCreate_Test()
337         {
338             using (var twitterApi = new TwitterApi())
339             {
340                 var mock = new Mock<IApiConnection>();
341                 mock.Setup(x =>
342                     x.PostLazyAsync<TwitterList>(
343                         new Uri("lists/create.json", UriKind.Relative),
344                         new Dictionary<string, string> {
345                             { "name", "hogehoge" },
346                             { "description", "aaaa" },
347                             { "mode", "private" },
348                         })
349                 )
350                 .ReturnsAsync(LazyJson.Create(new TwitterList()));
351
352                 twitterApi.apiConnection = mock.Object;
353
354                 await twitterApi.ListsCreate("hogehoge", description: "aaaa", @private: true)
355                     .IgnoreResponse()
356                     .ConfigureAwait(false);
357
358                 mock.VerifyAll();
359             }
360         }
361
362         [Fact]
363         public async Task ListsUpdate_Test()
364         {
365             using (var twitterApi = new TwitterApi())
366             {
367                 var mock = new Mock<IApiConnection>();
368                 mock.Setup(x =>
369                     x.PostLazyAsync<TwitterList>(
370                         new Uri("lists/update.json", UriKind.Relative),
371                         new Dictionary<string, string> {
372                             { "list_id", "12345" },
373                             { "name", "hogehoge" },
374                             { "description", "aaaa" },
375                             { "mode", "private" },
376                         })
377                 )
378                 .ReturnsAsync(LazyJson.Create(new TwitterList()));
379
380                 twitterApi.apiConnection = mock.Object;
381
382                 await twitterApi.ListsUpdate(12345L, name: "hogehoge", description: "aaaa", @private: true)
383                     .IgnoreResponse()
384                     .ConfigureAwait(false);
385
386                 mock.VerifyAll();
387             }
388         }
389
390         [Fact]
391         public async Task ListsDestroy_Test()
392         {
393             using (var twitterApi = new TwitterApi())
394             {
395                 var mock = new Mock<IApiConnection>();
396                 mock.Setup(x =>
397                     x.PostLazyAsync<TwitterList>(
398                         new Uri("lists/destroy.json", UriKind.Relative),
399                         new Dictionary<string, string> {
400                             { "list_id", "12345" },
401                         })
402                 )
403                 .ReturnsAsync(LazyJson.Create(new TwitterList()));
404
405                 twitterApi.apiConnection = mock.Object;
406
407                 await twitterApi.ListsDestroy(12345L)
408                     .IgnoreResponse()
409                     .ConfigureAwait(false);
410
411                 mock.VerifyAll();
412             }
413         }
414
415         [Fact]
416         public async Task ListsStatuses_Test()
417         {
418             using (var twitterApi = new TwitterApi())
419             {
420                 var mock = new Mock<IApiConnection>();
421                 mock.Setup(x =>
422                     x.GetAsync<TwitterStatus[]>(
423                         new Uri("lists/statuses.json", UriKind.Relative),
424                         new Dictionary<string, string> {
425                             { "list_id", "12345" },
426                             { "include_entities", "true" },
427                             { "include_ext_alt_text", "true" },
428                             { "count", "200" },
429                             { "max_id", "900" },
430                             { "since_id", "100" },
431                             { "include_rts", "true" },
432                         },
433                         "/lists/statuses")
434                 )
435                 .ReturnsAsync(new TwitterStatus[0]);
436
437                 twitterApi.apiConnection = mock.Object;
438
439                 await twitterApi.ListsStatuses(12345L, count: 200, maxId: 900L, sinceId: 100L, includeRTs: true)
440                     .ConfigureAwait(false);
441
442                 mock.VerifyAll();
443             }
444         }
445
446         [Fact]
447         public async Task ListsMembers_Test()
448         {
449             using (var twitterApi = new TwitterApi())
450             {
451                 var mock = new Mock<IApiConnection>();
452                 mock.Setup(x =>
453                     x.GetAsync<TwitterUsers>(
454                         new Uri("lists/members.json", UriKind.Relative),
455                         new Dictionary<string, string> {
456                             { "list_id", "12345" },
457                             { "include_entities", "true" },
458                             { "include_ext_alt_text", "true" },
459                             { "cursor", "-1" },
460                         },
461                         "/lists/members")
462                 )
463                 .ReturnsAsync(new TwitterUsers());
464
465                 twitterApi.apiConnection = mock.Object;
466
467                 await twitterApi.ListsMembers(12345L, cursor: -1)
468                     .ConfigureAwait(false);
469
470                 mock.VerifyAll();
471             }
472         }
473
474         [Fact]
475         public async Task ListsMembersShow_Test()
476         {
477             using (var twitterApi = new TwitterApi())
478             {
479                 var mock = new Mock<IApiConnection>();
480                 mock.Setup(x =>
481                     x.GetAsync<TwitterUser>(
482                         new Uri("lists/members/show.json", UriKind.Relative),
483                         new Dictionary<string, string> {
484                             { "list_id", "12345" },
485                             { "screen_name", "twitterapi" },
486                             { "include_entities", "true" },
487                             { "include_ext_alt_text", "true" },
488                         },
489                         "/lists/members/show")
490                 )
491                 .ReturnsAsync(new TwitterUser());
492
493                 twitterApi.apiConnection = mock.Object;
494
495                 await twitterApi.ListsMembersShow(12345L, "twitterapi")
496                     .ConfigureAwait(false);
497
498                 mock.VerifyAll();
499             }
500         }
501
502         [Fact]
503         public async Task ListsMembersCreate_Test()
504         {
505             using (var twitterApi = new TwitterApi())
506             {
507                 var mock = new Mock<IApiConnection>();
508                 mock.Setup(x =>
509                     x.PostLazyAsync<TwitterUser>(
510                         new Uri("lists/members/create.json", UriKind.Relative),
511                         new Dictionary<string, string> {
512                             { "list_id", "12345" },
513                             { "screen_name", "twitterapi" },
514                             { "include_entities", "true" },
515                             { "include_ext_alt_text", "true" },
516                         })
517                 )
518                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
519
520                 twitterApi.apiConnection = mock.Object;
521
522                 await twitterApi.ListsMembersCreate(12345L, "twitterapi")
523                     .IgnoreResponse()
524                     .ConfigureAwait(false);
525
526                 mock.VerifyAll();
527             }
528         }
529
530         [Fact]
531         public async Task DirectMessagesRecv_Test()
532         {
533             using (var twitterApi = new TwitterApi())
534             {
535                 var mock = new Mock<IApiConnection>();
536                 mock.Setup(x =>
537                     x.GetAsync<TwitterDirectMessage[]>(
538                         new Uri("direct_messages.json", UriKind.Relative),
539                         new Dictionary<string, string> {
540                             { "full_text", "true" },
541                             { "include_entities", "true" },
542                             { "include_ext_alt_text", "true" },
543                             { "count", "200" },
544                             { "max_id", "900" },
545                             { "since_id", "100" },
546                         },
547                         "/direct_messages")
548                 )
549                 .ReturnsAsync(new TwitterDirectMessage[0]);
550
551                 twitterApi.apiConnection = mock.Object;
552
553                 await twitterApi.DirectMessagesRecv(count: 200, maxId: 900L, sinceId: 100L)
554                     .ConfigureAwait(false);
555
556                 mock.VerifyAll();
557             }
558         }
559
560         [Fact]
561         public async Task DirectMessagesSent_Test()
562         {
563             using (var twitterApi = new TwitterApi())
564             {
565                 var mock = new Mock<IApiConnection>();
566                 mock.Setup(x =>
567                     x.GetAsync<TwitterDirectMessage[]>(
568                         new Uri("direct_messages/sent.json", UriKind.Relative),
569                         new Dictionary<string, string> {
570                             { "full_text", "true" },
571                             { "include_entities", "true" },
572                             { "include_ext_alt_text", "true" },
573                             { "count", "200" },
574                             { "max_id", "900" },
575                             { "since_id", "100" },
576                         },
577                         "/direct_messages/sent")
578                 )
579                 .ReturnsAsync(new TwitterDirectMessage[0]);
580
581                 twitterApi.apiConnection = mock.Object;
582
583                 await twitterApi.DirectMessagesSent(count: 200, maxId: 900L, sinceId: 100L)
584                     .ConfigureAwait(false);
585
586                 mock.VerifyAll();
587             }
588         }
589
590         [Fact]
591         public async Task DirectMessagesNew_Test()
592         {
593             using (var twitterApi = new TwitterApi())
594             {
595                 var mock = new Mock<IApiConnection>();
596                 mock.Setup(x =>
597                     x.PostLazyAsync<TwitterDirectMessage>(
598                         new Uri("direct_messages/new.json", UriKind.Relative),
599                         new Dictionary<string, string> {
600                             { "text", "hogehoge" },
601                             { "screen_name", "opentween" },
602                         })
603                 )
604                 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
605
606                 twitterApi.apiConnection = mock.Object;
607
608                 await twitterApi.DirectMessagesNew("hogehoge", "opentween")
609                     .IgnoreResponse()
610                     .ConfigureAwait(false);
611
612                 mock.VerifyAll();
613             }
614         }
615
616         [Fact]
617         public async Task DirectMessagesDestroy_Test()
618         {
619             using (var twitterApi = new TwitterApi())
620             {
621                 var mock = new Mock<IApiConnection>();
622                 mock.Setup(x =>
623                     x.PostLazyAsync<TwitterDirectMessage>(
624                         new Uri("direct_messages/destroy.json", UriKind.Relative),
625                         new Dictionary<string, string> { { "id", "100" } })
626                 )
627                 .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L }));
628
629                 twitterApi.apiConnection = mock.Object;
630
631                 await twitterApi.DirectMessagesDestroy(statusId: 100L)
632                     .IgnoreResponse()
633                     .ConfigureAwait(false);
634
635                 mock.VerifyAll();
636             }
637         }
638
639         [Fact]
640         public async Task UsersShow_Test()
641         {
642             using (var twitterApi = new TwitterApi())
643             {
644                 var mock = new Mock<IApiConnection>();
645                 mock.Setup(x =>
646                     x.GetAsync<TwitterUser>(
647                         new Uri("users/show.json", UriKind.Relative),
648                         new Dictionary<string, string> {
649                             { "screen_name", "twitterapi" },
650                             { "include_entities", "true" },
651                             { "include_ext_alt_text", "true" },
652                         },
653                         "/users/show/:id")
654                 )
655                 .ReturnsAsync(new TwitterUser { ScreenName = "twitterapi" });
656
657                 twitterApi.apiConnection = mock.Object;
658
659                 await twitterApi.UsersShow(screenName: "twitterapi")
660                     .ConfigureAwait(false);
661
662                 mock.VerifyAll();
663             }
664         }
665
666         [Fact]
667         public async Task UsersReportSpam_Test()
668         {
669             using (var twitterApi = new TwitterApi())
670             {
671                 var mock = new Mock<IApiConnection>();
672                 mock.Setup(x =>
673                     x.PostLazyAsync<TwitterUser>(
674                         new Uri("users/report_spam.json", UriKind.Relative),
675                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
676                 )
677                 .ReturnsAsync(LazyJson.Create(new TwitterUser { ScreenName = "twitterapi" }));
678
679                 twitterApi.apiConnection = mock.Object;
680
681                 await twitterApi.UsersReportSpam(screenName: "twitterapi")
682                     .IgnoreResponse()
683                     .ConfigureAwait(false);
684
685                 mock.VerifyAll();
686             }
687         }
688
689         [Fact]
690         public async Task FavoritesCreate_Test()
691         {
692             using (var twitterApi = new TwitterApi())
693             {
694                 var mock = new Mock<IApiConnection>();
695                 mock.Setup(x =>
696                     x.PostLazyAsync<TwitterStatus>(
697                         new Uri("favorites/create.json", UriKind.Relative),
698                         new Dictionary<string, string> { { "id", "100" } })
699                 )
700                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
701
702                 twitterApi.apiConnection = mock.Object;
703
704                 await twitterApi.FavoritesCreate(statusId: 100L)
705                     .IgnoreResponse()
706                     .ConfigureAwait(false);
707
708                 mock.VerifyAll();
709             }
710         }
711
712         [Fact]
713         public async Task FavoritesDestroy_Test()
714         {
715             using (var twitterApi = new TwitterApi())
716             {
717                 var mock = new Mock<IApiConnection>();
718                 mock.Setup(x =>
719                     x.PostLazyAsync<TwitterStatus>(
720                         new Uri("favorites/destroy.json", UriKind.Relative),
721                         new Dictionary<string, string> { { "id", "100" } })
722                 )
723                 .ReturnsAsync(LazyJson.Create(new TwitterStatus { Id = 100L }));
724
725                 twitterApi.apiConnection = mock.Object;
726
727                 await twitterApi.FavoritesDestroy(statusId: 100L)
728                     .IgnoreResponse()
729                     .ConfigureAwait(false);
730
731                 mock.VerifyAll();
732             }
733         }
734
735         [Fact]
736         public async Task FriendshipsShow_Test()
737         {
738             using (var twitterApi = new TwitterApi())
739             {
740                 var mock = new Mock<IApiConnection>();
741                 mock.Setup(x =>
742                     x.GetAsync<TwitterFriendship>(
743                         new Uri("friendships/show.json", UriKind.Relative),
744                         new Dictionary<string, string> { { "source_screen_name", "twitter" }, { "target_screen_name", "twitterapi" } },
745                         "/friendships/show")
746                 )
747                 .ReturnsAsync(new TwitterFriendship());
748
749                 twitterApi.apiConnection = mock.Object;
750
751                 await twitterApi.FriendshipsShow(sourceScreenName: "twitter", targetScreenName: "twitterapi")
752                     .ConfigureAwait(false);
753
754                 mock.VerifyAll();
755             }
756         }
757
758         [Fact]
759         public async Task FriendshipsCreate_Test()
760         {
761             using (var twitterApi = new TwitterApi())
762             {
763                 var mock = new Mock<IApiConnection>();
764                 mock.Setup(x =>
765                     x.PostLazyAsync<TwitterFriendship>(
766                         new Uri("friendships/create.json", UriKind.Relative),
767                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
768                 )
769                 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
770
771                 twitterApi.apiConnection = mock.Object;
772
773                 await twitterApi.FriendshipsCreate(screenName: "twitterapi")
774                     .IgnoreResponse()
775                     .ConfigureAwait(false);
776
777                 mock.VerifyAll();
778             }
779         }
780
781         [Fact]
782         public async Task FriendshipsDestroy_Test()
783         {
784             using (var twitterApi = new TwitterApi())
785             {
786                 var mock = new Mock<IApiConnection>();
787                 mock.Setup(x =>
788                     x.PostLazyAsync<TwitterFriendship>(
789                         new Uri("friendships/destroy.json", UriKind.Relative),
790                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
791                 )
792                 .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));
793
794                 twitterApi.apiConnection = mock.Object;
795
796                 await twitterApi.FriendshipsDestroy(screenName: "twitterapi")
797                     .IgnoreResponse()
798                     .ConfigureAwait(false);
799
800                 mock.VerifyAll();
801             }
802         }
803
804         [Fact]
805         public async Task NoRetweetIds_Test()
806         {
807             using (var twitterApi = new TwitterApi())
808             {
809                 var mock = new Mock<IApiConnection>();
810                 mock.Setup(x =>
811                     x.GetAsync<long[]>(
812                         new Uri("friendships/no_retweets/ids.json", UriKind.Relative),
813                         null,
814                         "/friendships/no_retweets/ids")
815                 )
816                 .ReturnsAsync(new long[0]);
817
818                 twitterApi.apiConnection = mock.Object;
819
820                 await twitterApi.NoRetweetIds()
821                     .ConfigureAwait(false);
822
823                 mock.VerifyAll();
824             }
825         }
826
827         [Fact]
828         public async Task FollowersIds_Test()
829         {
830             using (var twitterApi = new TwitterApi())
831             {
832                 var mock = new Mock<IApiConnection>();
833                 mock.Setup(x =>
834                     x.GetAsync<TwitterIds>(
835                         new Uri("followers/ids.json", UriKind.Relative),
836                         new Dictionary<string, string> { { "cursor", "-1" } },
837                         "/followers/ids")
838                 )
839                 .ReturnsAsync(new TwitterIds());
840
841                 twitterApi.apiConnection = mock.Object;
842
843                 await twitterApi.FollowersIds(cursor: -1L)
844                     .ConfigureAwait(false);
845
846                 mock.VerifyAll();
847             }
848         }
849
850         [Fact]
851         public async Task MutesUsersIds_Test()
852         {
853             using (var twitterApi = new TwitterApi())
854             {
855                 var mock = new Mock<IApiConnection>();
856                 mock.Setup(x =>
857                     x.GetAsync<TwitterIds>(
858                         new Uri("mutes/users/ids.json", UriKind.Relative),
859                         new Dictionary<string, string> { { "cursor", "-1" } },
860                         "/mutes/users/ids")
861                 )
862                 .ReturnsAsync(new TwitterIds());
863
864                 twitterApi.apiConnection = mock.Object;
865
866                 await twitterApi.MutesUsersIds(cursor: -1L)
867                     .ConfigureAwait(false);
868
869                 mock.VerifyAll();
870             }
871         }
872
873         [Fact]
874         public async Task BlocksIds_Test()
875         {
876             using (var twitterApi = new TwitterApi())
877             {
878                 var mock = new Mock<IApiConnection>();
879                 mock.Setup(x =>
880                     x.GetAsync<TwitterIds>(
881                         new Uri("blocks/ids.json", UriKind.Relative),
882                         new Dictionary<string, string> { { "cursor", "-1" } },
883                         "/blocks/ids")
884                 )
885                 .ReturnsAsync(new TwitterIds());
886
887                 twitterApi.apiConnection = mock.Object;
888
889                 await twitterApi.BlocksIds(cursor: -1L)
890                     .ConfigureAwait(false);
891
892                 mock.VerifyAll();
893             }
894         }
895
896         [Fact]
897         public async Task BlocksCreate_Test()
898         {
899             using (var twitterApi = new TwitterApi())
900             {
901                 var mock = new Mock<IApiConnection>();
902                 mock.Setup(x =>
903                     x.PostLazyAsync<TwitterUser>(
904                         new Uri("blocks/create.json", UriKind.Relative),
905                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
906                 )
907                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
908
909                 twitterApi.apiConnection = mock.Object;
910
911                 await twitterApi.BlocksCreate(screenName: "twitterapi")
912                     .IgnoreResponse()
913                     .ConfigureAwait(false);
914
915                 mock.VerifyAll();
916             }
917         }
918
919         [Fact]
920         public async Task BlocksDestroy_Test()
921         {
922             using (var twitterApi = new TwitterApi())
923             {
924                 var mock = new Mock<IApiConnection>();
925                 mock.Setup(x =>
926                     x.PostLazyAsync<TwitterUser>(
927                         new Uri("blocks/destroy.json", UriKind.Relative),
928                         new Dictionary<string, string> { { "screen_name", "twitterapi" } })
929                 )
930                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
931
932                 twitterApi.apiConnection = mock.Object;
933
934                 await twitterApi.BlocksDestroy(screenName: "twitterapi")
935                     .IgnoreResponse()
936                     .ConfigureAwait(false);
937
938                 mock.VerifyAll();
939             }
940         }
941
942         [Fact]
943         public async Task AccountUpdateProfile_Test()
944         {
945             using (var twitterApi = new TwitterApi())
946             {
947                 var mock = new Mock<IApiConnection>();
948                 mock.Setup(x =>
949                     x.PostLazyAsync<TwitterUser>(
950                         new Uri("account/update_profile.json", UriKind.Relative),
951                         new Dictionary<string, string> {
952                             { "include_entities", "true" },
953                             { "include_ext_alt_text", "true" },
954                             { "name", "Name" },
955                             { "url", "http://example.com/" },
956                             { "location", "Location" },
957                             { "description", "&lt;script&gt;alert(1)&lt;/script&gt;" },
958                         })
959                 )
960                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
961
962                 twitterApi.apiConnection = mock.Object;
963
964                 await twitterApi.AccountUpdateProfile(name: "Name", url: "http://example.com/", location: "Location", description: "<script>alert(1)</script>")
965                     .IgnoreResponse()
966                     .ConfigureAwait(false);
967
968                 mock.VerifyAll();
969             }
970         }
971
972         [Fact]
973         public async Task AccountUpdateProfileImage_Test()
974         {
975             using (var twitterApi = new TwitterApi())
976             using (var image = TestUtils.CreateDummyImage())
977             using (var media = new MemoryImageMediaItem(image))
978             {
979                 var mock = new Mock<IApiConnection>();
980                 mock.Setup(x =>
981                     x.PostLazyAsync<TwitterUser>(
982                         new Uri("account/update_profile_image.json", UriKind.Relative),
983                         new Dictionary<string, string> {
984                             { "include_entities", "true" },
985                             { "include_ext_alt_text", "true" },
986                         },
987                         new Dictionary<string, IMediaItem> { { "image", media } })
988                 )
989                 .ReturnsAsync(LazyJson.Create(new TwitterUser()));
990
991                 twitterApi.apiConnection = mock.Object;
992
993                 await twitterApi.AccountUpdateProfileImage(media)
994                     .IgnoreResponse()
995                     .ConfigureAwait(false);
996
997                 mock.VerifyAll();
998             }
999         }
1000
1001         [Fact]
1002         public async Task ApplicationRateLimitStatus_Test()
1003         {
1004             using (var twitterApi = new TwitterApi())
1005             {
1006                 var mock = new Mock<IApiConnection>();
1007                 mock.Setup(x =>
1008                     x.GetAsync<TwitterRateLimits>(
1009                         new Uri("application/rate_limit_status.json", UriKind.Relative),
1010                         null,
1011                         "/application/rate_limit_status")
1012                 )
1013                 .ReturnsAsync(new TwitterRateLimits());
1014
1015                 twitterApi.apiConnection = mock.Object;
1016
1017                 await twitterApi.ApplicationRateLimitStatus()
1018                     .ConfigureAwait(false);
1019
1020                 mock.VerifyAll();
1021             }
1022         }
1023
1024         [Fact]
1025         public async Task Configuration_Test()
1026         {
1027             using (var twitterApi = new TwitterApi())
1028             {
1029                 var mock = new Mock<IApiConnection>();
1030                 mock.Setup(x =>
1031                     x.GetAsync<TwitterConfiguration>(
1032                         new Uri("help/configuration.json", UriKind.Relative),
1033                         null,
1034                         "/help/configuration")
1035                 )
1036                 .ReturnsAsync(new TwitterConfiguration());
1037
1038                 twitterApi.apiConnection = mock.Object;
1039
1040                 await twitterApi.Configuration()
1041                     .ConfigureAwait(false);
1042
1043                 mock.VerifyAll();
1044             }
1045         }
1046
1047         [Fact]
1048         public async Task MediaUpload_Test()
1049         {
1050             using (var twitterApi = new TwitterApi())
1051             using (var image = TestUtils.CreateDummyImage())
1052             using (var media = new MemoryImageMediaItem(image))
1053             {
1054                 var mock = new Mock<IApiConnection>();
1055                 mock.Setup(x =>
1056                     x.PostLazyAsync<TwitterUploadMediaResult>(
1057                         new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
1058                         null,
1059                         new Dictionary<string, IMediaItem> { { "media", media } })
1060                 )
1061                 .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));
1062
1063                 twitterApi.apiConnection = mock.Object;
1064
1065                 await twitterApi.MediaUpload(media)
1066                     .IgnoreResponse()
1067                     .ConfigureAwait(false);
1068
1069                 mock.VerifyAll();
1070             }
1071         }
1072
1073         [Fact]
1074         public async Task UserStreams_Test()
1075         {
1076             using (var twitterApi = new TwitterApi())
1077             {
1078                 var mock = new Mock<IApiConnection>();
1079                 mock.Setup(x =>
1080                     x.GetStreamAsync(
1081                         new Uri("https://userstream.twitter.com/1.1/user.json", UriKind.Absolute),
1082                         new Dictionary<string, string> {
1083                             { "replies", "all" },
1084                             { "track", "OpenTween" },
1085                         })
1086                 )
1087                 .ReturnsAsync(new MemoryStream());
1088
1089                 twitterApi.apiConnection = mock.Object;
1090
1091                 var stream = await twitterApi.UserStreams(replies: "all", track: "OpenTween")
1092                     .ConfigureAwait(false);
1093
1094                 stream.Dispose();
1095
1096                 mock.VerifyAll();
1097             }
1098         }
1099     }
1100 }