OSDN Git Service

テストコード内で ConfigureAwait(false) を使用しない (xUnit1030)
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / TumblrTest.cs
index 27b0118..45ab7a3 100644 (file)
@@ -7,6 +7,7 @@ using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Web;
+using OpenTween.Models;
 using Xunit;
 
 namespace OpenTween.Thumbnail.Services
@@ -16,31 +17,33 @@ namespace OpenTween.Thumbnail.Services
         [Fact]
         public void ParsePostJson_Test()
         {
-            var json = @"{
-  ""meta"": { ""status"": 200, ""msg"": ""OK"" },
-  ""response"": {
-    ""blog"": { },
-    ""posts"": [
-      {
-        ""id"": 1234567,
-        ""post_url"": ""http://example.com/post/1234567"",
-        ""type"": ""photo"",
-        ""photos"": [
-          {
-            ""caption"": """",
-            ""alt_sizes"": [
-              {
-                ""width"": 1280,
-                ""height"": 722,
-                ""url"": ""http://example.com/photo/1280/1234567/1/tumblr_hogehoge""
-              }
-            ]
-          }
-        ]
-      }
-    ]
-  }
-}";
+            var json = """
+                {
+                  "meta": { "status": 200, "msg": "OK" },
+                  "response": {
+                    "blog": { },
+                    "posts": [
+                      {
+                        "id": 1234567,
+                        "post_url": "http://example.com/post/1234567",
+                        "type": "photo",
+                        "photos": [
+                          {
+                            "caption": "",
+                            "alt_sizes": [
+                              {
+                                "width": 1280,
+                                "height": 722,
+                                "url": "http://example.com/photo/1280/1234567/1/tumblr_hogehoge"
+                              }
+                            ]
+                          }
+                        ]
+                      }
+                    ]
+                  }
+                }
+                """;
             var jsonBytes = Encoding.UTF8.GetBytes(json);
             var thumbs = Tumblr.ParsePhotoPostJson(jsonBytes);
 
@@ -48,8 +51,8 @@ namespace OpenTween.Thumbnail.Services
             {
                 new ThumbnailInfo
                 {
-                    ImageUrl = "http://example.com/post/1234567",
-                    ThumbnailUrl = "http://example.com/photo/1280/1234567/1/tumblr_hogehoge",
+                    MediaPageUrl = "http://example.com/post/1234567",
+                    ThumbnailImageUrl = "http://example.com/photo/1280/1234567/1/tumblr_hogehoge",
                     TooltipText = null,
                 },
             };
@@ -69,25 +72,26 @@ namespace OpenTween.Thumbnail.Services
 
                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
 
-                Assert.Equal(ApplicationSettings.TumblrConsumerKey, query["api_key"]);
+                Assert.Equal("fake_api_key", query["api_key"]);
                 Assert.Equal("1234567", query["id"]);
 
                 return new HttpResponseMessage(HttpStatusCode.OK)
                 {
-                    Content = new StringContent(@"{
-  ""meta"": { ""status"": 200, ""msg"": ""OK"" },
-  ""response"": { ""blog"": { }, ""posts"": { } }
-}"),
+                    Content = new StringContent("""
+                        {
+                          "meta": { "status": 200, "msg": "OK" },
+                          "response": { "blog": { }, "posts": { } }
+                        }
+                        """),
                 };
             });
 
             using (var http = new HttpClient(handler))
             {
-                var service = new Tumblr(http);
+                var service = new Tumblr(ApiKey.Create("fake_api_key"), http);
 
                 var url = "http://hoge.tumblr.com/post/1234567/tetetete";
-                var thumb = await service.GetThumbnailInfoAsync(url, null, CancellationToken.None)
-                    .ConfigureAwait(false);
+                await service.GetThumbnailInfoAsync(url, new PostClass(), CancellationToken.None);
             }
 
             Assert.Equal(0, handler.QueueCount);
@@ -106,29 +110,43 @@ namespace OpenTween.Thumbnail.Services
 
                 var query = HttpUtility.ParseQueryString(x.RequestUri.Query);
 
-                Assert.Equal(ApplicationSettings.TumblrConsumerKey, query["api_key"]);
+                Assert.Equal("fake_api_key", query["api_key"]);
                 Assert.Equal("1234567", query["id"]);
 
                 return new HttpResponseMessage(HttpStatusCode.OK)
                 {
-                    Content = new StringContent(@"{
-  ""meta"": { ""status"": 200, ""msg"": ""OK"" },
-  ""response"": { ""blog"": { }, ""posts"": { } }
-}"),
+                    Content = new StringContent("""
+                        {
+                          "meta": { "status": 200, "msg": "OK" },
+                          "response": { "blog": { }, "posts": { } }
+                        }
+                        """),
                 };
             });
 
             using (var http = new HttpClient(handler))
             {
-                var service = new Tumblr(http);
+                var service = new Tumblr(ApiKey.Create("fake_api_key"), http);
 
                 // Tumblrのカスタムドメイン名を使ってるっぽいURL
                 var url = "http://tumblr.example.com/post/1234567/tetetete";
-                var thumb = await service.GetThumbnailInfoAsync(url, null, CancellationToken.None)
-                    .ConfigureAwait(false);
+                await service.GetThumbnailInfoAsync(url, new PostClass(), CancellationToken.None);
             }
 
             Assert.Equal(0, handler.QueueCount);
         }
+
+        [Fact]
+        public async Task GetThumbnailInfoAsync_ApiKeyErrorTest()
+        {
+            var handler = new HttpMessageHandlerMock();
+
+            using var http = new HttpClient(handler);
+            var service = new Tumblr(ApiKey.Create("%e%INVALID_API_KEY"), http);
+
+            var url = "http://hoge.tumblr.com/post/1234567/tetetete";
+            var thumb = await service.GetThumbnailInfoAsync(url, new PostClass(), CancellationToken.None);
+            Assert.Null(thumb);
+        }
     }
 }