-
Notifications
You must be signed in to change notification settings - Fork 41
/
mockhttp.py
189 lines (154 loc) · 6.63 KB
/
mockhttp.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from __future__ import unicode_literals
import re
import time
from io import BytesIO
from .compat import (BaseHTTPRequestHandler, decode_from_bytes,
encode_to_bytes, parse_qs, unquote_utf8, urlsplit)
from .mocket import Mocket, MocketEntry
try:
import magic
except ImportError:
magic = None
STATUS = dict([(k, v[0]) for k, v in BaseHTTPRequestHandler.responses.items()])
CRLF = '\r\n'
class Request(BaseHTTPRequestHandler):
def __init__(self, data):
_, self.body = decode_from_bytes(data).split('\r\n\r\n', 1)
self.rfile = BytesIO(encode_to_bytes(data))
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
self.method = self.command
self.querystring = parse_qs(unquote_utf8(urlsplit(self.path).query), keep_blank_values=True)
def add_data(self, data):
self.body += data
def __str__(self):
return "{} - {} - {}".format(self.method, self.path, self.headers)
class Response(object):
headers = None
is_file_object = False
def __init__(self, body='', status=200, headers=None, lib_magic=magic):
# needed for testing libmagic import failure
self.magic = lib_magic
headers = headers or {}
try:
# File Objects
self.body = body.read()
self.is_file_object = True
except AttributeError:
self.body = encode_to_bytes(body)
self.status = status
self.set_base_headers()
if headers is not None:
self.set_extra_headers(headers)
self.data = self.get_protocol_data() + self.body
def get_protocol_data(self):
status_line = 'HTTP/1.1 {status_code} {status}'.format(status_code=self.status, status=STATUS[self.status])
header_lines = CRLF.join(['{0}: {1}'.format(k.capitalize(), v) for k, v in self.headers.items()])
return '{0}\r\n{1}\r\n\r\n'.format(status_line, header_lines).encode('utf-8')
def set_base_headers(self):
self.headers = {
'Status': str(self.status),
'Date': time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime()),
'Server': 'Python/Mocket',
'Connection': 'close',
'Content-Length': str(len(self.body)),
}
if not self.is_file_object:
self.headers['Content-Type'] = 'text/plain; charset=utf-8'
elif self.magic:
self.headers['Content-Type'] = decode_from_bytes(magic.from_buffer(self.body, mime=True))
def set_extra_headers(self, headers):
for k, v in headers.items():
self.headers['-'.join([token.capitalize() for token in k.split('-')])] = v
class Entry(MocketEntry):
CONNECT = 'CONNECT'
DELETE = 'DELETE'
GET = 'GET'
HEAD = 'HEAD'
OPTIONS = 'OPTIONS'
PATCH = 'PATCH'
POST = 'POST'
PUT = 'PUT'
TRACE = 'TRACE'
METHODS = (CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE)
request_cls = Request
response_cls = Response
def __init__(self, uri, method, responses, match_querystring=True):
uri = urlsplit(uri)
if not uri.port:
if uri.scheme == 'https':
port = 443
else:
port = 80
super(Entry, self).__init__((uri.hostname, uri.port or port), responses)
self.schema = uri.scheme
self.path = uri.path
self.query = uri.query
self.method = method.upper()
self._sent_data = b''
self._match_querystring = match_querystring
def collect(self, data):
self._sent_data += data
decoded_data = decode_from_bytes(data)
if not decoded_data.startswith(Entry.METHODS) and decoded_data.endswith(CRLF):
Mocket.remove_last_request()
super(Entry, self).collect(self._sent_data)
def can_handle(self, data):
r"""
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
>>> e.can_handle(b'GET /?bar=foo HTTP/1.1\r\nHost: github.com\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUser-Agent: python-requests/2.7.0 CPython/3.4.3 Linux/3.19.0-16-generic\r\nAccept: */*\r\n\r\n')
False
>>> e = Entry('http://www.github.com/?bar=foo&foobar', Entry.GET, (Response(b'<html/>'),))
>>> e.can_handle(b'GET /?bar=foo&foobar HTTP/1.1\r\nHost: github.com\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nUser-Agent: python-requests/2.7.0 CPython/3.4.3 Linux/3.19.0-16-generic\r\nAccept: */*\r\n\r\n')
True
"""
try:
requestline, _ = decode_from_bytes(data).split(CRLF, 1)
method, path, version = self._parse_requestline(requestline)
except ValueError:
try:
return self == Mocket._last_entry
except AttributeError:
return False
uri = urlsplit(path)
can_handle = uri.path == self.path and method == self.method
if self._match_querystring:
kw = dict(keep_blank_values=True)
can_handle = can_handle and parse_qs(uri.query, **kw) == parse_qs(self.query, **kw)
if can_handle:
Mocket._last_entry = self
return can_handle
@staticmethod
def _parse_requestline(line):
"""
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
>>> Entry._parse_requestline('GET / HTTP/1.0') == ('GET', '/', '1.0')
True
>>> Entry._parse_requestline('post /testurl htTP/1.1') == ('POST', '/testurl', '1.1')
True
>>> Entry._parse_requestline('Im not a RequestLine')
Traceback (most recent call last):
...
ValueError: Not a Request-Line
"""
m = re.match(r'({})\s+(.*)\s+HTTP/(1.[0|1])'.format('|'.join(Entry.METHODS)), line, re.I)
if m:
return m.group(1).upper(), m.group(2), m.group(3)
else:
raise ValueError('Not a Request-Line')
@classmethod
def register(cls, method, uri, *responses, **config):
default_config = dict(match_querystring=True, add_trailing_slash=True)
default_config.update(config)
config = default_config
if config['add_trailing_slash'] and not urlsplit(uri).path:
uri += '/'
Mocket.register(cls(uri, method, responses, match_querystring=config['match_querystring']))
@classmethod
def single_register(cls, method, uri, body='', status=200, headers=None, match_querystring=True):
cls.register(
method, uri, cls.response_cls(
body=body, status=status, headers=headers
), match_querystring=match_querystring
)