public async void GetXml()
        {
            var responses = new[]
            {
                new MockResponse(SuccessResponse, HttpStatusCode.OK),
                new MockResponse(File.ReadAllText(TestFiles.Tocs), HttpStatusCode.OK),
            };

            var handler = new MockHttpMessageHandler(responses);
            var source  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());
            await source.Initiate(TestUser, TestPassword, CancellationToken.None);

            using (var reader =
                       await source.GetKnowledgebaseXml(KnowedgebaseSubjects.Tocs, CancellationToken.None))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        Assert.Equal("TrainOperatingCompanyList", reader.Name);
                        break;
                    }
                }
            }
        }
        public async void GetXmlThrowsExceptionIfNotInitiated()
        {
            var responses = new[]
            {
                new MockResponse(SuccessResponse, HttpStatusCode.OK),
                new MockResponse(File.ReadAllText(TestFiles.Tocs), HttpStatusCode.OK),
            };

            var handler = new MockHttpMessageHandler(responses);
            var source  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());

            var ex = await Assert.ThrowsAsync <KnowledgebaseException>(() =>
                                                                       source.GetKnowledgebaseXml(KnowedgebaseSubjects.Tocs, CancellationToken.None));

            Assert.Equal("Source not initialised.", ex.Message);
        }
        public async void GetXmlThrowsExceptionIfCallToNreFails()
        {
            var responses = new[]
            {
                new MockResponse(SuccessResponse, HttpStatusCode.OK),
                new MockResponse("Server error", HttpStatusCode.InternalServerError),
            };

            var handler = new MockHttpMessageHandler(responses);
            var source  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());
            await source.Initiate(TestUser, TestPassword, CancellationToken.None);

            var ex = await Assert.ThrowsAsync <KnowledgebaseException>(() =>
                                                                       source.GetKnowledgebaseXml(KnowedgebaseSubjects.Tocs, CancellationToken.None));

            Assert.Equal("Http Error InternalServerError getting Tocs", ex.Message);
        }
Пример #4
0
        public async void GetXml(KnowedgebaseSubjects subject, string expectedRoot)
        {
            using (var client = new NationalRailEnquiriesSource(new HttpClient(), Substitute.For <ILogger>()))
            {
                await client.Initiate(user, password, CancellationToken.None);

                using (var reader =
                           await client.GetKnowledgebaseXml(subject, CancellationToken.None))
                {
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            Assert.Equal(expectedRoot, reader.Name);
                            break;
                        }
                    }
                }
            }
        }