Skip to content

Commit

Permalink
Merge pull request #293 from bhodorog/bugfix/recording-uses-headers-body
Browse files Browse the repository at this point in the history
Use body and headers when recording upstream requests
  • Loading branch information
gabrielfalcao committed May 26, 2016
2 parents 7f1f67d + cfdbc78 commit e0e0602
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion httpretty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,10 @@ def record(cls, filename, indentation=4, encoding='utf-8'):
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,
Expand Down
6 changes: 5 additions & 1 deletion tests/functional/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ def get(self, matched):

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):
Expand Down
9 changes: 7 additions & 2 deletions tests/functional/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e0e0602

Please sign in to comment.