ExpandUrlAsync() public method

短縮 URL を非同期に展開します
public ExpandUrlAsync ( Uri uri ) : Task
uri System.Uri 展開するURL
return Task
コード例 #1
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_RecursiveTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // https://t.co/hoge1 -> https://bit.ly/hoge2
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://t.co/hoge1"), x.RequestUri);

                    return(this.CreateRedirectResponse("https://bit.ly/hoge2"));
                });

                // https://bit.ly/hoge2 -> http://example.com/hoge3
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    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("https://t.co/hoge1")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #2
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_RelativeRedirectTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // Location に相対 URL を指定したリダイレクト (テストに使う URL は適当)
                // https://t.co/hogehoge -> /tetetete
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://t.co/hogehoge"), x.RequestUri);

                    return(this.CreateRedirectResponse("/tetetete", UriKind.Relative));
                });

                // https://t.co/tetetete -> http://example.com/tetetete
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://t.co/tetetete"), x.RequestUri);

                    return(this.CreateRedirectResponse("http://example.com/tetetete"));
                });

                Assert.Equal(new Uri("http://example.com/tetetete"),
                             await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hogehoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #3
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_HttpErrorTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // https://t.co/hoge1 -> 503 Service Unavailable
                handler.Enqueue(x =>
                {
                    return(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable));
                });

                Assert.Equal(new Uri("https://t.co/hoge1"),
                             await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #4
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_String_InvalidUrlTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(x =>
                {
                    // リクエストは送信されないはず
                    Assert.True(false);
                    return(this.CreateRedirectResponse("http://example.com/hoge2"));
                });

                // 不正なURL
                Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlAsync("..hogehoge.."));

                Assert.Equal(1, handler.QueueCount);
            }
        }
コード例 #5
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_RelativeUriTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(x =>
                {
                    // このリクエストは実行されないはず
                    Assert.True(false);
                    return(this.CreateRedirectResponse(""));
                });

                // 相対 URI に対しては何も行わない
                Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
                             await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));

                Assert.Equal(1, handler.QueueCount);
            }
        }
コード例 #6
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_Test()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // http://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);

                    return this.CreateRedirectResponse("http://example.com/hoge2");
                });

                Assert.Equal(new Uri("http://example.com/hoge2"),
                    await shortUrl.ExpandUrlAsync(new Uri("http://t.co/hoge1")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #7
0
ファイル: ShortUrlTest.cs プロジェクト: urusupa/OpenTween
        public async Task ExpandUrlAsync_IrregularUrlTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // https://www.flickr.com/photo.gne?short=hoge -> /photos/foo/11111/
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://www.flickr.com/photo.gne?short=hoge"), x.RequestUri);

                    return this.CreateRedirectResponse("/photos/foo/11111/", UriKind.Relative);
                });

                Assert.Equal(new Uri("https://www.flickr.com/photos/foo/11111/"),
                    await shortUrl.ExpandUrlAsync(new Uri("https://www.flickr.com/photo.gne?short=hoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #8
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_DisableExpandingTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                shortUrl.DisableExpanding = true;

                // http://t.co/hoge1 -> http://example.com/hoge2
                handler.Enqueue(x =>
                {
                    // このリクエストは実行されないはず
                    Assert.True(false);
                    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(1, handler.QueueCount);
            }
        }
コード例 #9
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_IrregularUrlTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // https://www.flickr.com/photo.gne?short=hoge -> /photos/foo/11111/
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://www.flickr.com/photo.gne?short=hoge"), x.RequestUri);

                    return(this.CreateRedirectResponse("/photos/foo/11111/", UriKind.Relative));
                });

                Assert.Equal(new Uri("https://www.flickr.com/photos/foo/11111/"),
                             await shortUrl.ExpandUrlAsync(new Uri("https://www.flickr.com/photo.gne?short=hoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #10
0
ファイル: ShortUrlTest.cs プロジェクト: togtog/OpenTween
        public async Task ExpandUrlAsync_String_SchemeLessUrlTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // http://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);

                    return(this.CreateRedirectResponse("http://example.com/hoge2"));
                });

                // スキームが省略されたURL
                Assert.Equal("http://example.com/hoge2",
                             await shortUrl.ExpandUrlAsync("t.co/hoge1"));

                Assert.Equal(0, handler.QueueCount);
            }
        }
