예제 #1
0
        public void Get_returns_bad_request_when_empty_url_requested()
        {
            // act
            var result           = _sut.Get(string.Empty);
            var badRequestResult = result as BadRequestResult;

            // assert
            Assert.NotNull(badRequestResult);
            Assert.Equal(400, badRequestResult.StatusCode);
        }
예제 #2
0
        public void GetShouldReturnViewModel()
        {
            // Arrange
            var date1 = new DateTime(2012, 10, 12, 1, 2, 3);
            var date2 = new DateTime(2014, 2, 2, 4, 2, 3);

            var links = new[]
            {
                new Link
                {
                    Id           = 1,
                    CreationDate = date1,
                    Url          = "some url",
                    Redirects    = 18
                },
                new Link
                {
                    Id           = 2,
                    CreationDate = date2,
                    Url          = "http://doma.in/url",
                    Redirects    = 42
                }
            };

            var linkService = A.Fake <IDataLinkService>();

            A.CallTo(() => linkService.GetLinks(A <long> .Ignored))
            .Returns(links);

            var linkBuilder = A.Fake <IShortLinkBuilder>();

            A.CallTo(() => linkBuilder.Build(A <ActionContext> .Ignored, A <long> .Ignored))
            .ReturnsLazily((ActionContext context, long id) => $"ZXA{id}");

            var controller = new LinkController(linkService, linkBuilder, CreateTokenServiceMock());

            // Act
            var result = (controller.Get().Value as IEnumerable <LinkViewModel>).ToArray();

            // Assert
            Assert.That(result[0].CreationDate, Is.EqualTo(date1));
            Assert.That(result[0].OriginalUrl, Is.EqualTo("some url"));
            Assert.That(result[0].RedirectsCount, Is.EqualTo(18));
            Assert.That(result[0].ShortUrl, Is.EqualTo("ZXA1"));

            Assert.That(result[1].CreationDate, Is.EqualTo(date2));
            Assert.That(result[1].OriginalUrl, Is.EqualTo("http://doma.in/url"));
            Assert.That(result[1].RedirectsCount, Is.EqualTo(42));
            Assert.That(result[1].ShortUrl, Is.EqualTo("ZXA2"));
        }
예제 #3
0
        public async void Get_Not_Exists()
        {
            LinkControllerMockFacade mock = new LinkControllerMockFacade();

            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiLinkResponseModel>(null));
            LinkController controller = new LinkController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Get(default(int));

            response.Should().BeOfType <StatusCodeResult>();
            (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound);
            mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>()));
        }
예제 #4
0
        public async void Get_Exists()
        {
            LinkControllerMockFacade mock = new LinkControllerMockFacade();

            mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new ApiLinkServerResponseModel()));
            LinkController controller = new LinkController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Get(default(int));

            response.Should().BeOfType <OkObjectResult>();
            (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK);
            var record = (response as OkObjectResult).Value as ApiLinkServerResponseModel;

            record.Should().NotBeNull();
            mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>()));
        }