Beispiel #1
0
        public void Given_Request_When_ValidateAsync_Invoked_Then_It_Should_Throw_Exception(string original, string shortUrl, bool exists)
        {
            var request = new ShortenerRequest()
            {
                Friendly = shortUrl
            };

            var shortener = new Mock <IShortenerService>();

            shortener.Setup(p => p.ExistsAsync(It.IsAny <string>())).ReturnsAsync(exists);

            var expander = new Mock <IExpanderService>();

            var url = new Url(shortener.Object, expander.Object);

            typeof(Url).GetProperty("ShortenerRequest", BindingFlags.Public | BindingFlags.Instance)
            .SetValue(url, request);

            Func <Task> func = async() => await url.ValidateAsync().ConfigureAwait(false);

            func.Should().Throw <UrlExistsException>()
            .And.ShortUrl.Should().Be(shortUrl);

            url.IsFriendlyUrlValidated.Should().BeFalse();
        }
Beispiel #2
0
        public async Task Given_FriendlyUrl_When_ShortenAsync_Invoked_Then_It_Should_Return_Result(string hostname, int length, string original, string friendly)
        {
            var shortenUrl = new Mock <ShortenUrlSettings>();

            shortenUrl.SetupGet(p => p.Hostname).Returns(hostname);
            shortenUrl.SetupGet(p => p.Length).Returns(length);

            var settings = this._mocker.CreateAppSettingsInstance();

            settings.SetupGet(p => p.ShortenUrl).Returns(shortenUrl.Object);

            var query   = new Mock <IQuery>();
            var command = new Mock <ICommand>();

            var service = new ShortenerService(settings.Object, query.Object, command.Object);

            var payload = new ShortenerRequest()
            {
                Original = new Uri(original),
                Friendly = friendly
            };

            var result = await service.ShortenAsync(payload).ConfigureAwait(false);

            result.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/'));
            result.Shortened.ToString().TrimEnd('/').Should().Be($"https://{hostname}/{friendly.TrimEnd('/')}");
            result.ShortUrl.Should().Be(friendly);
        }
Beispiel #3
0
        public async Task Given_Request_When_ValidateAsync_Invoked_Then_It_Should_Return_True(string original, string shortUrl, bool exists)
        {
            var request = new ShortenerRequest()
            {
                Friendly = shortUrl
            };

            var shortener = new Mock <IShortenerService>();

            shortener.Setup(p => p.ExistsAsync(It.IsAny <string>())).ReturnsAsync(exists);

            var expander = new Mock <IExpanderService>();

            var url = new Url(shortener.Object, expander.Object);

            typeof(Url).GetProperty("ShortenerRequest", BindingFlags.Public | BindingFlags.Instance)
            .SetValue(url, request);

            var result = await url.ValidateAsync().ConfigureAwait(false);

            result.Should().BeOfType <Url>()
            .And.BeAssignableTo <IUrl>();

            url.IsFriendlyUrlValidated.Should().BeTrue();
        }
        private static async Task <ShortenerRequest> GetShortenerRequestFromQueryAsync(IQueryCollection query)
        {
            var original = query["original"];

            if (!original.Any())
            {
                throw new InvalidOperationException("Original URL is missing");
            }

            var owner = query["owner"];

            if (!owner.Any())
            {
                throw new InvalidOperationException("Owner is missing");
            }

            var friendly    = query["friendly"];
            var title       = query["title"];
            var description = query["description"];
            var coOwners    = query["coowners"];

            var request = new ShortenerRequest()
            {
                Original    = new Uri(original),
                Friendly    = friendly,
                Title       = title,
                Description = description,
                Owner       = owner,
                CoOwners    = coOwners.ToList()
            };

            return(await Task.FromResult(request).ConfigureAwait(false));
        }
        public void Given_Payload_Without_Original_When_Serialised_Then_It_Should_Throw_Exception()
        {
            var payload = new ShortenerRequest()
            {
                Friendly = "lorem ipsum"
            };

            Action action = () => JsonConvert.SerializeObject(payload);

            action.Should().Throw <JsonSerializationException>();
        }
