Beispiel #1
0
        public async Task ReadPageAsync()
        {
            var url  = "http://abc.com/api";
            var page = PageMaker.GetExpectedPage(
                data: UomTestData.GetUomsArray(),
                searchText: string.Empty,
                skip: 0,
                take: 20
                );
            var expectedResponse           = CollectionPageHttpResponseMaker.GetExpectedResponse(page, url);
            var expectedData               = CollectionPageDataMaker.GetExpectedPage(page, url);
            HttpRequestMessage sentMessage = null;
            var httpService  = new Mock <IHttpReadService>(MockBehavior.Strict);
            var deserializer = new Mock <IContentDeserializer>(MockBehavior.Strict);
            var sequence     = new MockSequence();

            httpService
            .InSequence(sequence)
            .Setup(h => h.SendAsync(It.IsAny <HttpRequestMessage>()))
            .ReturnsAsync(expectedResponse)
            .Callback <HttpRequestMessage>((request) => sentMessage = request);
            deserializer
            .InSequence(sequence)
            .Setup(d => d.DeserializeAsync <ImmutableList <Uom> >(expectedResponse.Content as StringContent))
            .ReturnsAsync(expectedData.Items as ImmutableList <Uom>);

            var uut        = new ReadPageServiceBuildRequest <Uom>(httpService.Object, deserializer.Object);
            var actualData = await uut.ReadPageAsync(url);

            Assert.NotNull(sentMessage);
            Assert.AreEqual(url, sentMessage.RequestUri.ToString());
            Assert.AreEqual(sentMessage.Method, HttpMethod.Get);
            Assert.True(CollectionPageDataEqual.Check(expectedData, actualData, UomEqual.Check));
        }
Beispiel #2
0
        public void ReadPageAsync_BadUrl()
        {
            var httpService  = new Mock <IHttpReadService>(MockBehavior.Strict).Object;
            var deserializer = new Mock <IContentDeserializer>(MockBehavior.Strict).Object;
            var uut          = new ReadPageServiceBuildRequest <Uom>(httpService, deserializer);

            Assert.ThrowsAsync <ArgumentException>(
                async() => await uut.ReadPageAsync("abc")
                );
        }
Beispiel #3
0
        public void ReadPageAsync_HttpStatus500()
        {
            var url = "http://qtb3.com/a/b";
            HttpRequestMessage sentMessage = null;
            var httpResponseMessage        = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            var httpService  = new Mock <IHttpReadService>(MockBehavior.Strict);
            var deserializer = new Mock <IContentDeserializer>(MockBehavior.Strict);

            httpService
            .Setup(h => h.SendAsync(It.IsAny <HttpRequestMessage>()))
            .ReturnsAsync(httpResponseMessage)
            .Callback <HttpRequestMessage>((request) => sentMessage = request);

            var uut       = new ReadPageServiceBuildRequest <Uom>(httpService.Object, deserializer.Object);
            var exception = Assert.ThrowsAsync <FailedRequestException>(
                async() => await uut.ReadPageAsync(url)
                );

            Assert.AreEqual(HttpStatusCode.InternalServerError, exception.StatusCode);
            Assert.NotNull(sentMessage);
            Assert.AreEqual(url, sentMessage.RequestUri.ToString());
            Assert.AreEqual(sentMessage.Method, HttpMethod.Get);
        }