def test_range_request_header(self): request = HttpRequest() response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response['Accept-ranges'], 'bytes') self.assertEqual(response['Content-length'], '90') self._verify_response_content(response, 0, 90) # Request with both bounds request = HttpRequest() request.META['HTTP_RANGE'] = 'bytes=5-7' response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response.status_code, 206) self.assertEqual(response['Content-length'], '3') self.assertEqual(response['Content-range'], 'bytes 5-7/90') self._verify_response_content(response, 5, 7+1) # Request with start bound request = HttpRequest() request.META['HTTP_RANGE'] = 'bytes=1-' response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response.status_code, 206) self.assertEqual(response['Content-length'], '89') self.assertEqual(response['Content-range'], 'bytes 1-89/90') self._verify_response_content(response, 1, 90) # Request with end bound request = HttpRequest() request.META['HTTP_RANGE'] = 'bytes=-1' response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response.status_code, 206) self.assertEqual(response['Content-length'], '2') self.assertEqual(response['Content-range'], 'bytes 0-1/90') self._verify_response_content(response, 0, 1+1) # Out of bounds request request = HttpRequest() request.META['HTTP_RANGE'] = 'bytes=0-90' response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response['Content-range'], 'bytes */90') self.assertEqual(response.status_code, 416) # Malformed headers, MUST be ignored according to spec for hdr in ['bytes=-', 'bytes=foo', 'bytes=3-2']: request = HttpRequest() request.META['HTTP_RANGE'] = hdr response = real_sendfile(request, self.filepath, accept_ranges=True) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-length'], '90') self._verify_response_content(response, 0, 90) # Without accept-ranges, it should be disabled request = HttpRequest() request.META['HTTP_RANGE'] = 'bytes=5-7' response = real_sendfile(request, self.filepath) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-length'], '90') self._verify_response_content(response, 0, 90)
def test_attachment_filename(self): response = real_sendfile(HttpRequest(), self._get_readme(), attachment=True, attachment_filename='tests.txt') self.assertTrue(response is not None) self.assertEqual('attachment; filename="tests.txt"', response['Content-Disposition'])
def test_set_encoding(self): response = real_sendfile(HttpRequest(), self._get_readme(), encoding='utf8') self.assertTrue(response is not None) self.assertEqual('utf8', response['Content-Encoding'])
def test_set_mimetype(self): response = real_sendfile(HttpRequest(), self._get_readme(), mimetype='text/plain') self.assertTrue(response is not None) self.assertEqual('text/plain', response['Content-Type'])
def test_404(self): try: real_sendfile(HttpRequest(), 'fhdsjfhjk.txt') except Http404: pass
def test_xaccelredirect_header_containing_unicode(self): filepath = self.ensure_file(u'péter_là_gueule.txt') response = real_sendfile(HttpRequest(), filepath) self.assertTrue(response is not None) self.assertEqual(u'/private/péter_là_gueule.txt', response['X-Accel-Redirect'].decode('utf-8'))
def test_correct_url_in_xaccelredirect_header(self): filepath = self.ensure_file('readme.txt') response = real_sendfile(HttpRequest(), filepath) self.assertTrue(response is not None) self.assertEqual('/private/readme.txt', response['X-Accel-Redirect'])
def test_xsendfile_header_containing_unicode(self): filepath = self.ensure_file(u'péter_là_gueule.txt') response = real_sendfile(HttpRequest(), filepath) self.assertTrue(response is not None) self.assertEqual(filepath, response['X-Sendfile'].decode('utf-8'))
def test_correct_file_in_xsendfile_header(self): filepath = self.ensure_file('readme.txt') response = real_sendfile(HttpRequest(), filepath) self.assertTrue(response is not None) self.assertEqual(filepath, response['X-Sendfile'])