Example #1
0
        public async Task CreateShortUrlAsync_NonHttpUrl_ReturnsError(ShortenerService sut)
        {
            // When
            var result = await sut.CreateShortUrlAsync("ftp://ftp.shouldfail.com/");

            // Then
            Assert.IsNull(result.Item1);
            Assert.IsNotEmpty(result.Item2);
        }
Example #2
0
        public async Task CreateShortUrlAsync_ExistingLongUrl_ReturnsSameShortCode(
            string fakeShortCode,
            [Frozen] Mock <IShortenUrlRepository> mockShortUrlRepository,
            ShortenerService sut)
        {
            // Given
            mockShortUrlRepository.Setup(m => m.FindShortCode(It.IsAny <string>()))
            .Returns(fakeShortCode);

            // When
            var result = await sut.CreateShortUrlAsync("http://www.google.com/");

            // Then
            Assert.AreEqual(result.Item1, fakeShortCode); // shortcode result
            Assert.IsNull(result.Item2);                  // error should be null
        }
Example #3
0
        public async Task CreateShortUrlAsync_ValidLongUrl_ReturnsNewShortCode(
            [Frozen] Mock <IShortenUrlRepository> mockShortUrlRepository,
            ShortenerService sut)
        {
            // Given
            mockShortUrlRepository.Setup(m => m.FindShortCode(It.IsAny <string>()))
            .Returns(string.Empty);

            // When
            var result = await sut.CreateShortUrlAsync("http://www.google.com/");

            // Then
            mockShortUrlRepository.Verify(m => m.SaveAsync(result.Item1, It.IsAny <string>()));

            Assert.IsNotEmpty(result.Item1); // shortcode result
            Assert.IsNull(result.Item2);     // error should be null
        }
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req)
        {
            // Get request body
            dynamic data = await req.Content.ReadAsAsync <object>();

            string longUrl = data?.url;

            if (string.IsNullOrWhiteSpace(longUrl))
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }

            var service = new ShortenerService();
            var result  = await service.CreateShortUrlAsync(longUrl.Trim());

            return(result.Item1 == null
                ? req.CreateErrorResponse(HttpStatusCode.BadRequest, result.Item2)
                : req.CreateResponse(HttpStatusCode.Created, result.Item1));
        }