public async Task Given_ShortUrl_When_ExpandAsync_Invoked_Then_It_Should_Return_Result(string hostname, string shortUrl, string original) { var shorten = new Mock <ShortenUrlSettings>(); shorten.SetupGet(p => p.Hostname).Returns(hostname); var settings = this._mocker.CreateAppSettingsInstance(); settings.SetupGet(p => p.ShortenUrl).Returns(shorten.Object); var item = new UrlItemEntity() { ShortUrl = shortUrl, OriginalUrl = new Uri(original) }; var query = new Mock <IQuery>(); query.Setup(p => p.GetUrlItemEntityAsync(It.IsAny <string>())).ReturnsAsync(item); var command = new Mock <ICommand>(); var service = new ExpanderService(settings.Object, query.Object, command.Object); var payload = new ExpanderRequest() { ShortUrl = shortUrl }; var result = await service.ExpandAsync(payload).ConfigureAwait(false); result.ShortUrl.Should().Be(shortUrl); result.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/')); result.Shortened.ToString().TrimEnd('/').Should().Be($"https://{hostname}/{shortUrl}"); }
public void Given_Payload_Without_Original_When_Serialised_Then_It_Should_Throw_Exception() { var payload = new ExpanderRequest(); Action action = () => JsonConvert.SerializeObject(payload); action.Should().Throw <JsonSerializationException>(); }
public async Task Given_Request_When_ExpandAsync_Invoked_Then_It_Should_Return_Result(string original, string shortUrl, string title, string description, string owner, params string[] coOwners) { var shortener = new Mock <IShortenerService>(); var shortened = $"https://dvrl.kr/{(string.IsNullOrWhiteSpace(shortUrl) ? "helloworld" : shortUrl)}"; var response = new ExpanderResponse() { Original = new Uri(original), Shortened = new Uri(shortened), ShortUrl = shortUrl, Title = title, Description = description, Owner = owner, CoOwners = coOwners.ToList() }; var expander = new Mock <IExpanderService>(); expander.Setup(p => p.ExpandAsync(It.IsAny <ExpanderRequest>())).ReturnsAsync(response); var request = new ExpanderRequest(); var url = new Url(shortener.Object, expander.Object); typeof(Url).GetProperty("ExpanderRequest", BindingFlags.Public | BindingFlags.Instance) .SetValue(url, request); var result = await url.ExpandAsync() .ConfigureAwait(false); result.Should().BeOfType <Url>() .And.BeAssignableTo <IUrl>(); result.ExpanderResponse.Should().NotBeNull(); result.ExpanderResponse.Original.ToString().TrimEnd('/').Should().Be(original.TrimEnd('/')); result.ExpanderResponse.Shortened.ToString().TrimEnd('/').Should().Be(shortened.TrimEnd('/')); result.ExpanderResponse.ShortUrl.Should().Be(shortUrl); result.ExpanderResponse.Title.Should().Be(title); result.ExpanderResponse.Description.Should().Be(description); result.ExpanderResponse.Owner.Should().Be(owner); result.ExpanderResponse.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); }
/// <inheritdoc/> public async Task <ExpanderResponse> ExpandAsync(ExpanderRequest payload) { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } var item = await this._query.GetUrlItemEntityAsync(payload.ShortUrl); if (item == null) { return(null); } var response = new ExpanderResponse(item); response.Shortened = new Uri($"https://{this._settings.ShortenUrl.Hostname.TrimEnd('/')}/{item.ShortUrl}"); return(response); }
public async Task Given_Request_When_ExpandAsync_Invoked_Then_It_Should_Return_Null() { var shortener = new Mock <IShortenerService>(); var expander = new Mock <IExpanderService>(); expander.Setup(p => p.ExpandAsync(It.IsAny <ExpanderRequest>())).ReturnsAsync((ExpanderResponse)null); var request = new ExpanderRequest(); var url = new Url(shortener.Object, expander.Object); typeof(Url).GetProperty("ExpanderRequest", BindingFlags.Public | BindingFlags.Instance) .SetValue(url, request); var result = await url.ExpandAsync() .ConfigureAwait(false); result.Should().BeOfType <Url>() .And.BeAssignableTo <IUrl>(); result.ExpanderResponse.Should().BeNull(); }
/// <summary> /// Extracts <see cref="ExpanderRequest"/> from the given value. /// </summary> /// <param name="req"><see cref="HttpRequest"/> instance.</param> /// <param name="shortUrl">Short URL value.</param> /// <returns>Returns <see cref="ExpanderRequest"/> instance.</returns> public static async Task <ExpanderRequest> GetExpanderRequestAsync(this HttpRequest req, string shortUrl) { if (req == null) { throw new ArgumentNullException(nameof(req)); } if (string.IsNullOrWhiteSpace(shortUrl)) { throw new ArgumentNullException(nameof(shortUrl)); } if (req.Method.ToLowerInvariant() != "get") { throw new InvalidOperationException(); } var request = new ExpanderRequest() { ShortUrl = shortUrl }; return(await Task.FromResult(request).ConfigureAwait(false)); }