Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
58 lines (44 loc) · 1.64 KB
/
Copy pathutil.py
File metadata and controls
58 lines (44 loc) · 1.64 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
from __future__ import absolute_import
import json
import collections
from json import JSONEncoder
import duo_client
import six
class MockObjectJsonEncoder(json.JSONEncoder):
def default(self, obj):
return getattr(obj.__class__, "to_json")(obj)
# put params in a dict to avoid inconsistent ordering
def params_to_dict(param_str):
param_dict = collections.defaultdict(list)
for (key, val) in (param.split('=') for param in param_str.split('&')):
param_dict[key].append(val)
return param_dict
class MockHTTPConnection(object):
"""
Mock HTTP(S) connection that returns a dummy JSON response.
"""
status = 200 # success!
def __init__(self, data_response_should_be_list=False):
# if a response object should be a list rather than
# a dict, then set this flag to true
self.data_response_should_be_list = data_response_should_be_list
def dummy(self):
return self
_connect = _disconnect = close = getresponse = dummy
def read(self):
response = self.__dict__
if self.data_response_should_be_list:
response = [self.__dict__]
return json.dumps({"stat":"OK", "response":response},
cls=MockObjectJsonEncoder)
def request(self, method, uri, body, headers):
self.method = method
self.uri = uri
self.body = body
self.headers = {}
for k, v in headers.items():
if isinstance(k, six.binary_type):
k = k.decode('ascii')
if isinstance(v, six.binary_type):
v = v.decode('ascii')
self.headers[k] = v