Skip to content

Commit

Permalink
added file support for HTTP client and improved generator checking
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 19, 2018
1 parent 8c4b034 commit 9fd4b4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/quorum/httpc.py
Expand Up @@ -433,6 +433,7 @@ def _method_empty(
redirect = redirect,
timeout = timeout
)
result = _result(result, info)
return (result, file) if handle else result

def _method_payload(
Expand Down Expand Up @@ -505,6 +506,7 @@ def _method_payload(
timeout = timeout
)

result = _result(result, info)
return (result, file) if handle else result

def _redirect(
Expand Down Expand Up @@ -550,8 +552,10 @@ def _resolve(*args, **kwargs):
return result

def _resolve_legacy(url, method, headers, data, silent, timeout, **kwargs):
is_generator = not data == None and not legacy.is_string(data)
is_generator = not data == None and legacy.is_generator(data)
if is_generator: next(data); data = b"".join(data)
is_file = hasattr(data, "tell")
if is_file: data = data.read()
opener = legacy.build_opener(legacy.HTTPHandler)
request = legacy.Request(url, data = data, headers = headers)
request.get_method = lambda: method
Expand All @@ -561,11 +565,11 @@ def _resolve_requests(url, method, headers, data, silent, timeout, **kwargs):
import requests

# verifies if the provided data is a generator, assumes that if the
# data is not invalid and not of types string then it's a generator
# data is not invalid and is of type generator then it's a generator
# and then if that's the case encapsulates this size based generator
# into a generator based file-like object so that it can be used inside
# the request infra-structure (as it accepts only file objects)
is_generator = not data == None and not legacy.is_string(data)
is_generator = not data == None and legacy.is_generator(data)
if is_generator: data = structures.GeneratorFile(data)

method = method.lower()
Expand Down
33 changes: 33 additions & 0 deletions src/quorum/test/httpc.py
Expand Up @@ -152,6 +152,39 @@ def test_get_f(self):
self.assertEqual(len(file.data) > 100, True)
self.assertEqual(len(file.data_b64) > 100, True)

@quorum.secured
def test_generator(self):
def text_g(message = [b"hello", b" ", b"world"]):
yield sum(len(value) for value in message)
for value in message:
yield value

data, response = quorum.post_json(
"https://%s/post" % self.httpbin,
data = text_g(),
handle = True,
reuse = False
)

code = response.getcode()
self.assertNotEqual(code, 302)
self.assertEqual(code, 200)
self.assertEqual(data["data"], "hello world")

@quorum.secured
def test_file(self):
data, response = quorum.post_json(
"https://%s/post" % self.httpbin,
data = quorum.legacy.BytesIO(b"hello world"),
handle = True,
reuse = False
)

code = response.getcode()
self.assertNotEqual(code, 302)
self.assertEqual(code, 200)
self.assertEqual(data["data"], "hello world")

@quorum.secured
def test_multithread(self):
threads = []
Expand Down

0 comments on commit 9fd4b4c

Please sign in to comment.