{
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);
}
shortUrl.DisableExpanding = true;
- // http://t.co/hoge1 -> http://example.com/hoge2
+ // https://t.co/hoge1 -> http://example.com/hoge2
handler.Enqueue(x =>
{
// このリクエストは実行されないはず
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);
}
{
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);
}
{
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 =>
{
// このリクエストは実行されないはず
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();
{
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);
}
{
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");
});
{
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);
}
{
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);
}