OSDN Git Service

短縮URL展開時にHTTPSが使用できるドメインは強制的にHTTPSを使用する
[opentween/open-tween.git] / OpenTween.Tests / ShortUrlTest.cs
index fd1ed82..2fed93b 100644 (file)
@@ -42,17 +42,17 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://example.com/hoge2
+                // https://t.co/hoge1 -> http://example.com/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
                     return this.CreateRedirectResponse("http://example.com/hoge2");
                 });
 
                 Assert.Equal(new Uri("http://example.com/hoge2"),
-                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
+                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));
 
                 Assert.Equal(0, handler.QueueCount);
             }
@@ -92,7 +92,7 @@ namespace OpenTween
 
                 shortUrl.DisableExpanding = true;
 
-                // http://t.co/hoge1 -> http://example.com/hoge2
+                // https://t.co/hoge1 -> http://example.com/hoge2
                 handler.Enqueue(x =>
                 {
                     // このリクエストは実行されないはず
@@ -100,8 +100,8 @@ namespace OpenTween
                     return this.CreateRedirectResponse("http://example.com/hoge2");
                 });
 
-                Assert.Equal(new Uri("http://t.co/hoge1"),
-                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
+                Assert.Equal(new Uri("https://t.co/hoge1"),
+                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));
 
                 Assert.Equal(1, handler.QueueCount);
             }
@@ -115,26 +115,26 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://bit.ly/hoge2
+                // https://t.co/hoge1 -> https://bit.ly/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
-                    return this.CreateRedirectResponse("http://bit.ly/hoge2");
+                    return this.CreateRedirectResponse("https://bit.ly/hoge2");
                 });
 
-                // http://bit.ly/hoge2 -> http://example.com/hoge3
+                // https://bit.ly/hoge2 -> http://example.com/hoge3
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
+                    Assert.Equal(new Uri("https://bit.ly/hoge2"), x.RequestUri);
 
                     return this.CreateRedirectResponse("http://example.com/hoge3");
                 });
 
                 Assert.Equal(new Uri("http://example.com/hoge3"),
-                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
+                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));
 
                 Assert.Equal(0, handler.QueueCount);
             }
@@ -148,25 +148,25 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://bit.ly/hoge2
+                // https://t.co/hoge1 -> https://bit.ly/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
-                    return this.CreateRedirectResponse("http://bit.ly/hoge2");
+                    return this.CreateRedirectResponse("https://bit.ly/hoge2");
                 });
 
-                // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
+                // https://bit.ly/hoge2 -> https://tinyurl.com/hoge3
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);
+                    Assert.Equal(new Uri("https://bit.ly/hoge2"), x.RequestUri);
 
-                    return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
+                    return this.CreateRedirectResponse("https://tinyurl.com/hoge3");
                 });
 
-                // http://tinyurl.com/hoge3 -> http://example.com/hoge4
+                // https://tinyurl.com/hoge3 -> http://example.com/hoge4
                 handler.Enqueue(x =>
                 {
                     // このリクエストは実行されないはず
@@ -174,14 +174,64 @@ namespace OpenTween
                     return this.CreateRedirectResponse("http://example.com/hoge4");
                 });
 
-                Assert.Equal(new Uri("http://tinyurl.com/hoge3"),
-                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1"), redirectLimit: 2));
+                Assert.Equal(new Uri("https://tinyurl.com/hoge3"),
+                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1"), redirectLimit: 2));
 
                 Assert.Equal(1, handler.QueueCount);
             }
         }
 
         [Fact]
+        public async Task ExpandUrlAsync_UpgradeToHttpsTest()
+        {
+            var handler = new HttpMessageHandlerMock();
+            using (var http = new HttpClient(handler))
+            {
+                var shortUrl = new ShortUrl(http);
+
+                // http://t.co/hoge -> http://example.com/hoge
+                handler.Enqueue(x =>
+                {
+                    // https:// に変換されてリクエストが送信される
+                    Assert.Equal(HttpMethod.Head, x.Method);
+                    Assert.Equal(new Uri("https://t.co/hoge"), x.RequestUri);
+
+                    return this.CreateRedirectResponse("http://example.com/hoge");
+                });
+
+                Assert.Equal(new Uri("http://example.com/hoge"),
+                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge")));
+
+                Assert.Equal(0, handler.QueueCount);
+            }
+        }
+
+        [Fact]
+        public async Task ExpandUrlAsync_InsecureDomainTest()
+        {
+            var handler = new HttpMessageHandlerMock();
+            using (var http = new HttpClient(handler))
+            {
+                var shortUrl = new ShortUrl(http);
+
+                // http://htn.to/hoge -> http://example.com/hoge
+                handler.Enqueue(x =>
+                {
+                    // HTTPS非対応のドメインは http:// のままリクエストが送信される
+                    Assert.Equal(HttpMethod.Head, x.Method);
+                    Assert.Equal(new Uri("http://htn.to/hoge"), x.RequestUri);
+
+                    return this.CreateRedirectResponse("http://example.com/hoge");
+                });
+
+                Assert.Equal(new Uri("http://example.com/hoge"),
+                    await shortUrl.ExpandUrlAsync(new Uri("http://htn.to/hoge")));
+
+                Assert.Equal(0, handler.QueueCount);
+            }
+        }
+
+        [Fact]
         public async Task ExpandUrlAsync_RelativeUriTest()
         {
             var handler = new HttpMessageHandlerMock();
@@ -246,17 +296,17 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://example.com/hoge2
+                // https://t.co/hoge1 -> http://example.com/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
                     return this.CreateRedirectResponse("http://example.com/hoge2");
                 });
 
                 Assert.Equal("http://example.com/hoge2",
-                    await shortUrl.ExpandUrlAsync("http://t.co/hoge1"));
+                    await shortUrl.ExpandUrlAsync("https://t.co/hoge1"));
 
                 Assert.Equal(0, handler.QueueCount);
             }
@@ -270,11 +320,11 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://example.com/hoge2
+                // https://t.co/hoge1 -> http://example.com/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
                     return this.CreateRedirectResponse("http://example.com/hoge2");
                 });
@@ -317,14 +367,14 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> 503 Service Unavailable
+                // https://t.co/hoge1 -> 503 Service Unavailable
                 handler.Enqueue(x =>
                 {
                     return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
                 });
 
-                Assert.Equal(new Uri("http://t.co/hoge1"),
-                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));
+                Assert.Equal(new Uri("https://t.co/hoge1"),
+                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));
 
                 Assert.Equal(0, handler.QueueCount);
             }
@@ -338,17 +388,17 @@ namespace OpenTween
             {
                 var shortUrl = new ShortUrl(http);
 
-                // http://t.co/hoge1 -> http://example.com/hoge2
+                // https://t.co/hoge1 -> http://example.com/hoge2
                 handler.Enqueue(x =>
                 {
                     Assert.Equal(HttpMethod.Head, x.Method);
-                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);
+                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);
 
                     return this.CreateRedirectResponse("http://example.com/hoge2");
                 });
 
                 Assert.Equal("<a href=\"http://example.com/hoge2\">hogehoge</a>",
-                    await shortUrl.ExpandUrlHtmlAsync("<a href=\"http://t.co/hoge1\">hogehoge</a>"));
+                    await shortUrl.ExpandUrlHtmlAsync("<a href=\"https://t.co/hoge1\">hogehoge</a>"));
 
                 Assert.Equal(0, handler.QueueCount);
             }