コード例 #11
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        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);
            }
        }
コード例 #12
0
ファイル: ShortUrlTest.cs プロジェクト: tsubasa/OpenTween
        public async Task ExpandUrlAsync_DisableExpandingTest()
        {
            var handler = new HttpMessageHandlerMock();

            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                shortUrl.DisableExpanding = true;

                // https://t.co/hoge1 -> http://example.com/hoge2
                handler.Enqueue(x =>
                {
                    // このリクエストは実行されないはず
                    Assert.True(false);
                    return(this.CreateRedirectResponse("http://example.com/hoge2"));
                });

                Assert.Equal(new Uri("https://t.co/hoge1"),
                             await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hoge1")));

                Assert.Equal(1, handler.QueueCount);
            }
        }
コード例 #13
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_HttpErrorTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // http://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(0, handler.QueueCount);
            }
        }
コード例 #14
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_String_InvalidUrlTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(x =>
                {
                    // リクエストは送信されないはず
                    Assert.True(false);
                    return this.CreateRedirectResponse("http://example.com/hoge2");
                });

                // 不正なURL
                Assert.Equal("..hogehoge..", await shortUrl.ExpandUrlAsync("..hogehoge.."));

                Assert.Equal(1, handler.QueueCount);
            }
        }
コード例 #15
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_RelativeUriTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(x =>
                {
                    // このリクエストは実行されないはず
                    Assert.True(false);
                    return this.CreateRedirectResponse("");
                });

                // 相対 URI に対しては何も行わない
                Assert.Equal(new Uri("./foo/bar", UriKind.Relative),
                    await shortUrl.ExpandUrlAsync(new Uri("./foo/bar", UriKind.Relative)));

                Assert.Equal(1, handler.QueueCount);
            }
        }
コード例 #16
0
ファイル: ShortUrlTest.cs プロジェクト: egcube/OpenTween
        public async Task ExpandUrlAsync_RecursiveLimitTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // http://t.co/hoge1 -> http://bit.ly/hoge2
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("http://t.co/hoge1"), x.RequestUri);

                    return this.CreateRedirectResponse("http://bit.ly/hoge2");
                });

                // http://bit.ly/hoge2 -> http://tinyurl.com/hoge3
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("http://bit.ly/hoge2"), x.RequestUri);

                    return this.CreateRedirectResponse("http://tinyurl.com/hoge3");
                });

                // http://tinyurl.com/hoge3 -> http://example.com/hoge4
                handler.Enqueue(x =>
                {
                    // このリクエストは実行されないはず
                    Assert.True(false);
                    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(1, handler.QueueCount);
            }
        }
コード例 #17
0
ファイル: ShortUrlTest.cs プロジェクト: urusupa/OpenTween
        public async Task ExpandUrlAsync_RelativeRedirectTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                // Location に相対 URL を指定したリダイレクト (テストに使う URL は適当)
                // https://t.co/hogehoge -> /tetetete
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://t.co/hogehoge"), x.RequestUri);

                    return this.CreateRedirectResponse("/tetetete", UriKind.Relative);
                });

                // https://t.co/tetetete -> http://example.com/tetetete
                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Head, x.Method);
                    Assert.Equal(new Uri("https://t.co/tetetete"), x.RequestUri);

                    return this.CreateRedirectResponse("http://example.com/tetetete");
                });

                Assert.Equal(new Uri("http://example.com/tetetete"),
                    await shortUrl.ExpandUrlAsync(new Uri("https://t.co/hogehoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }