Merge pull request #58 from spulec/better-callback-streaming-posts

Better callback streaming posts
This commit is contained in:
Gabriel Falcão
2013-05-03 14:44:35 -07:00
2 changed files with 23 additions and 4 deletions

View File

@@ -242,8 +242,13 @@ class fakesock(object):
headers = utf8(last_requestline(self._sent_data))
body = utf8(self._sent_data[-1])
if self._entry.body_is_callable and hasattr(self, 'callable_body'):
self.callable_body(self.request, self.info.full_url(), headers)
last_entry = self._entry
last_entry.request.body = body
request_headers = dict(last_entry.request.headers)
# If we are receiving more data and the last entry to be processed
# was a callback responsed, send the new data to the callback
if last_entry.body_is_callable:
last_entry.callable_body(last_entry.request, last_entry.info.full_url(), request_headers)
try:
return httpretty.historify_request(headers, body, False)

View File

@@ -536,6 +536,13 @@ def test_httpretty_should_allow_multiple_methods_for_the_same_uri():
request_action = getattr(requests, method.lower())
expect(request_action(url).text).to.equal(method)
data_received = []
def my_callback(request, url, headers):
if request.body.strip():
data_received.append(request.body.strip())
return 200, headers, "Received"
@httprettified
def test_httpretty_should_allow_registering_regexes_with_streaming_responses():
@@ -544,7 +551,7 @@ def test_httpretty_should_allow_registering_regexes_with_streaming_responses():
HTTPretty.register_uri(
HTTPretty.POST,
re.compile("https://api.yipit.com/v1/deal;brand=(?P<brand_name>\w+)"),
body="Found brand",
body=my_callback,
)
def gen():
@@ -555,7 +562,14 @@ def test_httpretty_should_allow_registering_regexes_with_streaming_responses():
'https://api.yipit.com/v1/deal;brand=gap?first_name=chuck&last_name=norris',
data=gen(),
)
expect(response.text).to.equal('Found brand')
expect(data_received).to.equal([
b'2',
b'hi',
b'5',
b'there',
b'0',
b'0',
])
expect(HTTPretty.last_request.method).to.equal('POST')
expect(HTTPretty.last_request.path).to.equal('/v1/deal;brand=gap?first_name=chuck&last_name=norris')