forked from Vonage/vonage-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
72 lines (44 loc) · 1.65 KB
/
Copy pathutil.py
File metadata and controls
72 lines (44 loc) · 1.65 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os.path
import re
import pytest
try:
from urllib.parse import urlparse, parse_qs
except ImportError:
from urlparse import urlparse, parse_qs
import responses
def request_body():
return responses.calls[0].request.body
def request_query():
return urlparse(responses.calls[0].request.url).query
def request_params():
""" Obtain the query params, as a dict. """
return parse_qs(request_query())
def request_headers():
return responses.calls[0].request.headers
def request_user_agent():
return responses.calls[0].request.headers["User-Agent"]
def request_authorization():
return responses.calls[0].request.headers["Authorization"].decode("utf-8")
def request_content_type():
return responses.calls[0].request.headers["Content-Type"]
def stub(method, url, fixture_path=None, status_code=200):
body = load_fixture(fixture_path) if fixture_path else '{"key":"value"}'
responses.add(
method, url, body=body, status=status_code, content_type="application/json"
)
def stub_bytes(method, url):
responses.add(method, url, body=b"THISISANMP3", status=200)
def assert_re(pattern, string):
__tracebackhide__ = True
if not re.search(pattern, string):
pytest.fail("Cannot find pattern {!r} in {!r}".format(pattern, string))
def assert_basic_auth():
params = request_params()
assert "api_key" not in params
assert "api_secret" not in params
assert (
request_headers()["Authorization"]
== "Basic bmV4bW8tYXBpLWtleTpuZXhtby1hcGktc2VjcmV0"
)
def load_fixture(fixture_path):
return open(os.path.join(os.path.dirname(__file__), "data", fixture_path)).read()