ShortenUrlAsync() public method

指定された短縮URLサービスを使用してURLを短縮します
public ShortenUrlAsync ( MyCommon shortenerType, Uri srcUri ) : Task
shortenerType MyCommon 使用する短縮URLサービス
srcUri System.Uri 短縮するURL
return Task
Example #1
0
        public async Task ShortenUrlAsync_UxnuUrlTest()
        {
            var handler = new HttpMessageHandlerMock();

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

                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Get, x.Method);
                    Assert.Equal("https://ux.nu/api/short?format=plain&url=http:%2F%2Fexample.com%2Fhogehoge",
                                 x.RequestUri.AbsoluteUri);

                    return(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(Encoding.UTF8.GetBytes("https://ux.nu/hoge")),
                    });
                });

                Assert.Equal(new Uri("https://ux.nu/hoge"),
                             await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
Example #2
0
        public async Task ShortenUrlAsync_TinyUrlTest()
        {
            var handler = new HttpMessageHandlerMock();

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

                handler.Enqueue(async x =>
                {
                    Assert.Equal(HttpMethod.Post, x.Method);
                    Assert.Equal(new Uri("https://tinyurl.com/api-create.php"), x.RequestUri);
                    Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehogehoge", await x.Content.ReadAsStringAsync());

                    return(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
                    });
                });

                Assert.Equal(new Uri("https://tinyurl.com/hoge"),
                             await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehogehoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
Example #3
0
        public async Task ShortenUrlAsync_UxnuUrlTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(x =>
                {
                    Assert.Equal(HttpMethod.Get, x.Method);
                    Assert.Equal("http://ux.nu/api/short?format=plain&url=http://example.com/hogehoge",
                        x.RequestUri.ToString());

                    return new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://ux.nu/hoge")),
                    };
                });

                Assert.Equal(new Uri("http://ux.nu/hoge"),
                    await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.Uxnu, new Uri("http://example.com/hogehoge")));

                Assert.Equal(0, handler.QueueCount);
            }
        }
Example #4
0
        public async Task ShortenUrlAsync_TinyUrlTest()
        {
            var handler = new HttpMessageHandlerMock();
            using (var http = new HttpClient(handler))
            {
                var shortUrl = new ShortUrl(http);

                handler.Enqueue(async x =>
                {
                    Assert.Equal(HttpMethod.Post, x.Method);
                    Assert.Equal(new Uri("http://tinyurl.com/api-create.php"), x.RequestUri);
                    Assert.Equal("url=http%3A%2F%2Fexample.com%2Fhogehoge", await x.Content.ReadAsStringAsync());

                    return new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(Encoding.UTF8.GetBytes("http://tinyurl.com/hoge")),
                    };
                });

                Assert.Equal(new Uri("http://tinyurl.com/hoge"),
                    await shortUrl.ShortenUrlAsync(MyCommon.UrlConverter.TinyUrl, new Uri("http://example.com/hogehoge")));

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