예제 #1
0
        public async Task AccountUpdateProfile_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterUser>(
                               new Uri("account/update_profile.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "include_entities", "true" },
                    { "include_ext_alt_text", "true" },
                    { "tweet_mode", "extended" },
                    { "name", "Name" },
                    { "url", "http://example.com/" },
                    { "location", "Location" },
                    { "description", "&lt;script&gt;alert(1)&lt;/script&gt;" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterUser()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.AccountUpdateProfile(name : "Name", url : "http://example.com/", location : "Location", description : "<script>alert(1)</script>")
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #2
0
        public async Task FavoritesDestroy_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterStatus>(
                               new Uri("favorites/destroy.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "id", "100" },
                    { "tweet_mode", "extended" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterStatus {
                    Id = 100L
                }));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.FavoritesDestroy(statusId : 100L)
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #3
0
        public async Task StatusesUpdate_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterStatus>(
                               new Uri("statuses/update.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "status", "hogehoge" },
                    { "include_entities", "true" },
                    { "include_ext_alt_text", "true" },
                    { "tweet_mode", "extended" },
                    { "in_reply_to_status_id", "100" },
                    { "media_ids", "10,20" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterStatus()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.StatusesUpdate("hogehoge", replyToId : 100L, mediaIds : new[] { 10L, 20L })
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #4
0
        public async Task MediaUploadInit_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterUploadMediaInit>(
                           new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
                           new Dictionary <string, string> {
                { "command", "INIT" },
                { "total_bytes", "123456" },
                { "media_type", "image/png" },
                { "media_category", "dm_image" },
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaInit()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.MediaUploadInit(totalBytes : 123456L, mediaType : "image/png", mediaCategory : "dm_image")
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #5
0
        public async Task MediaUpload_Test()
        {
            using (var twitterApi = new TwitterApi())
                using (var image = TestUtils.CreateDummyImage())
                    using (var media = new MemoryImageMediaItem(image))
                    {
                        var mock = new Mock <IApiConnection>();
                        mock.Setup(x =>
                                   x.PostLazyAsync <TwitterUploadMediaResult>(
                                       new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
                                       null,
                                       new Dictionary <string, IMediaItem> {
                            { "media", media }
                        })
                                   )
                        .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));

                        twitterApi.apiConnection = mock.Object;

                        await twitterApi.MediaUpload(media)
                        .IgnoreResponse()
                        .ConfigureAwait(false);

                        mock.VerifyAll();
                    }
        }
예제 #6
0
        public async Task ListsUpdate_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterList>(
                               new Uri("lists/update.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "list_id", "12345" },
                    { "name", "hogehoge" },
                    { "description", "aaaa" },
                    { "mode", "private" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterList()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.ListsUpdate(12345L, name : "hogehoge", description : "aaaa", @private : true)
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #7
0
        public async Task DirectMessagesDestroy_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterDirectMessage>(
                               new Uri("direct_messages/destroy.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "id", "100" }
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage {
                    Id = 100L
                }));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.DirectMessagesDestroy(statusId : 100L)
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #8
0
        public async Task ListsMembersDestroy_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterUser>(
                               new Uri("lists/members/destroy.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "list_id", "12345" },
                    { "screen_name", "twitterapi" },
                    { "include_entities", "true" },
                    { "include_ext_alt_text", "true" },
                    { "tweet_mode", "extended" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterUser()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.ListsMembersDestroy(12345L, "twitterapi")
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #9
0
        public async Task StatusesUpdate_ExcludeReplyUserIdsEmptyTest()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterStatus>(
                           new Uri("statuses/update.json", UriKind.Relative),
                           new Dictionary <string, string> {
                { "status", "hogehoge" },
                { "include_entities", "true" },
                { "include_ext_alt_text", "true" },
                { "tweet_mode", "extended" },
                // exclude_reply_user_ids は空の場合には送信されない
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterStatus()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.StatusesUpdate("hogehoge", replyToId : null, mediaIds : null, excludeReplyUserIds : Array.Empty <long>())
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #10
0
        public async Task StatusesUpdate_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterStatus>(
                           new Uri("statuses/update.json", UriKind.Relative),
                           new Dictionary <string, string> {
                { "status", "hogehoge" },
                { "include_entities", "true" },
                { "include_ext_alt_text", "true" },
                { "tweet_mode", "extended" },
                { "in_reply_to_status_id", "100" },
                { "media_ids", "10,20" },
                { "auto_populate_reply_metadata", "true" },
                { "exclude_reply_user_ids", "100,200" },
                { "attachment_url", "https://twitter.com/twitterapi/status/22634515958" },
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterStatus()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.StatusesUpdate("hogehoge", replyToId : 100L, mediaIds : new[] { 10L, 20L },
                                            autoPopulateReplyMetadata : true, excludeReplyUserIds : new[] { 100L, 200L },
                                            attachmentUrl : "https://twitter.com/twitterapi/status/22634515958")
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #11
0
        public async Task UsersReportSpam_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterUser>(
                               new Uri("users/report_spam.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "screen_name", "twitterapi" },
                    { "tweet_mode", "extended" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterUser {
                    ScreenName = "twitterapi"
                }));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.UsersReportSpam(screenName : "twitterapi")
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #12
0
        public async Task StatusesRetweet_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterStatus>(
                               new Uri("statuses/retweet.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "id", "100" },
                    { "include_entities", "true" },
                    { "include_ext_alt_text", "true" },
                    { "tweet_mode", "extended" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterStatus()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.StatusesRetweet(100L)
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #13
0
        public async Task AccountUpdateProfileImage_Test()
        {
            using (var twitterApi = new TwitterApi())
                using (var image = TestUtils.CreateDummyImage())
                    using (var media = new MemoryImageMediaItem(image))
                    {
                        var mock = new Mock <IApiConnection>();
                        mock.Setup(x =>
                                   x.PostLazyAsync <TwitterUser>(
                                       new Uri("account/update_profile_image.json", UriKind.Relative),
                                       new Dictionary <string, string> {
                            { "include_entities", "true" },
                            { "include_ext_alt_text", "true" },
                            { "tweet_mode", "extended" },
                        },
                                       new Dictionary <string, IMediaItem> {
                            { "image", media }
                        })
                                   )
                        .ReturnsAsync(LazyJson.Create(new TwitterUser()));

                        twitterApi.apiConnection = mock.Object;

                        await twitterApi.AccountUpdateProfileImage(media)
                        .IgnoreResponse()
                        .ConfigureAwait(false);

                        mock.VerifyAll();
                    }
        }
예제 #14
0
        public async Task LoadJsonAsync_InvalidJsonTest()
        {
            var body = Encoding.UTF8.GetBytes("### Invalid JSON ###");
            using (var bodyStream = new MemoryStream(body))
            using (var response = new HttpResponseMessage())
            {
                response.Content = new StreamContent(bodyStream);

                using (var lazyJson = new LazyJson<string>(response))
                {
                    // この時点ではまだレスポンスボディは読まれない
                    Assert.Equal(0, bodyStream.Position);

                    var exception = await Assert.ThrowsAnyAsync<WebApiException>(() => lazyJson.LoadJsonAsync())
                        .ConfigureAwait(false);

                    Assert.IsType<SerializationException>(exception.InnerException);
                }
            }
        }
예제 #15
0
        public async Task LoadJsonAsync_Test()
        {
            var body = Encoding.UTF8.GetBytes("\"hogehoge\"");
            using (var bodyStream = new MemoryStream(body))
            using (var response = new HttpResponseMessage())
            {
                response.Content = new StreamContent(bodyStream);

                using (var lazyJson = new LazyJson<string>(response))
                {
                    // この時点ではまだレスポンスボディは読まれない
                    Assert.Equal(0, bodyStream.Position);

                    var result = await lazyJson.LoadJsonAsync()
                        .ConfigureAwait(false);

                    Assert.Equal("hogehoge", result);
                }
            }
        }
예제 #16
0
        public async Task ListsDestroy_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterList>(
                           new Uri("lists/destroy.json", UriKind.Relative),
                           new Dictionary <string, string> {
                { "list_id", "12345" },
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterList()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.ListsDestroy(12345L)
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #17
0
        public async Task FriendshipsDestroy_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterFriendship>(
                           new Uri("friendships/destroy.json", UriKind.Relative),
                           new Dictionary <string, string> {
                { "screen_name", "twitterapi" }
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterFriendship()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.FriendshipsDestroy(screenName : "twitterapi")
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #18
0
        public async Task MediaUploadFinalize_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostLazyAsync <TwitterUploadMediaResult>(
                           new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
                           new Dictionary <string, string> {
                { "command", "FINALIZE" },
                { "media_id", "11111" },
            })
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.MediaUploadFinalize(mediaId : 11111L)
            .IgnoreResponse()
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #19
0
        public async Task DirectMessagesEventsNew_Test()
        {
            var mock = new Mock <IApiConnection>();

            mock.Setup(x =>
                       x.PostJsonAsync <TwitterMessageEventSingle>(
                           new Uri("direct_messages/events/new.json", UriKind.Relative),
                           @"{
  ""event"": {
    ""type"": ""message_create"",
    ""message_create"": {
      ""target"": {
        ""recipient_id"": ""12345""
      },
      ""message_data"": {
        ""text"": ""hogehoge"",
        ""attachment"": {
          ""type"": ""media"",
          ""media"": {
            ""id"": ""67890""
          }
        }
      }
    }
  }
}")
                       )
            .ReturnsAsync(LazyJson.Create(new TwitterMessageEventSingle()));

            using var twitterApi     = new TwitterApi();
            twitterApi.apiConnection = mock.Object;

            await twitterApi.DirectMessagesEventsNew(recipientId : 12345L, text : "hogehoge", mediaId : 67890L)
            .ConfigureAwait(false);

            mock.VerifyAll();
        }
예제 #20
0
        public async Task DirectMessagesNew_Test()
        {
            using (var twitterApi = new TwitterApi())
            {
                var mock = new Mock <IApiConnection>();
                mock.Setup(x =>
                           x.PostLazyAsync <TwitterDirectMessage>(
                               new Uri("direct_messages/new.json", UriKind.Relative),
                               new Dictionary <string, string> {
                    { "text", "hogehoge" },
                    { "screen_name", "opentween" },
                })
                           )
                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));

                twitterApi.apiConnection = mock.Object;

                await twitterApi.DirectMessagesNew("hogehoge", "opentween")
                .IgnoreResponse()
                .ConfigureAwait(false);

                mock.VerifyAll();
            }
        }
예제 #21
0
        public async Task IgnoreResponse_Test()
        {
            using (var bodyStream = new InvalidStream())
            using (var response = new HttpResponseMessage())
            {
                // IgnoreResponse() によってレスポンスの Stream が読まれずに破棄されることをテストするため、
                // 読み込みが行われると IOException が発生する InvalidStream クラスを bodyStream に使用している
                response.Content = new StreamContent(bodyStream);

                using (var lazyJson = new LazyJson<string>(response))
                {
                    // レスポンスボディを読まずに破棄
                    await Task.FromResult(lazyJson)
                        .IgnoreResponse()
                        .ConfigureAwait(false);

                    Assert.True(bodyStream.IsDisposed);
                }
            }
        }