Skip to content

Commit

Permalink
Final version of JSON dump uses bytes.hex().
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Salluzzo committed Feb 22, 2017
1 parent 2b36a16 commit 5b7bf2d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
55 changes: 9 additions & 46 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
self.fd = io.BytesIO()
self._closed = True
self._connected = False
self._buflen = 1024
self._buflen = 65536
self._entry = None
self.family = family
self.type = type
Expand Down Expand Up @@ -245,66 +245,29 @@ def true_sendall(self, data, *args, **kwargs):

# try to get the response from the dictionary
try:
lines = response_dict['response']
gzipped_lines = response_dict['gzip']
r_lines = []
for line_no, line in enumerate(lines):
line = encode_to_bytes(line)
if line_no + 1 in gzipped_lines:
gzip_buffer = io.BytesIO()
gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer)
try:
gzip_file.write(line)
finally:
gzip_file.close()
line = gzip_buffer.getvalue()
r_lines.append(line)
encoded_response = b'\r\n'.join(r_lines)
encoded_response = bytes.fromhex(response_dict['response'])

# if not available, call the real sendall
except KeyError:
self._connect()
self.true_socket.sendall(data, *args, **kwargs)
r = io.BytesIO()
encoded_response = b''
# https://github.com/kennethreitz/requests/blob/master/tests/testserver/server.py#L13
while select.select([self.true_socket], [], [], 0.5)[0]:
recv = self.true_socket.recv(self._buflen)
if recv:
r.write(recv)
encoded_response += recv
else:
break

encoded_response = r.getvalue()

response_dict['request'] = req
lines = response_dict['response'] = []
gzipped_lines = response_dict['gzip'] = []

# default encoding to UTF-8
charset = encoding

# update the dictionary with the response obtained
for line_no, line in enumerate(encoded_response.split(b'\r\n')):
try:
line = decode_from_bytes(line, charset=charset)
_, params = cgi.parse_header(line)
if params and 'charset' in params:
charset = params['charset']
except UnicodeDecodeError:
f = gzip.GzipFile(mode='rb', fileobj=io.BytesIO(line))
try:
line = f.read(len(line))
finally:
f.close()
line = decode_from_bytes(line)
gzipped_lines.append(line_no + 1)

lines.append(line)

# dump the resulting dictionary to a JSON file
if Mocket.get_truesocket_recording_dir():

with io.open(path, mode='w', encoding=encoding) as f:
# update the dictionary with request and response lines
response_dict['request'] = req
response_dict['response'] = encoded_response.hex()

with io.open(path, mode='w') as f:
f.write(decode_from_bytes(json.dumps(responses, indent=4, sort_keys=True)))

# response back to .sendall() which writes it to the mocket socket and flush the BytesIO
Expand Down
2 changes: 1 addition & 1 deletion tests/main/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_truesendall_with_gzip_recording(self):

@mocketize(truesocket_recording_dir=recording_directory)
def test_truesendall_with_chunk_recording(self):
url = 'http://httpbin.org/range/2048?chunk_size=256'
url = 'http://httpbin.org/range/70000?chunk_size=65536'

requests.get(url)
resp = requests.get(url)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"httpbin.org": {
"80": {
"eaa1896a5d6eed6d4dd83a6696efc485": {
"gzip": [],
"request": "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nuser-agent: Fake-User-Agent\r\n\r\n",
"response": ["HTTP/1.1 200 OK", "Server: nginx", "Date: Wed, 15 Feb 2017 13:28:10 GMT", "Content-Type: application/json", "Content-Length: 33", "Connection: keep-alive", "Access-Control-Allow-Origin: *", "Access-Control-Allow-Credentials: true", "", "{\n \"origin\": \"185.27.157.132\"\n}\n"]
},
"31ae71d6074aa0e73fb4feb91f28546a": {
"gzip": [11],
"request": "GET /gzip HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nuser-agent: Fake-User-Agent\r\n\r\n",
"response": ["HTTP/1.1 200 OK", "Server: nginx", "Date: Wed, 15 Feb 2017 13:28:10 GMT", "Content-Type: application/json", "Content-Length: 172", "Connection: keep-alive", "Content-Encoding: gzip", "Access-Control-Allow-Origin: *", "Access-Control-Allow-Credentials: true", "", "{\n \"gzipped\": true, \n \"headers\": {\n \"Accept\": \"*/*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"Fake-User-Agent\"\n }, \n \"method\": \"GET\", \n \"origin\": \"185.27.157.132\"\n}\n"]
}
}
}
"httpbin.org": {
"80": {
"31ae71d6074aa0e73fb4feb91f28546a": {
"request": "GET /gzip HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nuser-agent: Fake-User-Agent\r\n\r\n",
"response": "485454502f312e3120323030204f4b0d0a5365727665723a206e67696e780d0a446174653a205765642c2032322046656220323031372031313a33383a313020474d540d0a436f6e74656e742d547970653a206170706c69636174696f6e2f6a736f6e0d0a436f6e74656e742d4c656e6774683a203137300d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a436f6e74656e742d456e636f64696e673a20677a69700d0a4163636573732d436f6e74726f6c2d416c6c6f772d4f726967696e3a202a0d0a4163636573732d436f6e74726f6c2d416c6c6f772d43726564656e7469616c733a20747275650d0a0d0a1f8b08002278ad5802ff4d8e410ec2201045f79c82ccb22998eaa2d65d17550fa007a865a4a40a047063d3bb0b34695cfef7df9fcc4c2805f955d6a280130dee83254d6cc45ea0f391cd3146d00e03da103314bb02b2b451d6e9c108a565aad3b5920a7cbefa809b78353e8fc710ec43696e9cdcbabb47c75a893a1be77e42f687a2b3ac3fbd318c26bd0997eeb6aec13825954eac39f07dd5f063cdabba06b2901f8d4f4c9bdc000000"
},
"eaa1896a5d6eed6d4dd83a6696efc485": {
"request": "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nuser-agent: Fake-User-Agent\r\n\r\n",
"response": "485454502f312e3120323030204f4b0d0a5365727665723a206e67696e780d0a446174653a205765642c2032322046656220323031372031313a33383a303920474d540d0a436f6e74656e742d547970653a206170706c69636174696f6e2f6a736f6e0d0a436f6e74656e742d4c656e6774683a2033320d0a436f6e6e656374696f6e3a206b6565702d616c6976650d0a4163636573732d436f6e74726f6c2d416c6c6f772d4f726967696e3a202a0d0a4163636573732d436f6e74726f6c2d416c6c6f772d43726564656e7469616c733a20747275650d0a0d0a7b0a2020226f726967696e223a202239332e3231392e38372e313737220a7d0a"
}
}
}
}

0 comments on commit 5b7bf2d

Please sign in to comment.