예제 #1
0
        public void ArticleClient_GetTagEntriesAsync_find_test()
        {
            #region Setup

            Guid articleId  = Guid.NewGuid();
            Guid tagId      = Guid.NewGuid();
            Guid tagEntryId = Guid.NewGuid();

            var handler = new Mock <HttpMessageHandler>();
            handler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()).Returns((HttpRequestMessage request, CancellationToken cancellationToken) =>
            {
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                if (request.RequestUri.ToString().EndsWith("NA"))
                {
                    httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.NotFound);
                    httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new CollectionModelBase <ArticleTagEntry>()));
                }
                else if (request.RequestUri.ToString().EndsWith("Tag"))
                {
                    httpResponseMessage         = new HttpResponseMessage(HttpStatusCode.OK);
                    httpResponseMessage.Content = new StringContent(JsonConvert.SerializeObject(new CollectionModelBase <ArticleTagEntry>()
                    {
                        Item = new List <ArticleTagEntry>()
                        {
                            new ArticleTagEntry()
                            {
                                Id = tagEntryId, ArticleId = articleId, TagId = tagId
                            }
                        }
                    }));
                }
                return(Task.FromResult(httpResponseMessage));
            });

            Mock <HttpClientFactory> httpClientFactory = new Mock <HttpClientFactory>(new Mock <IDependencyResolver>().Object);
            httpClientFactory.Setup(p => p.Create()).Returns(() =>
            {
                return(new HttpClient(handler.Object));
            });

            Mock <IBaasicClientFactory> baasicClientFactory = new Mock <IBaasicClientFactory>();
            baasicClientFactory.Setup(f => f.Create(It.IsAny <IClientConfiguration>())).Returns((IClientConfiguration config) => new BaasicClient(config, httpClientFactory.Object, new JsonFormatter()));

            Mock <IClientConfiguration> clientConfiguration = new Mock <IClientConfiguration>();
            clientConfiguration.Setup(p => p.DefaultMediaType).Returns(ClientConfiguration.HalJsonMediaType);
            clientConfiguration.Setup(p => p.DefaultTimeout).Returns(TimeSpan.FromSeconds(1));
            clientConfiguration.Setup(p => p.ApplicationIdentifier).Returns("Test");
            clientConfiguration.Setup(p => p.SecureBaseAddress).Returns("https://api.baasic.com/v1");
            clientConfiguration.Setup(p => p.BaseAddress).Returns("http://api.baasic.com/v1");

            ArticleClient target = new ArticleClient(clientConfiguration.Object, baasicClientFactory.Object);

            target.Should().NotBeNull();

            #endregion Setup

            var expected = target.FindTagEntriesAsync(articleId, "NA", 1, 10, "", "").Result;
            expected.Should().NotBeNull();
            expected.Item.Should().NotBeNull();
            expected.Item.Count.Should().Be(0);

            expected = target.FindTagEntriesAsync(articleId, "Tag", 1, 10, "", "").Result;
            expected.Should().NotBeNull();
            expected.Item.Should().NotBeNull();
            expected.Item.First().TagId.Should().Be(tagId);
        }