def test_no_content_type(HTTPResponse, *ignored): """ Given a filename with unknown content type, no Content-Type header is set. """ fd = mock.Mock() mod.send_file(fd, 'foo') HTTPResponse.assert_called_once_with(fd, status=200)
def test_send_file_no_optional_args(HTTPResponse, *ignored): """ Given no optional arguments, no headers are generated. """ fd = mock.Mock() mod.send_file(fd) HTTPResponse.assert_called_once_with(fd, status=200)
def test_send_file_attachment_no_filename(HTTPResponse, *ignored): """ Given attachment flag and no filename, Content-Disposition header is not set. """ fd = mock.Mock() mod.send_file(fd, attachment=True) HTTPResponse.assert_called_once_with(fd, status=200)
def test_wrong_range(HTTPError, request, parse_range_header): """ Given size and invalid Range request header, HTTP 416 error should be returned. """ fd = mock.Mock() parse_range_header.return_value = [] mod.send_file(fd, 'foo', size=200) HTTPError.assert_called_once_with(416, 'Request Range Not Satisfiable')
def test_head(HTTPResponse, request): """ Given request method is HEAD, emtpy string is returned as body instead of the file descriptor. """ fd = mock.Mock() request.method = 'HEAD' mod.send_file(fd, 'foo') HTTPResponse.assert_called_once_with('', status=200)
def test_send_with_timestamp(HTTPResponse, request): """ Given a timestamp, it should set Last-Modified header. """ fd = mock.Mock() request.environ.get.return_value = None mod.send_file(fd, 'foo', timestamp=1429458831) expected_headers = {'Last-Modified': 'Sun, 19 Apr 2015 15:53:51 GMT'} HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_with_wrong_object(): """ Given an object that has no 'read' attribute, when calling send_file(), ValueError is raised. """ fd = mock.Mock() del fd.read with pytest.raises(ValueError): mod.send_file(fd, 'foo.txt')
def test_send_file_mime(HTTPResponse, *ignored): """ Given a file descriptor and filename, mime type is automatically guessed and appropriate Content-Type header is set. """ fd = mock.Mock() mod.send_file(fd, 'foo.pdf') expected_headers = {'Content-Type': 'application/pdf'} HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_custom_encoding(HTTPResponse, *ignored): """ Given a filename that matches a text type, when send_file() is called with specific encoding, the encoding is added to the Content-Type header. """ fd = mock.Mock() mod.send_file(fd, 'foo.html', charset='ascii') expected_headers = {'Content-Type': 'text/html; charset=ascii'} HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_mime_text(HTTPResponse, *ignored): """ Given a filename that matches a text type, appropriate charset is added to the Content-Type header. """ fd = mock.Mock() mod.send_file(fd, 'foo.html') expected_headers = {'Content-Type': 'text/html; charset=UTF-8'} HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_custom_encoding(HTTPResponse, *ignored): """ Given a filename that matches a text type, when send_file() is called with specific encoding, the encoding is added to the Content-Type header. """ fd = mock.Mock() mod.send_file(fd, 'foo.html', charset='ascii') expected_headers = { 'Content-Type': 'text/html; charset=ascii' } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_attachment(HTTPResponse, *ignored): """ Given both filename and attachment flag, Content-Disposition header is set. """ fd = mock.Mock() mod.send_file(fd, 'foo.pdf', attachment=True) expected_headers = { 'Content-Type': 'application/pdf', 'Content-Disposition': 'attachment; filename="foo.pdf"' } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_with_timestamp(HTTPResponse, request): """ Given a timestamp, it should set Last-Modified header. """ fd = mock.Mock() request.environ.get.return_value = None mod.send_file(fd, 'foo', timestamp=1429458831) expected_headers = { 'Last-Modified': 'Sun, 19 Apr 2015 15:53:51 GMT' } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_no_content_type_but_has_enc(HTTPResponse, *ignored): """ Given a filename for which only encoding can be calculated, no Content-Type header is set. """ fd = mock.Mock() mod.send_file(fd, 'foo.gz') expected_headers = { 'Content-Encoding': 'gzip', } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_mime_text(HTTPResponse, *ignored): """ Given a filename that matches a text type, appropriate charset is added to the Content-Type header. """ fd = mock.Mock() mod.send_file(fd, 'foo.html') expected_headers = { 'Content-Type': 'text/html; charset=UTF-8' } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_mime(HTTPResponse, *ignored): """ Given a file descriptor and filename, mime type is automatically guessed and appropriate Content-Type header is set. """ fd = mock.Mock() mod.send_file(fd, 'foo.pdf') expected_headers = { 'Content-Type': 'application/pdf' } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_with_size(HTTPResponse, request): """ Given a size, it should set both Content-Length and Accept-Ranges headers. """ fd = mock.Mock() request.environ.get.return_value = None mod.send_file(fd, 'foo', 200) expected_headers = { 'Content-Length': 200, 'Accept-Ranges': 'bytes', } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_send_file_enctype(HTTPResponse, *ignored): """ Given a filename with .gz extension, when send_file() is called, it should set Content-Encoding header. """ fd = mock.Mock() mod.send_file(fd, 'foo.tar.gz') expected_headers = { 'Content-Type': 'application/x-tar', 'Content-Encoding': 'gzip', } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_range_wrapper(HTTPResponse, request, parse_range_header): """ Given size and valid Range request header, file descriptor is wrapped, and Content-Range and Content-Length headers are set, with 206 status code. """ fd = mock.Mock() wrapper = mock.Mock() parse_range_header.return_value = ((20, 300),) mod.send_file(fd, 'foo', size=400, wrapper=wrapper) expected_headers = { 'Accept-Ranges': 'bytes', 'Content-Length': '280', 'Content-Range': 'bytes 20-299/400', } wrapper.assert_called_once_with(fd, 20, 280) HTTPResponse.assert_called_once_with( wrapper.return_value, status=206, **expected_headers)
def test_range_wrapper(HTTPResponse, request, parse_range_header): """ Given size and valid Range request header, file descriptor is wrapped, and Content-Range and Content-Length headers are set, with 206 status code. """ fd = mock.Mock() wrapper = mock.Mock() parse_range_header.return_value = ((20, 300), ) mod.send_file(fd, 'foo', size=400, wrapper=wrapper) expected_headers = { 'Accept-Ranges': 'bytes', 'Content-Length': '280', 'Content-Range': 'bytes 20-299/400', } wrapper.assert_called_once_with(fd, 20, 280) HTTPResponse.assert_called_once_with(wrapper.return_value, status=206, **expected_headers)
def test_if_modified_since_stale(HTTPResponse, request, getmtime, parse_date): """ Given timestamp and If-Modified-Since request header and timestamp that is newer than the header value, when calling send_file(), HTTP 304 response is created instead of the regular response. A Date header should be set to current timestamp. """ fd = mock.Mock() timestamp = 1428841335 modsince = 'Sat, 24 Apr 2015 12:22:14 GMT' parse_date.return_value = 1428841334 getmtime.return_value = time.struct_time( (2015, 4, 19, 16, 48, 12, 6, 0, 0)) request.environ.get.return_value = modsince mod.send_file(fd, 'foo', timestamp=timestamp) expected_headers = { 'Last-Modified': 'Sun, 19 Apr 2015 16:48:12 GMT', } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)
def test_if_modified_since_stale(HTTPResponse, request, getmtime, parse_date): """ Given timestamp and If-Modified-Since request header and timestamp that is newer than the header value, when calling send_file(), HTTP 304 response is created instead of the regular response. A Date header should be set to current timestamp. """ fd = mock.Mock() timestamp = 1428841335 modsince = 'Sat, 24 Apr 2015 12:22:14 GMT' parse_date.return_value = 1428841334 getmtime.return_value = time.struct_time(( 2015, 4, 19, 16, 48, 12, 6, 0, 0)) request.environ.get.return_value = modsince mod.send_file(fd, 'foo', timestamp=timestamp) expected_headers = { 'Last-Modified': 'Sun, 19 Apr 2015 16:48:12 GMT', } HTTPResponse.assert_called_once_with(fd, status=200, **expected_headers)