Skip to content

Commit 6f85a54

Browse files
Fleshing out the mock object
1 parent cbb5c05 commit 6f85a54

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

test/test_apikeys.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
from StringIO import StringIO
1010
except ImportError: # Python 3
1111
from io import StringIO
12-
import logging
12+
try:
13+
import urllib.request as urllib_request
14+
from urllib.parse import urlencode
15+
from urllib.error import HTTPError
16+
except ImportError: # Python 2
17+
import urllib2 as urllib_request
18+
from urllib2 import HTTPError
19+
from urllib import urlencode
1320

1421
import sendgrid
1522
from sendgrid import SendGridClient, Mail
@@ -21,9 +28,22 @@
2128
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
2229

2330
class MockSendGridAPIClientRequest(SendGridAPIClient):
31+
def __init__(self, apikey, **opts):
32+
super(MockSendGridAPIClientRequest, self).__init__(apikey, **opts)
33+
self._req = None
34+
2435
def _build_request(self, url=None, json_header=False, method='GET', data=None):
36+
req = urllib_request.Request(url)
37+
req.get_method = lambda: method
38+
req.add_header('User-Agent', self.useragent)
39+
req.add_header('Authorization', 'Bearer ' + self.apikey)
40+
if json_header:
41+
req.add_header('Content-Type', 'application/json')
42+
print "url= " + req._Request__original
43+
print "headers= " + str(req.headers)
44+
print "data= " + json.dumps(data)
2545
response = 200
26-
body = {"message": "success"}
46+
body = {"mock": "success"}
2747
return response, body
2848

2949
class TestSendGridAPIClient(unittest.TestCase):

0 commit comments

Comments
 (0)