Exemplo n.º 1
0
        public async Task AppendRawToBlocklistShouldMergeDuplicates()
        {
            var merger = new HostMergerLogic(new Mock <IHttpClient>().Object, new Mock <ILogger>().Object);

            var blocklist = new Blocklist
            {
                Hostnames = new HashSet <string>(new[]
                {
                    "example.com"
                })
            };
            var sources = new HostSource
            {
                Raw = new[]
                {
                    ":: example.com",
                    ":: foobar.com"
                }
            };

            blocklist = await merger.AppendNewAsync(blocklist, sources);

            blocklist.Hostnames.Should().HaveCount(2);
            blocklist.Hostnames.Should().ContainInOrder("example.com", "foobar.com");
        }
Exemplo n.º 2
0
        public async Task AppendLinkToBlocklistShouldMergeDuplicates()
        {
            const string url          = "http://example.com/fakehosts.txt";
            var          clientMock   = new Mock <IHttpClient>();
            var          fakeResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK);

            fakeResponse.Content = new StringContent(string.Join(Environment.NewLine, new[] { "# fake.com", "0.0.0.0 example.com", ":: foobar.com" }));

            clientMock.Setup(x => x.GetAsync(url))
            .Returns(Task.FromResult(fakeResponse));

            var merger = new HostMergerLogic(clientMock.Object, new Mock <ILogger>().Object);

            var blocklist = new Blocklist
            {
                Hostnames = new HashSet <string>(new[]
                {
                    "example.com"
                })
            };
            var sources = new HostSource
            {
                Links = new[]
                {
                    url
                }
            };

            blocklist = await merger.AppendNewAsync(blocklist, sources);

            blocklist.Hostnames.Should().HaveCount(2);
            blocklist.Hostnames.Should().ContainInOrder("example.com", "foobar.com");
        }
Exemplo n.º 3
0
        public static async Task MergeHostsAsync(
            [TimerTrigger(Constants.EveryDay, RunOnStartup = true)] TimerInfo timer,
            ExecutionContext context,
            ILogger log)
        {
            var config = LoadConfig(context, log);

            // ugly setup here but ensures better testability of components..
            var merger                 = new HostMergerLogic(new HttpClient(new System.Net.Http.HttpClient()), log);
            var cloudBlobManager       = new CloudBlobManager(config.AzureWebJobsStorage, config.ContainerName);
            var outputCloudBlobManager =
                string.IsNullOrEmpty(config.OutputContainerName) ||
                config.OutputContainerName == config.ContainerName
                    ? cloudBlobManager
                    : new CloudBlobManager(config.AzureWebJobsStorage, config.OutputContainerName);

            await merger.RunHostMergingAsync(cloudBlobManager, outputCloudBlobManager, config);
        }
Exemplo n.º 4
0
        public async Task AppendHostsToEmptyBlocklist()
        {
            var merger = new HostMergerLogic(new Mock <IHttpClient>().Object, new Mock <ILogger>().Object);

            var blocklist = new Blocklist();
            var sources   = new HostSource
            {
                Hosts = new[]
                {
                    "example.com",
                    "foobar.com"
                }
            };

            blocklist = await merger.AppendNewAsync(blocklist, sources);

            blocklist.Hostnames.Should().HaveCount(2);
            blocklist.Hostnames.Should().ContainInOrder("example.com", "foobar.com");
        }