Skip to content

Commit

Permalink
tests for Partitial generator response
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Oct 23, 2023
1 parent ea6db95 commit 7bd96dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
14 changes: 14 additions & 0 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ def test_partial_empty(req):
return res


@app.route('/test/partial/generator')
def test_partial_generator(req):
def gen():
for i in range(10):
yield b"line %d\n" % i

res = GeneratorResponse(gen(), content_length=70)
ranges = {}
if 'Range' in req.headers:
ranges = parse_range(req.headers['Range'])
res.make_partial(ranges.get("bytes", None))
return res


@app.route('/yield')
def yielded(req):
"""Simple response generator by yield."""
Expand Down
29 changes: 21 additions & 8 deletions tests_integrity/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ def test_file_response_304_etag(self, url):
headers={'If-None-Match': etag},
status_code=304)

def test_partial_file(self, url):

class TestPartialResponse():
"""Tests for Partial Responses"""

def test_file(self, url):
res = check_url(url+"/simple.py",
headers={'Range': 'bytes=-100'},
status_code=206)
Expand All @@ -125,18 +129,27 @@ def test_partial_file(self, url):
def test_empty_response(self, url):
check_url("{url}/test/empty".format(url=url), status_code=204)

def test_partial_empty(self, url):
def test_empty(self, url):
check_url("{url}/test/partial/empty".format(url=url), status_code=200)

def test_partial_empty_first_100(self, url):
def test_empty_first_100(self, url):
check_url("{url}/test/partial/empty".format(url=url),
headers={'Range': 'bytes=0-100'},
headers={'Range': 'bytes=0-99'},
status_code=416)

def test_partial_empty_last_100(self, url):
check_url("{url}/test/partial/empty".format(url=url),
headers={'Range': 'bytes=-100'},
status_code=416)
def test_first_15(self, url):
res = check_url("{url}/test/partial/generator".format(url=url),
headers={'Range': 'bytes=0-14'},
status_code=206)
assert len(res.text) == 15
assert res.text[-22:] == "line 0\nline 1\nl"

def test_last_15(self, url):
res = check_url("{url}/test/partial/generator".format(url=url),
headers={'Range': 'bytes=-15'},
status_code=206)
assert len(res.text) == 15
assert res.text[-22:] == "\nline 8\nline 9\n"


class TestSession():
Expand Down

0 comments on commit 7bd96dd

Please sign in to comment.