public void ShortenUrl_SignatureFromOptionsIsAdded()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.OK, SampleAnswer);

            fixture.Inject(httpClient);
            string code = "123456789";

            options.Signature = code;
            sut = fixture.Create <YourlsApi>();

            var param = "test";

            sut.ShortenUrl(param);

            string expectedQueryFragment = $"signature={code}";

            httpMessageHandlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri.ToString().Contains(expectedQueryFragment) // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
        public void ShortenUrl_RequestUrlIsCorect()
        {
            var asnswer =
                @"{'url':{'keyword':'3','url':'https:\/\/thebests.kotaku.com\/aa','title':'Aa - Gaming Reviews, News, Tips and More. | Kotaku','date':'2021-02-16 21:08:10','ip':'89.151.39.72'},'status':'success','message':'https:\/\/thebests.kotaku.com\/aa added to database','title':'Aa - Gaming Reviews, News, Tips and More. | Kotaku','shorturl':'https:\/\/wyrodek.pl\/x\/3','statusCode':200}";
            var httpClient = SetupMockedHttpClient(HttpStatusCode.OK, asnswer);

            fixture.Inject(httpClient);
            sut = fixture.Create <YourlsApi>();
            var param = "http://test.pl/test/org?params&param";

            var result = sut.ShortenUrl(param);

            Assert.That(result, Is.EqualTo("https://wyrodek.pl/x/3"));
        }
        public void ShortenUrl_WrongSignature_ErrorIsThrown()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.Forbidden, "{'message':'Please log in','errorCode':403,'callback':''}");

            fixture.Inject(httpClient);
            string code = "123456789";

            options.Signature = code;
            sut = fixture.Create <YourlsApi>();

            var param = "test";

            Assert.Throws <UnauthorizedAccessException>(
                () => sut.ShortenUrl(param));
        }
        public void ShortenUrl_UnexpectedStatus_ErrorIsThrown()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.NotFound, "{'message':'Please log in','errorCode':403,'callback':''}");

            fixture.Inject(httpClient);
            const string code = "123456789";

            options.Signature = code;
            sut = fixture.Create <YourlsApi>();

            var param = "test";

            Assert.Throws <ExternalException>(
                () => sut.ShortenUrl(param));
        }
        public void ShortenUrl_ReturnFormatIsSetToJson()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.OK, SampleAnswer);

            fixture.Inject(httpClient);
            sut = fixture.Create <YourlsApi>();

            var param = "http://test.pl/test/org?params&param";

            sut.ShortenUrl(param);

            var expectedQueryFragment = "format=json";

            httpMessageHandlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri.ToString().Contains(expectedQueryFragment) // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
        public List <Episode> ShortenAllLinks(List <Episode> episodes)
        {
            foreach (var episode in episodes)
            {
                foreach (var article  in episode.Articles)
                {
                    try
                    {
                        article.Link = api.ShortenUrl(article.Link);
                    }
                    catch (UnauthorizedAccessException ae)
                    {
                        logger.LogWarning($"An error with authorization to link shortener skiping rest of the linkts P{ae.Message}");
                        return(episodes);
                    }
                    catch (Exception e)
                    {
                        logger.LogWarning($"There was issue with url {article.Link} Error {e.Message}");
                    }
                }
            }

            return(episodes);
        }
        public void ShortenUrl_UsesAdressFromOptions()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.OK, SampleAnswer);

            fixture.Inject(httpClient);

            sut = fixture.Create <YourlsApi>();

            var param = "test";

            sut.ShortenUrl(param);



            httpMessageHandlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri.ToString().StartsWith("https://test.com.pl/x/yourls-api.php") // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }
        public void ShortenUrl_ActionIsSetToShortUrl()
        {
            var httpClient = SetupMockedHttpClient(HttpStatusCode.OK, "{'id':1,'value':'1'}");

            fixture.Inject(httpClient);

            sut = fixture.Create <YourlsApi>();

            const string param = "test";

            sut.ShortenUrl(param);

            var expectedQueryFragment = $"action=shorturl";

            httpMessageHandlerMock.Protected().Verify(
                "SendAsync",
                Times.Exactly(1), // we expected a single external request
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get && // we expected a GET request
                                               req.RequestUri.ToString().Contains(expectedQueryFragment) // to this uri
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }