Merge pull request #293 from bhodorog/bugfix/recording-uses-headers-body

Use body and headers when recording upstream requests
This commit is contained in:
Gabriel Falcão 2016-05-26 15:51:25 -04:00
commit e0e0602809
3 changed files with 16 additions and 4 deletions

View File

@ -914,7 +914,10 @@ class httpretty(HttpBaseClass):
def record_request(request, uri, headers):
cls.disable()
response = http.request(request.method, uri)
kw = {}
kw.setdefault('body', request.body)
kw.setdefault('headers', dict(request.headers))
response = http.request(request.method, uri, **kw)
calls.append({
'request': {
'uri': uri,

View File

@ -61,7 +61,11 @@ class JSONEchoHandler(tornado.web.RequestHandler):
def post(self, matched):
payload = dict(self.request.arguments)
self.write(json.dumps({matched or 'index': payload}, indent=4))
self.write(json.dumps({
matched or 'index': payload,
'req_body': self.request.body,
'req_headers': dict(self.request.headers.items()),
}, indent=4))
class JSONEchoServer(threading.Thread):

View File

@ -708,7 +708,9 @@ def test_recording_calls(port):
# When I record some calls
with HTTPretty.record(destination):
requests.get(server_url("/foobar?name=Gabriel&age=25", port))
requests.post(server_url("/foobar", port), data=json.dumps({'test': '123'}))
requests.post(server_url("/foobar", port),
data=json.dumps({'test': '123'}),
headers={"Test": "foobar"})
# Then the destination path should exist
os.path.exists(destination).should.be.true
@ -750,7 +752,10 @@ def test_recording_calls(port):
# Then the responses should be the expected
response1.json().should.equal({"foobar": {"age": "25", "name": "Gabriel"}})
response2.json().should.equal({"foobar": {}})
response2.json()["foobar"].should.equal({})
response2.json()["req_body"].should.equal(json.dumps({"test": "123"}))
response2.json()["req_headers"].should.have.key("Test")
response2.json()["req_headers"]["Test"].should.equal("foobar")
@httprettified