示例#1
0
        public void GetData_CheckSendAsyncIsCalled()
        {
            var    mockHandler = new Mock <HttpMessageHandler>();
            string content     = "[{\"displayName\": \"A2\",  \"statusSeverity\": \"Good\",\"statusSeverityDescription\": \"No Exceptional Delays\" }]";

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json")
            });
            HttpClient client = new HttpClient(mockHandler.Object);

            uri = new Uri("https://api.tfl.gov.uk/Road");
            client.BaseAddress = uri;

            roadUsers = new TflRoadUsers.TflRoadUsers(client);
            var result = roadUsers.GetRoadStatusAsync(uri.ToFullUrl("A2")).GetAwaiter().GetResult();

            mockHandler.Protected().Verify(
                "SendAsync",
                Times.Exactly(1),
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Get

                                               ),
                ItExpr.IsAny <CancellationToken>());
        }
示例#2
0
        public void TestGetRoadStatus_ReturnsListofRoadType()
        {
            var mockHandler = new Mock <HttpMessageHandler>();

            string content = "[{\"displayName\": \"A2\",  \"statusSeverity\": \"Good\",\"statusSeverityDescription\": \"No Exceptional Delays\" }]";

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json")
            });
            HttpClient client = new HttpClient(mockHandler.Object);

            uri = new Uri("https://api.tfl.gov.uk/Road");
            client.BaseAddress = uri;

            roadUsers = new TflRoadUsers.TflRoadUsers(client);
            var result = roadUsers.GetRoadStatusAsync(uri.ToFullUrl("A2")).GetAwaiter().GetResult();

            Assert.IsTrue(result.GetType() == typeof(List <Road>));
        }
示例#3
0
        public void GetData_CheckInvalidResponse_EmptyContent()
        {
            var mockHandler = new Mock <HttpMessageHandler>();

            string content = "[{\"exceptionType\": \"EntityNotFoundException\",\"httpStatusCode\": 404,\"httpStatus\": \"NotFound\" }]";

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json")
            });
            HttpClient client = new HttpClient(mockHandler.Object);

            uri = new Uri("https://api.tfl.gov.uk/Road");
            client.BaseAddress = uri;

            roadUsers = new TflRoadUsers.TflRoadUsers(client);
            var result = roadUsers.GetRoadStatusAsync(uri.ToFullUrl("A2")).GetAwaiter().GetResult();

            Assert.IsTrue(result.GetType() == typeof(List <Road>));
            Assert.IsTrue(result.Count == 0);
        }