class TestAtlasRequest(TestCase):
    def setUp(self):
        self.request = AtlasRequest(**{
            "key": "blaaaa",
            "server": "test",
            "url_path": "testing"
        })

    def test_headers(self):
        """Tests header fields of the request."""
        expected_output = {
            "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
            "Content-Type": "application/json",
            "Accept": "application/json"
        }
        self.assertEqual(expected_output, self.request.get_headers())

    def test_http_method_args(self):
        """Tests initial args that will be passed later to HTTP method."""
        expected_output = {
            "params": {"key": "blaaaa"},
            "verify": True,
            "headers": {
                "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "proxies": {},
        }
        self.assertEqual(expected_output, self.request.http_method_args)

    def test_get_method(self):
        """Tests GET reuest method"""
        extra_params = {"bull": "shit", "cow": "shit", "horse": "shit"}
        expected_args = {
            "params": {
                "key": "blaaaa", "bull": "shit",
                "cow": "shit", "horse": "shit"
            },
            "verify": True,
            "headers": {
                "User-Agent": "RIPE ATLAS Cousteau v{0}".format(__version__),
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "proxies": {},
        }
        with mock.patch("ripe.atlas.cousteau.request.AtlasRequest.http_method") as mock_get:
            mock_get.return_value = True
            self.request.get(**extra_params)
            self.assertEqual(self.request.http_method_args, expected_args)

    def test_url_build(self):
        """Tests build of the url of the request."""
        self.request.build_url()
        self.assertEqual(self.request.url, "https://testtesting")

    def test_success_http_method(self):
        """Tests the main http method function of the request in case of success"""
        with mock.patch("ripe.atlas.cousteau.AtlasRequest.get_http_method") as mock_get:
            fake = FakeResponse(json_return={"blaaa": "b"})
            mock_get.return_value = fake
            self.assertEqual(
                self.request.http_method("GET"),
                (True, {"blaaa": "b"})
            )

            fake_error = FakeErrorResponse()
            mock_get.return_value = fake_error
            self.assertEqual(
                self.request.http_method("GET"),
                (True, "testing")
            )

    def test_not_success_http_method(self):
        """Tests the main http method function of the request in case of fail"""
        with mock.patch("ripe.atlas.cousteau.AtlasRequest.get_http_method") as mock_get:
            fake = FakeResponse(json_return={"blaaa": "b"}, ok=False)
            mock_get.return_value = fake
            self.assertEqual(
                self.request.http_method("GET"),
                (False, {"blaaa": "b"})
            )

            fake_error = FakeErrorResponse(ok=False)
            mock_get.return_value = fake_error
            self.assertEqual(
                self.request.http_method("GET"),
                (False, "testing")
            )

    def test_exception_http_method(self):
        """Tests the main http method function of the request in case of fail"""
        with mock.patch("ripe.atlas.cousteau.AtlasRequest.get_http_method") as mock_get:
            mock_get.side_effect = requests.exceptions.RequestException("excargs")
            self.assertEqual(
                self.request.http_method("GET"),
                (False, ("excargs",))
            )

    def test_user_agent(self):
        with mock.patch("ripe.atlas.cousteau.request.__version__", 999):
            standard = "RIPE ATLAS Cousteau v999"
            self.assertEqual(AtlasRequest().http_agent, standard)
            self.assertEqual(AtlasRequest(user_agent=None).http_agent, standard)
            self.assertEqual(AtlasRequest(user_agent="w00t").http_agent, "w00t")