Beispiel #6
0
        public async Task Given_Request_When_ShortenAsync_Invoked_Then_It_Should_Return_Result(string original, string shortUrl, string title, string description, string owner, params string[] coOwners)
        {
            var shortened = $"https://dvrl.kr/{(string.IsNullOrWhiteSpace(shortUrl) ? "helloworld" : shortUrl)}";
            var response  = new ShortenerResponse()
            {
                Original    = new Uri(original),
                Shortened   = new Uri(shortened),
                ShortUrl    = shortUrl,
                Title       = title,
                Description = description,
                Owner       = owner,
                CoOwners    = coOwners.ToList()
            };
            var shortener = new Mock <IShortenerService>();

            shortener.Setup(p => p.ShortenAsync(It.IsAny <ShortenerRequest>())).ReturnsAsync(response);
            shortener.SetupSequence(p => p.ExistsAsync(It.IsAny <string>()))
            .ReturnsAsync(true)
            .ReturnsAsync(false);

            var expander = new Mock <IExpanderService>();

            var request = new ShortenerRequest();

            var url = new Url(shortener.Object, expander.Object);

            typeof(Url).GetProperty("ShortenerRequest", BindingFlags.Public | BindingFlags.Instance)
            .SetValue(url, request);

            var result = await url.ShortenAsync()
                         .ConfigureAwait(false);

            result.Should().BeOfType <Url>()
            .And.BeAssignableTo <IUrl>();

            result.ShortenerResponse.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/'));
            result.ShortenerResponse.Shortened.ToString().TrimEnd('/').Should().Be(shortened.TrimEnd('/'));
            result.ShortenerResponse.ShortUrl.Should().Be(shortUrl);
            result.ShortenerResponse.Title.Should().Be(title);
            result.ShortenerResponse.Description.Should().Be(description);
            result.ShortenerResponse.Owner.Should().Be(owner);
            result.ShortenerResponse.CoOwners.Should().BeEquivalentTo(coOwners);

            url.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/'));
            url.Shortened.ToString().TrimEnd('/').Should().Be(shortened.TrimEnd('/'));
            url.ShortUrl.Should().Be(shortUrl);
            url.Title.Should().Be(title);
            url.Description.Should().Be(description);
            url.Owner.Should().Be(owner);
            url.CoOwners.Should().BeEquivalentTo(coOwners);
        }
Beispiel #7
0
        public async Task Given_RequestWithPost_When_GetRequestAsync_Invoked_Then_It_Should_Return_Result(string method, string original, string friendly, string title, string description, string owner, params string[] coOwners)
        {
            var shortener = new Mock <IShortenerService>();
            var expander  = new Mock <IExpanderService>();

            var url = new Url(shortener.Object, expander.Object);

            var payload = new ShortenerRequest()
            {
                Original    = new Uri(original),
                Friendly    = friendly,
                Title       = title,
                Description = description,
                Owner       = owner,
                CoOwners    = coOwners.ToList()
            };
            var serialised = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload));
            var body       = new MemoryStream(serialised);

            var req = new Mock <HttpRequest>();

            req.SetupGet(p => p.Method).Returns(method);
            req.SetupGet(p => p.Body).Returns(body);

            var result = await url.GetRequestAsync(req.Object)
                         .ConfigureAwait(false);

            result.Should().BeOfType <Url>()
            .And.BeAssignableTo <IUrl>();

            var request = result.ShortenerRequest;

            request.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/'));
            request.Friendly.Should().Be(friendly);
            request.Title.Should().Be(title);
            request.Description.Should().Be(description);
            request.Owner.Should().Be(owner);
            request.CoOwners.Should().BeEquivalentTo(coOwners);

            body.Dispose();
        }
Beispiel #8
0
        /// <inheritdoc/>
        public async Task <ShortenerResponse> ShortenAsync(ShortenerRequest payload)
        {
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            var response = new ShortenerResponse()
            {
                Original    = payload.Original,
                Title       = payload.Title,
                Description = payload.Description,
                Owner       = payload.Owner,
                CoOwners    = payload.CoOwners,
            };

            if (!string.IsNullOrWhiteSpace(payload.Friendly))
            {
                response.Shortened = new Uri($"https://{this._settings.ShortenUrl.Hostname}/{payload.Friendly}");
                response.ShortUrl  = payload.Friendly;

                return(await Task.FromResult(response).ConfigureAwait(false));
            }

            var sb = new StringBuilder();

            for (var i = 0; i < this._settings.ShortenUrl.Length; i++)
            {
                var index = random.Next(this._settings.ShortenUrl.Length);
                sb.Append(CharacterPool[index]);
            }

            response.Shortened = new Uri($"https://{this._settings.ShortenUrl.Hostname}/{sb.ToString()}");
            response.ShortUrl  = sb.ToString();

            return(await Task.FromResult(response).ConfigureAwait(false));
        }
        public async Task Given_Body_When_GetShortenerRequestAsync_Invoked_Then_It_Should_Return_Result(string method, string original, string friendly, string owner)
        {
            var payload = new ShortenerRequest()
            {
                Original = new Uri(original),
                Friendly = friendly,
                Owner    = owner
            };
            var serialised = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload));
            var body       = new MemoryStream(serialised);

            var req = new Mock <HttpRequest>();

            req.SetupGet(p => p.Method).Returns(method);
            req.SetupGet(p => p.Body).Returns(body);

            var result = await HttpRequestExtensions.GetShortenerRequestAsync(req.Object).ConfigureAwait(false);

            result.Should().NotBeNull();
            result.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/'));
            result.Friendly.Should().Be(friendly);

            body.Dispose();
        }