def test_force_seekable(self):

        with mockup_http_static_server(self.cat_jpeg) as http_server:
            url = 'http://%s:%s' % http_server.server_address
            original_sum = md5sum(self.cat_jpeg)

            with AttachableDescriptor(url) as descriptor:
                descriptor.prepare_to_read(backend='file')
                self.assertEqual(original_sum, md5sum(descriptor))

            with AttachableDescriptor(url) as descriptor:
                descriptor.prepare_to_read(backend='temp')
                self.assertEqual(original_sum, md5sum(descriptor))

            with AttachableDescriptor(url) as descriptor:
                descriptor.prepare_to_read(backend='memory')
                self.assertEqual(original_sum, md5sum(descriptor))

            with AttachableDescriptor(url) as descriptor:
                # Reading some bytes, before making the stream seekable
                descriptor.get_header_buffer()
                descriptor.prepare_to_read(backend='temp')
                self.assertEqual(original_sum, md5sum(descriptor))

            with AttachableDescriptor(url) as descriptor:
                self.assertRaises(DescriptorOperationError, descriptor.prepare_to_read, backend='InvalidBackend')

            with open(self.dog_jpeg, 'rb') as f, AttachableDescriptor(url) as descriptor:
                descriptor.replace(f, position=1024)

            with open(self.dog_jpeg, 'rb') as f, AttachableDescriptor(url) as descriptor:
                descriptor.replace(f)
                self.assertEqual(md5sum(descriptor), md5sum(self.dog_jpeg))
    def test_localfs(self):

        descriptor = AttachableDescriptor(self.cat_jpeg, width=100, height=80)
        self.assertIsInstance(descriptor, LocalFileSystemDescriptor)
        self.assertEqual(descriptor.filename, self.cat_jpeg)

        # Must be determined from the given file's extension: .jpg
        self.assertEqual(descriptor.content_type, 'image/jpeg')
        self.assertEqual(descriptor.original_filename, self.cat_jpeg)

        self.assertEqual(descriptor.width, 100)
        self.assertEqual(descriptor.height, 80)

        self.assertEqual(len(descriptor.get_header_buffer()), 1024)

        buffer = io.BytesIO()
        copy_stream(descriptor, buffer)
        buffer.seek(0)
        self.assertEqual(md5sum(buffer), md5sum(self.cat_jpeg))
    def test_cgi_field_storage(self):
        # encode a multipart form
        content_type, body, content_length = encode_multipart_data(files=dict(cat=self.cat_jpeg))
        environ = {
            'REQUEST_METHOD': 'POST',
            'CONTENT_TYPE': content_type,
            'CONTENT_LENGTH': content_length
        }

        storage = cgi.FieldStorage(body, environ=environ)

        descriptor = AttachableDescriptor(storage['cat'])
        self.assertIsInstance(descriptor, CgiFieldStorageDescriptor)
        self.assertEqual(descriptor.content_type, 'image/jpeg')
        self.assertEqual(descriptor.original_filename, split(self.cat_jpeg)[1])

        buffer = io.BytesIO()
        copy_stream(descriptor, buffer)
        buffer.seek(0)
        self.assertEqual(md5sum(buffer), md5sum(self.cat_jpeg))