11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33
4- import httpretty
5- import httpretty .core
64import sys
75sys .path .append ('..' )
86
97import geoip2
10- import json
11- import requests
8+ import requests_mock
129from geoip2 .errors import AddressNotFoundError , AuthenticationError , \
1310 GeoIP2Error , HTTPError , InvalidRequestError , OutOfQueriesError
1411from geoip2 .webservice import Client
2825 unittest .TestCase .assertRegex = unittest .TestCase .assertRegexpMatches
2926
3027
31- @httpretty .activate
3228class TestClient (unittest .TestCase ):
3329
3430 def setUp (self ):
@@ -54,12 +50,13 @@ def _content_type(self, endpoint):
5450 return ('application/vnd.maxmind.com-' +
5551 endpoint + '+json; charset=UTF-8; version=1.0' )
5652
57- def test_country_ok (self ):
58- httpretty .register_uri (httpretty .GET ,
59- self .base_uri + 'country/1.2.3.4' ,
60- body = json .dumps (self .country ),
61- status = 200 ,
62- content_type = self ._content_type ('country' ))
53+ @requests_mock .mock ()
54+ def test_country_ok (self , mock ):
55+ mock .get (self .base_uri + 'country/1.2.3.4' ,
56+ json = self .country ,
57+ status_code = 200 ,
58+ headers = {'Content-Type' :
59+ self ._content_type ('country' )})
6360 country = self .client .country ('1.2.3.4' )
6461 self .assertEqual (type (country ), geoip2 .models .Country ,
6562 'return value of client.country' )
@@ -82,24 +79,25 @@ def test_country_ok(self):
8279 'queries_remaining is 11' )
8380 self .assertEqual (country .raw , self .country , 'raw response is correct' )
8481
85- def test_me (self ):
86- httpretty .register_uri (httpretty .GET ,
87- self .base_uri + 'country/me' ,
88- body = json .dumps (self .country ),
89- status = 200 ,
90- content_type = self ._content_type ('country' ))
82+ @requests_mock .mock ()
83+ def test_me (self , mock ):
84+ mock .get (self .base_uri + 'country/me' ,
85+ json = self .country ,
86+ status_code = 200 ,
87+ headers = {'Content-Type' :
88+ self ._content_type ('country' )})
9189 implicit_me = self .client .country ()
9290 self .assertEqual (type (implicit_me ), geoip2 .models .Country ,
9391 'country() returns Country object' )
9492 explicit_me = self .client .country ()
9593 self .assertEqual (type (explicit_me ), geoip2 .models .Country ,
9694 'country(\' me\' ) returns Country object' )
9795
98- def test_200_error ( self ):
99- httpretty . register_uri ( httpretty . GET ,
100- self .base_uri + 'country/1.1.1.1' ,
101- status = 200 ,
102- content_type = self ._content_type ('country' ))
96+ @ requests_mock . mock ()
97+ def test_200_error ( self , mock ):
98+ mock . get ( self .base_uri + 'country/1.1.1.1' ,
99+ status_code = 200 ,
100+ headers = { 'Content-Type' : self ._content_type ('country' )} )
103101 with self .assertRaisesRegex (GeoIP2Error ,
104102 'could not decode the response as JSON' ):
105103 self .client .country ('1.1.1.1' )
@@ -110,126 +108,138 @@ def test_bad_ip_address(self):
110108 "or IPv6 address" ):
111109 self .client .country ('1.2.3' )
112110
113- def test_no_body_error ( self ):
114- httpretty . register_uri ( httpretty . GET ,
115- self .base_uri + 'country/' + '1.2.3.7' ,
116- body = '' ,
117- status = 400 ,
118- content_type = self ._content_type ('country' ))
111+ @ requests_mock . mock ()
112+ def test_no_body_error ( self , mock ):
113+ mock . get ( self .base_uri + 'country/' + '1.2.3.7' ,
114+ text = '' ,
115+ status_code = 400 ,
116+ headers = { 'Content-Type' : self ._content_type ('country' )} )
119117 with self .assertRaisesRegex (HTTPError ,
120118 'Received a 400 error for .* with no body' ):
121119 self .client .country ('1.2.3.7' )
122120
123- def test_weird_body_error ( self ):
124- httpretty . register_uri ( httpretty . GET ,
125- self .base_uri + 'country/' + '1.2.3.8' ,
126- body = '{"wierd": 42}' ,
127- status = 400 ,
128- content_type = self ._content_type ('country' ))
121+ @ requests_mock . mock ()
122+ def test_weird_body_error ( self , mock ):
123+ mock . get ( self .base_uri + 'country/' + '1.2.3.8' ,
124+ text = '{"wierd": 42}' ,
125+ status_code = 400 ,
126+ headers = { 'Content-Type' : self ._content_type ('country' )} )
129127 with self .assertRaisesRegex (HTTPError ,
130128 'Response contains JSON but it does not '
131129 'specify code or error keys' ):
132130 self .client .country ('1.2.3.8' )
133131
134- def test_bad_body_error ( self ):
135- httpretty . register_uri ( httpretty . GET ,
136- self .base_uri + 'country/' + '1.2.3.9' ,
137- body = 'bad body' ,
138- status = 400 ,
139- content_type = self ._content_type ('country' ))
132+ @ requests_mock . mock ()
133+ def test_bad_body_error ( self , mock ):
134+ mock . get ( self .base_uri + 'country/' + '1.2.3.9' ,
135+ text = 'bad body' ,
136+ status_code = 400 ,
137+ headers = { 'Content-Type' : self ._content_type ('country' )} )
140138 with self .assertRaisesRegex (HTTPError ,
141139 'it did not include the expected JSON body'
142140 ):
143141 self .client .country ('1.2.3.9' )
144142
145- def test_500_error ( self ):
146- httpretty . register_uri ( httpretty . GET ,
147- self .base_uri + 'country/' + '1.2.3.10' ,
148- status = 500 )
143+ @ requests_mock . mock ()
144+ def test_500_error ( self , mock ):
145+ mock . get ( self .base_uri + 'country/' + '1.2.3.10' ,
146+ status_code = 500 )
149147 with self .assertRaisesRegex (HTTPError ,
150148 'Received a server error \(500\) for' ):
151149 self .client .country ('1.2.3.10' )
152150
153- def test_300_error ( self ):
154- httpretty . register_uri ( httpretty . GET ,
155- self .base_uri + 'country/' + '1.2.3.11' ,
156- status = 300 ,
157- content_type = self ._content_type ('country' ))
151+ @ requests_mock . mock ()
152+ def test_300_error ( self , mock ):
153+ mock . get ( self .base_uri + 'country/' + '1.2.3.11' ,
154+ status_code = 300 ,
155+ headers = { 'Content-Type' : self ._content_type ('country' )} )
158156 with self .assertRaisesRegex (HTTPError ,
159157 'Received a very surprising HTTP status '
160158 '\(300\) for' ):
161159
162160 self .client .country ('1.2.3.11' )
163161
164- def test_address_not_found_error (self ):
162+ @requests_mock .mock ()
163+ def test_address_not_found_error (self , mock ):
165164 body = {'error' : 'Not in DB' , 'code' : 'IP_ADDRESS_NOT_FOUND' }
166- httpretty .register_uri (httpretty .GET ,
167- self .base_uri + 'country/' + '1.2.3.13' ,
168- body = json .dumps (body ),
169- status = 404 ,
170- content_type = self ._content_type ('country' ))
165+ mock .get (self .base_uri + 'country/' + '1.2.3.13' ,
166+ json = body ,
167+ status_code = 404 ,
168+ headers = {'Content-Type' : self ._content_type ('country' )})
171169 with self .assertRaisesRegex (AddressNotFoundError ,
172170 'Not in DB' ):
173171 self .client .country ('1.2.3.13' )
174172
175- def test_private_address_error (self ):
173+ @requests_mock .mock ()
174+ def test_private_address_error (self , mock ):
176175 body = {'error' : 'Private' , 'code' : 'IP_ADDRESS_RESERVED' }
177- httpretty .register_uri (httpretty .GET ,
178- self .base_uri + 'country/' + '1.2.3.14' ,
179- body = json .dumps (body ),
180- status = 401 ,
181- content_type = self ._content_type ('country' ))
176+ mock .get (self .base_uri + 'country/' + '1.2.3.14' ,
177+ json = body ,
178+ status_code = 401 ,
179+ headers = {'Content-Type' : self ._content_type ('country' )})
182180 with self .assertRaisesRegex (AddressNotFoundError , 'Private' ):
183181 self .client .country ('1.2.3.14' )
184182
185- def test_auth_invalid (self ):
183+ @requests_mock .mock ()
184+ def test_auth_invalid (self , mock ):
186185 body = {'error' : 'Invalid auth' , 'code' : 'AUTHORIZATION_INVALID' }
187- httpretty .register_uri (httpretty .GET ,
188- self .base_uri + 'country/' + '1.2.3.15' ,
189- body = json .dumps (body ),
190- status = 400 ,
191- content_type = self ._content_type ('country' ))
186+ mock .get (self .base_uri + 'country/' + '1.2.3.15' ,
187+ json = body ,
188+ status_code = 400 ,
189+ headers = {'Content-Type' : self ._content_type ('country' )})
192190 with self .assertRaisesRegex (AuthenticationError , 'Invalid auth' ):
193191 self .client .country ('1.2.3.15' )
194192
195- def test_license_required (self ):
193+ @requests_mock .mock ()
194+ def test_license_required (self , mock ):
196195 body = {'error' : 'License required' , 'code' : 'LICENSE_KEY_REQUIRED' }
197- httpretty .register_uri (httpretty .GET ,
198- self .base_uri + 'country/' + '1.2.3.16' ,
199- body = json .dumps (body ),
200- status = 401 ,
201- content_type = self ._content_type ('country' ))
196+ mock .get (self .base_uri + 'country/' + '1.2.3.16' ,
197+ json = body ,
198+ status_code = 401 ,
199+ headers = {'Content-Type' : self ._content_type ('country' )})
202200 with self .assertRaisesRegex (AuthenticationError , 'License required' ):
203201 self .client .country ('1.2.3.16' )
204202
205- def test_user_id_required (self ):
203+ @requests_mock .mock ()
204+ def test_user_id_required (self , mock ):
206205 body = {'error' : 'User ID required' , 'code' : 'USER_ID_REQUIRED' }
207- httpretty .register_uri (httpretty .GET ,
208- self .base_uri + 'country/' + '1.2.3.17' ,
209- body = json .dumps (body ),
210- status = 401 ,
211- content_type = self ._content_type ('country' ))
206+ mock .get (self .base_uri + 'country/' + '1.2.3.17' ,
207+ json = body ,
208+ status_code = 401 ,
209+ headers = {'Content-Type' : self ._content_type ('country' )})
212210 with self .assertRaisesRegex (AuthenticationError , 'User ID required' ):
213211 self .client .country ('1.2.3.17' )
214212
215- def test_out_of_queries_error (self ):
213+ @requests_mock .mock ()
214+ def test_out_of_queries_error (self , mock ):
216215 body = {'error' : 'Out of Queries' , 'code' : 'OUT_OF_QUERIES' }
217- httpretty .register_uri (httpretty .GET ,
218- self .base_uri + 'country/' + '1.2.3.18' ,
219- body = json .dumps (body ),
220- status = 402 ,
221- content_type = self ._content_type ('country' ))
216+ mock .get (self .base_uri + 'country/' + '1.2.3.18' ,
217+ json = body ,
218+ status_code = 402 ,
219+ headers = {'Content-Type' : self ._content_type ('country' )})
222220 with self .assertRaisesRegex (OutOfQueriesError , 'Out of Queries' ,):
223221 self .client .country ('1.2.3.18' )
224222
225- def test_request (self ):
226- httpretty .register_uri (httpretty .GET ,
227- self .base_uri + 'country/' + '1.2.3.4' ,
228- body = json .dumps (self .country ),
229- status = 200 ,
230- content_type = self ._content_type ('country' ))
231- country = self .client .country ('1.2.3.4' )
232- request = httpretty .core .httpretty .latest_requests [- 1 ]
223+ @requests_mock .mock ()
224+ def test_unknown_error (self , mock ):
225+ msg = 'Unknown error type'
226+ ip = '1.2.3.19'
227+ body = {'error' : msg , 'code' : 'UNKNOWN_TYPE' }
228+ mock .get (self .base_uri + 'country/' + ip ,
229+ json = body ,
230+ status_code = 400 ,
231+ headers = {'Content-Type' : self ._content_type ('country' )})
232+ with self .assertRaisesRegex (InvalidRequestError , msg ,):
233+ self .client .country (ip )
234+
235+ @requests_mock .mock ()
236+ def test_request (self , mock ):
237+ mock .get (self .base_uri + 'country/' + '1.2.3.4' ,
238+ json = self .country ,
239+ status_code = 200 ,
240+ headers = {'Content-Type' : self ._content_type ('country' )})
241+ self .client .country ('1.2.3.4' )
242+ request = mock .request_history [- 1 ]
233243
234244 self .assertEqual (request .path ,
235245 '/geoip/v2.1/country/1.2.3.4' ,
@@ -243,32 +253,32 @@ def test_request(self):
243253 self .assertEqual (request .headers ['Authorization' ],
244254 'Basic NDI6YWJjZGVmMTIzNDU2' , 'correct auth' )
245255
246- def test_city_ok ( self ):
247- httpretty . register_uri ( httpretty . GET ,
248- self .base_uri + 'city/' + '1.2.3.4' ,
249- body = json . dumps ( self .country ) ,
250- status = 200 ,
251- content_type = self ._content_type ('city' ))
256+ @ requests_mock . mock ()
257+ def test_city_ok ( self , mock ):
258+ mock . get ( self .base_uri + 'city/' + '1.2.3.4' ,
259+ json = self .country ,
260+ status_code = 200 ,
261+ headers = { 'Content-Type' : self ._content_type ('city' )} )
252262 city = self .client .city ('1.2.3.4' )
253263 self .assertEqual (type (city ), geoip2 .models .City ,
254264 'return value of client.city' )
255265
256- def test_insights_ok ( self ):
257- httpretty . register_uri ( httpretty . GET ,
258- self .base_uri + 'insights/1.2.3.4' ,
259- body = json . dumps ( self .country ) ,
260- status = 200 ,
261- content_type = self ._content_type ('country' ))
266+ @ requests_mock . mock ()
267+ def test_insights_ok ( self , mock ):
268+ mock . get ( self .base_uri + 'insights/1.2.3.4' ,
269+ json = self .country ,
270+ status_code = 200 ,
271+ headers = { 'Content-Type' : self ._content_type ('country' )} )
262272 insights = self .client .insights ('1.2.3.4' )
263273 self .assertEqual (type (insights ), geoip2 .models .Insights ,
264274 'return value of client.insights' )
265275
266- def test_insights_ok ( self ):
267- httpretty . register_uri ( httpretty . GET ,
268- self .base_uri + 'insights/1.2.3.4' ,
269- body = json . dumps ( self .country ) ,
270- status = 200 ,
271- content_type = self ._content_type ('country' ))
276+ @ requests_mock . mock ()
277+ def test_insights_ok ( self , mock ):
278+ mock . get ( self .base_uri + 'insights/1.2.3.4' ,
279+ json = self .country ,
280+ status_code = 200 ,
281+ headers = { 'Content-Type' : self ._content_type ('country' )} )
272282 insights = self .client .insights ('1.2.3.4' )
273283 self .assertEqual (type (insights ), geoip2 .models .Insights ,
274284 'return value of client.insights' )
0 commit comments