-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi_test_send_headers.py
43 lines (32 loc) · 1.21 KB
/
api_test_send_headers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from api import app
from base64 import b64encode
import unittest
# Helper API client to send auth headers in tests.
# It only has `get` method and hard-coded user name / password, but can
# easily be extended to support all HTTP methods and configurable auth data,
# so authentication itself can also be tested.
class ApiClient:
"""Performs API requests."""
def __init__(self, app):
self.client = app.test_client()
def get(self, url, **kwargs):
"""Sends GET request and returns the response."""
return self.client.get(url, headers=self.request_headers(), **kwargs)
def request_headers(self):
"""Returns API request headers."""
auth = '{0}:{1}'.format('user', 'secret')
return {
'Accept': 'application/json',
'Authorization': 'Basic {encoded_login}'.format(
encoded_login=b64encode(auth.encode('utf-8')).decode('utf-8')
)
}
class TestIntegrations(unittest.TestCase):
def setUp(self):
self.app = ApiClient(app)
# self.app = app.test_client()
def test_thing(self):
response = self.app.get('/')
print(response.data)
if __name__ == '__main__':
unittest.main()