def test_non_seekable(self):
        class NonSeekableStream(io.BytesIO):
            def seekable(self, *args, **kwargs):
                return False

        inp = b'abcdefghijklmnopqrstuvwxyz'
        descriptor = AttachableDescriptor(NonSeekableStream(inp),
                                          header_buffer_size=10)

        # Fetching header, it forces to cache header_buffer_size bytes from
        # header.
        buffer = descriptor.get_header_buffer()
        self.assertEqual(buffer, b'abcdefghij')

        # fetching again to test the cache functionality
        buffer = descriptor.get_header_buffer()
        self.assertEqual(buffer, b'abcdefghij')

        out = b''
        out += descriptor.read(9)
        self.assertEqual(descriptor.tell(), 9)
        out += descriptor.read(11)
        self.assertEqual(descriptor.tell(), 20)
        out += descriptor.read(10)
        self.assertEqual(out, inp)

        # Max length error
        descriptor = AttachableDescriptor(NonSeekableStream(inp),
                                          header_buffer_size=24,
                                          max_length=20)
        buffer = descriptor.get_header_buffer()
        self.assertEqual(buffer, b'abcdefghijklmnopqrstuvwx')
        self.assertRaises(MaximumLengthIsReachedError, descriptor.read, 1)

        # Test getting header buffer after read on non-seekable streams.
        descriptor = AttachableDescriptor(NonSeekableStream(inp),
                                          header_buffer_size=10,
                                          max_length=20)
        self.assertEqual(descriptor.read(10), b'abcdefghij')
        self.assertRaises(DescriptorOperationError,
                          descriptor.get_header_buffer)
    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))