예제 #1
0
 def test_500_error(self):
     httpretty.register_uri(httpretty.GET,
                            self.base_uri + "country/" + "1.2.3.10",
                            status=500)
     with self.assertRaisesRegex(HTTPError,
                                 r"Received a server error \(500\) for"):
         self.run_client(self.client.country("1.2.3.10"))
예제 #2
0
    def test_request(self):
        httpretty.register_uri(
            httpretty.GET,
            self.base_uri + "country/" + "1.2.3.4",
            body=json.dumps(self.country),
            status=200,
            content_type=self._content_type("country"),
        )
        self.run_client(self.client.country("1.2.3.4"))
        request = httpretty.last_request

        self.assertEqual(request.path, "/geoip/v2.1/country/1.2.3.4",
                         "correct URI is used")
        self.assertEqual(request.headers["Accept"], "application/json",
                         "correct Accept header")
        self.assertRegex(
            request.headers["User-Agent"],
            "^GeoIP2-Python-Client/",
            "Correct User-Agent",
        )
        self.assertEqual(
            request.headers["Authorization"],
            "Basic NDI6YWJjZGVmMTIzNDU2",
            "correct auth",
        )
예제 #3
0
def test_httpretty_should_mock_a_simple_get_with_requests_read():
    """HTTPretty should mock a simple GET with requests.get"""
    httpretty.register_uri(httpretty.GET,
                           "http://yipit.com/",
                           body="Find the best daily deals")

    response = requests.get("http://yipit.com")
    expect(response.text).to.equal("Find the best daily deals")
    expect(httpretty.last_request.method).to.equal("GET")
    expect(httpretty.last_request.path).to.equal("/")
예제 #4
0
 def test_bad_body_error(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/" + "1.2.3.9",
         body="bad body",
         status=400,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(
             HTTPError, "it did not include the expected JSON body"):
         self.run_client(self.client.country("1.2.3.9"))
예제 #5
0
 def test_no_body_error(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/" + "1.2.3.7",
         body="",
         status=400,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(
             HTTPError, "Received a 400 error for .* with no body"):
         self.run_client(self.client.country("1.2.3.7"))
예제 #6
0
 def test_200_error(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/1.1.1.1",
         body="",
         status=200,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(GeoIP2Error,
                                 "could not decode the response as JSON"):
         self.run_client(self.client.country("1.1.1.1"))
예제 #7
0
 def _test_error(self, status, error_code, error_class):
     msg = "Some error message"
     body = {"error": msg, "code": error_code}
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/1.2.3.18",
         body=json.dumps(body),
         status=status,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(error_class, msg):
         self.run_client(self.client.country("1.2.3.18"))
예제 #8
0
    def test_300_error(self):
        httpretty.register_uri(
            httpretty.GET,
            self.base_uri + "country/" + "1.2.3.11",
            status=300,
            content_type=self._content_type("country"),
        )
        with self.assertRaisesRegex(
                HTTPError,
                r"Received a very surprising HTTP status \(300\) for"):

            self.run_client(self.client.country("1.2.3.11"))
예제 #9
0
 def test_city_ok(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "city/" + "1.2.3.4",
         body=json.dumps(self.country),
         status=200,
         content_type=self._content_type("city"),
     )
     city = self.run_client(self.client.city("1.2.3.4"))
     self.assertEqual(type(city), geoip2.models.City,
                      "return value of client.city")
     self.assertEqual(city.traits.network,
                      ipaddress.ip_network("1.2.3.0/24"), "network")
예제 #10
0
 def test_unknown_error(self):
     msg = "Unknown error type"
     ip = "1.2.3.19"
     body = {"error": msg, "code": "UNKNOWN_TYPE"}
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/" + ip,
         body=json.dumps(body),
         status=400,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(InvalidRequestError, msg):
         self.run_client(self.client.country(ip))
예제 #11
0
 def test_weird_body_error(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/" + "1.2.3.8",
         body='{"wierd": 42}',
         status=400,
         content_type=self._content_type("country"),
     )
     with self.assertRaisesRegex(
             HTTPError,
             "Response contains JSON but it does not "
             "specify code or error keys",
     ):
         self.run_client(self.client.country("1.2.3.8"))
예제 #12
0
 def test_insights_ok(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "insights/1.2.3.4",
         body=json.dumps(self.insights),
         status=200,
         content_type=self._content_type("country"),
     )
     insights = self.run_client(self.client.insights("1.2.3.4"))
     self.assertEqual(type(insights), geoip2.models.Insights,
                      "return value of client.insights")
     self.assertEqual(insights.traits.network,
                      ipaddress.ip_network("1.2.3.0/24"), "network")
     self.assertEqual(insights.traits.static_ip_score, 1.3,
                      "static_ip_score is 1.3")
     self.assertEqual(insights.traits.user_count, 2, "user_count is 2")
예제 #13
0
 def test_me(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/me",
         body=json.dumps(self.country),
         status=200,
         content_type=self._content_type("country"),
     )
     implicit_me = self.run_client(self.client.country())
     self.assertEqual(type(implicit_me), geoip2.models.Country,
                      "country() returns Country object")
     explicit_me = self.run_client(self.client.country())
     self.assertEqual(
         type(explicit_me),
         geoip2.models.Country,
         "country('me') returns Country object",
     )
예제 #14
0
 def test_country_ok(self):
     httpretty.register_uri(
         httpretty.GET,
         self.base_uri + "country/1.2.3.4",
         body=json.dumps(self.country),
         status=200,
         content_type=self._content_type("country"),
     )
     country = self.run_client(self.client.country("1.2.3.4"))
     self.assertEqual(type(country), geoip2.models.Country,
                      "return value of client.country")
     self.assertEqual(country.continent.geoname_id, 42,
                      "continent geoname_id is 42")
     self.assertEqual(country.continent.code, "NA", "continent code is NA")
     self.assertEqual(country.continent.name, "North America",
                      "continent name is North America")
     self.assertEqual(country.country.geoname_id, 1,
                      "country geoname_id is 1")
     self.assertIs(
         country.country.is_in_european_union,
         False,
         "country is_in_european_union is False",
     )
     self.assertEqual(country.country.iso_code, "US",
                      "country iso_code is US")
     self.assertEqual(country.country.names,
                      {"en": "United States of America"}, "country names")
     self.assertEqual(
         country.country.name,
         "United States of America",
         "country name is United States of America",
     )
     self.assertEqual(country.maxmind.queries_remaining, 11,
                      "queries_remaining is 11")
     self.assertIs(
         country.registered_country.is_in_european_union,
         True,
         "registered_country is_in_european_union is True",
     )
     self.assertEqual(country.traits.network,
                      ipaddress.ip_network("1.2.3.0/24"), "network")
     self.assertEqual(country.raw, self.country, "raw response is correct")