From 30ede029d53652f35336524a8f816d68b05b80cd Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 23 Jun 2016 13:40:57 -0400 Subject: [PATCH] Fix GeneratorFile for working with BufferedReader The user files system uses a BufferedReader along with the magic library to determine the mime type of the user file being served. Currently, BufferedReader fails with an exception on Swift storage, because Swift storage returns a GeneratorFile, which is missing the `readable()` method. --- test/test_util.py | 28 ++++++++++++++++++++++++++++ util/registry/generatorfile.py | 3 +++ 2 files changed, 31 insertions(+) diff --git a/test/test_util.py b/test/test_util.py index fc73407d8..8b1022940 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,4 +1,5 @@ import unittest +import magic from itertools import islice from semantic_version import Version, Spec @@ -7,6 +8,7 @@ from util.validation import generate_valid_usernames from util.registry.generatorfile import GeneratorFile from util.registry.dockerver import docker_version from util import slash_join +from _pyio import BufferedReader class TestGeneratorFile(unittest.TestCase): def sample_generator(self): @@ -76,6 +78,32 @@ class TestGeneratorFile(unittest.TestCase): self.assertEquals("thisisatest", f.read(60)) self.assertEquals(11, f.tell()) + def test_with_bufferedreader(self): + with GeneratorFile(self.sample_generator()) as f: + buffered = BufferedReader(f) + self.assertEquals("thisisatest", buffered.peek(10)) + self.assertEquals("thisisates", buffered.read(10)) + + def mimed_html_generator(self): + yield '' + yield '' + yield 'sometext' * 1024 + yield '' + yield '' + + def test_magic(self): + mgc = magic.Magic(mime=True) + + with GeneratorFile(self.mimed_html_generator()) as f: + buffered = BufferedReader(f) + file_header_bytes = buffered.peek(1024) + self.assertEquals("text/html", mgc.from_buffer(file_header_bytes)) + + with GeneratorFile(self.sample_generator()) as f: + buffered = BufferedReader(f) + file_header_bytes = buffered.peek(1024) + self.assertEquals("text/plain", mgc.from_buffer(file_header_bytes)) + class TestUsernameGenerator(unittest.TestCase): def assert_generated_output(self, input_username, expected_output): diff --git a/util/registry/generatorfile.py b/util/registry/generatorfile.py index e7d2bb657..90f971267 100644 --- a/util/registry/generatorfile.py +++ b/util/registry/generatorfile.py @@ -33,6 +33,9 @@ class GeneratorFile(object): raise StopIteration return r + def readable(self): + return not self._closed + def readline(self): buf = [] while True: