示例#1
0
 def test_store_to_disk_fail_invalid_type(self):
     execution = self.mk_exec_locals()
     # remove a required key
     execution.locals['__metadata_path__'] = None
     response = Response('hello world',
                         headers={
                             'content-type': 'text/plain',
                         })
     with self.assertRaises(TypeError):
         response.store_to_disk(execution)
     self.assertFalse(exists(execution.locals['__path__']))
示例#2
0
 def test_store_to_disk_fail_cannot_create_dir(self):
     execution = self.mk_exec_locals()
     # prevent directory creation with an empty file
     execution.locals['__path__'].parent.write_bytes(b'')
     response = Response('hello world',
                         headers={
                             'content-type': 'text/plain',
                         })
     with self.assertRaises(ValueError):
         response.store_to_disk(execution)
     self.assertFalse(exists(execution.locals['__path__']))
     self.assertFalse(exists(execution.locals['__metadata_path__']))
示例#3
0
    def test_restore_from_disk_failure(self):
        execution = self.mk_exec_locals()

        execution.locals['__path__'].parent.mkdir()
        execution.locals['__path__'].write_bytes(b'hi')
        with self.assertRaises(FileNotFoundError):
            Response.restore_from_disk(execution)
        execution.locals['__path__'].unlink()

        execution.locals['__metadata_path__'].parent.mkdir()
        execution.locals['__metadata_path__'].write_bytes(b'hi')
        with self.assertRaises(FileNotFoundError):
            Response.restore_from_disk(execution)
        execution.locals['__metadata_path__'].unlink()
示例#4
0
    def test_store_to_disk_success_roundtrip(self):
        execution = self.mk_exec_locals()
        self.assertFalse(exists(execution.locals['__path__']))
        self.assertFalse(exists(execution.locals['__metadata_path__']))
        response = Response('hello world',
                            headers={
                                'content-type': 'text/plain',
                            })
        response.store_to_disk(execution)
        self.assertTrue(exists(execution.locals['__path__']))
        self.assertTrue(exists(execution.locals['__metadata_path__']))

        new_response = Response.restore_from_disk(execution)
        self.assertEqual(new_response.content, response.content)
        self.assertEqual(new_response.headers, response.headers)
示例#5
0
 def __call__(self, identifier):
     mimetype, encoding = self.mimetypes.guess_type(identifier)
     content = self.loader(identifier)
     headers = {
         'Content-type': mimetype,
     }
     return Response(content, headers)
示例#6
0
 def test_response_headers(self):
     response = Response('text', headers={
         'content-length': '4',
     })
     self.assertEqual(response.content, b'text')
     self.assertEqual(response.headers, {
         'content-length': '4',
     })
示例#7
0
    def __call__(self, mold_id, data={}, content_type='text/html'):
        """
        Generates a full response
        """

        content = self.engine.render(mold_id, data=data)
        headers = {
            'Content-type': content_type,
        }
        return Response(content, headers)
示例#8
0
 def test_response_base(self):
     response = Response('text')
     self.assertEqual(response.content, b'text')
     self.assertEqual(response.headers, {})
示例#9
0
 def __call__(self, **data):
     return Response(self.template.render(**self.build_data(**data)),
                     self.headers)