<commit_before># -*- coding: utf-8 -*-
"""
Testing HTTP collector
"""
import datetime
import os
import unittest

import requests_mock

import intelmq.lib.test as test
import intelmq.lib.utils as utils
from intelmq.bots.collectors.http.collector_http import HTTPCollectorBot

OUTPUT = [
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/two_files.tar.gz",
        "raw": utils.base64_encode('bar text\n'),
        "extra.file_name": "bar",
    },
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/two_files.tar.gz",
        "raw": utils.base64_encode('foo text\n'),
        "extra.file_name": "foo",
    },
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/foobar.txt",
        "raw": utils.base64_encode("bar text\n"),
    }
]


ASSET_PATH = os.path.join(os.path.dirname(__file__), '../../../assets/')


def prepare_mocker(mocker):
    for filename in os.listdir(ASSET_PATH):
        mocker.get('http://localhost/%s' % filename,
                   body=open(os.path.join(ASSET_PATH, filename), 'rb'))


@requests_mock.Mocker()
class TestHTTPCollectorBot(test.BotTestCase, unittest.TestCase):
    """
    A TestCase for HTTPCollectorBot.
    """

    @classmethod
    def set_bot(cls):
        cls.bot_reference = HTTPCollectorBot
        cls.sysconfig = {'http_url': 'http://localhost/two_files.tar.gz',
                         'extract_files': True,
                         'name': 'Example feed',
                         }

    def test_targz_twofiles(self, mocker):
        """ Test tar.gz archive with two files inside. """
        self.input_message = None
        prepare_mocker(mocker)
        self.run_bot(iterations=1)

        self.assertMessageEqual(0, OUTPUT[0])
        self.assertMessageEqual(1, OUTPUT[1])<commit_msg>TST: use a context manager for the mocked files<commit_after># -*- coding: utf-8 -*-
"""
Testing HTTP collector
"""
import datetime
import os
import unittest

import requests_mock

import intelmq.lib.test as test
import intelmq.lib.utils as utils
from intelmq.bots.collectors.http.collector_http import HTTPCollectorBot

OUTPUT = [
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/two_files.tar.gz",
        "raw": utils.base64_encode('bar text\n'),
        "extra.file_name": "bar",
    },
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/two_files.tar.gz",
        "raw": utils.base64_encode('foo text\n'),
        "extra.file_name": "foo",
    },
    {
        "__type": "Report",
        "feed.name": "Example feed",
        "feed.accuracy": 100.,
        "feed.url": "http://localhost/foobar.txt",
        "raw": utils.base64_encode("bar text\n"),
    }
]


ASSET_PATH = os.path.join(os.path.dirname(__file__), '../../../assets/')


def prepare_mocker(mocker):
    for filename in os.listdir(ASSET_PATH):
        with open(os.path.join(ASSET_PATH, filename), 'rb') as f:
            mocker.get('http://localhost/%s' % filename, content=f.read())



@requests_mock.Mocker()
class TestHTTPCollectorBot(test.BotTestCase, unittest.TestCase):
    """
    A TestCase for HTTPCollectorBot.
    """

    @classmethod
    def set_bot(cls):
        cls.bot_reference = HTTPCollectorBot
        cls.sysconfig = {'http_url': 'http://localhost/two_files.tar.gz',
                         'extract_files': True,
                         'name': 'Example feed',
                         }

    def test_targz_twofiles(self, mocker):
        """ Test tar.gz archive with two files inside. """
        self.input_message = None
        prepare_mocker(mocker)
        self.run_bot(iterations=1)

        self.assertMessageEqual(0, OUTPUT[0])
        self.assertMessageEqual(1, OUTPUT[1])
{'fraction_non_alphanumeric': 0.15341049002708693, 'fraction_numerical': 0.010342280226545186, 'mean_word_length': 2.4020100502512562, 'pattern_counts': {'":': 34, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '671575', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1291, 'n_words': 251}