예제 #1
0
        public void GetCategoryById()
        {
            string serialized            = File.ReadAllText("Resources/Responses/GetCategoryById.json");
            var    mockHttpClientFactory = new Mock <IHttpClientFactory>();
            var    mockHandler           = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mockHttpClientFactory.Setup(x => x.CreateClient(DefaultClientNames.Api)).Returns(new HttpClient(mockHandler.Object)
            {
                BaseAddress = new Uri("https://api.sphere.io/test-project/")
            });
            mockHandler
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(serialized)
            })
            .Verifiable();
            IClient commerceToolsClient = new CtpClient(
                mockHttpClientFactory.Object,
                this.clientFixture.GetService <IHttpApiCommandFactory>(),
                this.clientFixture.GetService <ISerializerService>(),
                this.clientFixture.GetService <IUserAgentProvider>()
                );
            string   categoryId = "2bafc816-4223-4ff0-ac8a-0f08a8f29fd6";
            Category category   = commerceToolsClient.ExecuteAsync(new GetByIdCommand <Category>(new Guid(categoryId))).Result;

            Assert.Equal(categoryId, category.Id.ToString());
        }
예제 #2
0
        public void GetMessageByIdForCategoryCreatedAction()
        {
            string serialized            = File.ReadAllText("Resources/Responses/CategoryCreatedMessage.json");
            var    mockHttpClientFactory = new Mock <IHttpClientFactory>();
            var    mockHandler           = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mockHttpClientFactory.Setup(x => x.CreateClient(DefaultClientNames.Api)).Returns(new HttpClient(mockHandler.Object)
            {
                BaseAddress = new Uri("https://api.europe-west1.gcp.commercetools.com/test-project/")
            });
            mockHandler
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(serialized)
            })
            .Verifiable();
            IClient commerceToolsClient = new CtpClient(
                mockHttpClientFactory.Object,
                this.clientFixture.GetService <IHttpApiCommandFactory>(),
                this.clientFixture.GetService <ISerializerService>(),
                this.clientFixture.GetService <IUserAgentProvider>()
                );
            string messageId = "174adf2f-783f-4ce5-a2d5-ee7d3ee7caf4";
            CategoryCreatedMessage categoryCreatedMessage = commerceToolsClient.ExecuteAsync(new GetByIdCommand <Message>(new Guid(messageId))).Result as CategoryCreatedMessage;

            Assert.Equal(messageId, categoryCreatedMessage.Id);
        }