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 TokenCorrectlySet()
        {
            var handler = new MockHttpMessageHandler(SuccessResponse, HttpStatusCode.OK);
            var client  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());
            await client.Initiate(TestUser, TestPassword, CancellationToken.None);

            Assert.Equal("testuser:1601637685000:xAN06yHWAL2P9Tzud2S3piJN2701IDqz1gUpbmMM3CQ=", client.Token);
        }
        public async void UserPasswordInInitiateRequest()
        {
            var handler = new MockHttpMessageHandler(SuccessResponse, HttpStatusCode.OK);
            var client  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());
            await client.Initiate(TestUser, TestPassword, CancellationToken.None);

            Assert.True(client.IsInititated);
        }
        public async void NreErrorWhenInitialising()
        {
            var handler = new MockHttpMessageHandler("Server error", HttpStatusCode.InternalServerError);
            var client  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());

            var ex = await Assert.ThrowsAsync <KnowledgebaseException>(() => client.Initiate(TestUser, TestPassword, CancellationToken.None));

            Assert.Equal("Authentication error. Response: Server error", ex.Message);;
        }
        public async void BadUserOrPassword()
        {
            var handler = new MockHttpMessageHandler(InvalidResponse, HttpStatusCode.Unauthorized);
            var client  = new NationalRailEnquiriesSource(new HttpClient(handler), Substitute.For <ILogger>());

            var ex = await Assert.ThrowsAsync <KnowledgebaseException>(() => client.Initiate(TestUser, TestPassword, CancellationToken.None));

            Assert.Contains("Invalid username/password", ex.Message);;
        }
Exemplo n.º 6
0
        public async void GetTocs()
        {
            var logger = Substitute.For <ILogger>();

            using (var source = new NationalRailEnquiriesSource(new HttpClient(), logger))
            {
                await source.Initiate(user, password, CancellationToken.None);

                var knowledgebase = new Knowledgebase(source, logger);
                var tocs          = await knowledgebase.GetTocs(CancellationToken.None);

                Assert.NotEmpty(tocs.TrainOperatingCompany);
            }
        }
        public async void DisposeRemovesToken()
        {
            NationalRailEnquiriesSource client;
            var handler    = new MockHttpMessageHandler(SuccessResponse, HttpStatusCode.OK);
            var httpClient = new HttpClient(handler);

            using (client = new NationalRailEnquiriesSource(httpClient, Substitute.For <ILogger>()))
            {
                await client.Initiate(TestUser, TestPassword, CancellationToken.None);

                Assert.NotNull(client.Token);
            }
            Assert.Null(client.Token);
            Assert.DoesNotContain(httpClient.DefaultRequestHeaders, h => h.Key == "X-Auth-Token");
        }
Exemplo n.º 8
0
        static async Task Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                .AddCommandLine(args)
                                .Build();

            Log.Logger = new LoggerConfiguration()
                         .ReadFrom.Configuration(configuration)
                         .Enrich.FromLogContext()
                         .CreateLogger();

            var user     = configuration.GetValue <string>("user");
            var password = configuration.GetValue <string>("password");

            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password))
            {
                Log.Fatal("User or password not provided in commandline");
                Log.Information("Usage:  NreKnowledgebaseDownloader user=<user> password=<password>");
                return;
            }

            using (var source = new NationalRailEnquiriesSource(new HttpClient(), Log.Logger))
            {
                await source.Initiate(user, password, CancellationToken.None);

                Log.Information("Initialised");

                var outputFolder = CreateOutputFolder();
                Log.Information("Created directory");
                var tasks = new List <Task>();
                var token = CancellationToken.None;

                foreach (var subject in (KnowedgebaseSubjects[])Enum.GetValues(typeof(KnowedgebaseSubjects)))
                {
                    var outputTask = CreateOutputTask(subject, source, outputFolder, token);
                    tasks.Add(outputTask);
                }
                Log.Information("Waiting");
                if (!Task.WaitAll(tasks.ToArray(), 30000))
                {
                    Log.Error("Did not get all of the knowledgebase before timeout.");
                }

                Log.Information("Done");
            }
        }
        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);
        }
Exemplo n.º 10
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;
                        }
                    }
                }
            }
        }