Exemplo n.º 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>());
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter road name");
            string       roadName     = Console.ReadLine();
            TflRoadUsers tflRoadUsers = new TflRoadUsers(new System.Net.Http.HttpClient());

            tflRoadUsers.RunAsync(roadName).GetAwaiter().GetResult();
        }
Exemplo n.º 3
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>));
        }
Exemplo n.º 4
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);
        }