Exemplo n.º 1
0
 def test_setting_file_for_download_includes_a_content_length_header(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         tmpfile.write(b'foobar')
         tmpfile.seek(0)
         response.file(tmpfile.name, download=True, name='image.png')
         self.assertEqual(response.headers['Content-Length'], '6')
Exemplo n.º 2
0
 def test_setting_file_for_download(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         response.file(tmpfile.name, download=True)
         filename = os.path.basename(tmpfile.name)
         self.assertEqual(response.headers['Content-Disposition'],
                          'attachment; filename="{}"'.format(filename))
Exemplo n.º 3
0
 def test_setting_file_without_extension_changes_content_type_to_octet_stream(
         self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         response.file(tmpfile.name)
         self.assertEqual(response.headers['Content-Type'],
                          'application/octet-stream')
Exemplo n.º 4
0
 def test_setting_file_without_name_puts_the_path_filename_in_the_header(
         self):
     response = Response()
     with NamedTemporaryFile(suffix='.png') as tmpfile:
         response.file(tmpfile.name)
         filename = os.path.basename(tmpfile.name)
         self.assertEqual(response.headers['Content-Disposition'],
                          'inline; filename="{}"'.format(filename))
Exemplo n.º 5
0
 def test_file_gets_returned_as_generator_to_wsgi(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         tmpfile.write(b'hello world')
         tmpfile.seek(0)
         response.file(tmpfile.name)
         start_respose = Mock()
         self.assertIsInstance(response.wsgi(start_respose), GeneratorType)
Exemplo n.º 6
0
 def test_all_file_contents_are_yielded_by_its_generator(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         tmpfile.write(b'hello world')
         tmpfile.seek(0)
         response.file(tmpfile.name)
         start_respose = Mock()
         file_generator = response.wsgi(start_respose)
         contents = b''
         for chunk in file_generator:
             contents += chunk
         self.assertEqual(contents, b'hello world')
Exemplo n.º 7
0
 def test_setting_inexistent_file_for_download_triggers_exception(self):
     response = Response()
     with self.assertRaises(FileNotFoundError):
         response.file('foobar.file')
Exemplo n.º 8
0
 def test_setting_file_for_download_with_name(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         response.file(tmpfile.name, download=True, name='image.png')
         self.assertEqual(response.headers['Content-Disposition'],
                          'attachment; filename="image.png"')
Exemplo n.º 9
0
 def test_setting_file_with_name(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         response.file(tmpfile.name, name='image.png')
         self.assertEqual(response.headers['Content-Disposition'],
                          'inline; filename="image.png"')
Exemplo n.º 10
0
 def test_setting_file_without_specified_mime_type_makes_it_guess(self):
     response = Response()
     with NamedTemporaryFile(suffix='.png') as tmpfile:
         response.file(tmpfile.name)
         self.assertEqual(response.headers['Content-Type'], 'image/png')
Exemplo n.º 11
0
 def test_setting_file_with_specified_mime_type(self):
     response = Response()
     with NamedTemporaryFile() as tmpfile:
         response.file(tmpfile.name, type='image/png')
         self.assertEqual(response.headers['Content-Type'], 'image/png')