def serve_unresized_image(self, blobkey, environ, start_response): """Use blob_download to rewrite and serve unresized image directly.""" state = request_rewriter.RewriterState( environ, '200 OK', [(blobstore.BLOB_KEY_HEADER, blobkey)], []) blob_download.blobstore_download_rewriter(state) start_response(state.status, list(state.headers.items())) return state.body
def serve_unresized_image(self, blobkey, environ, start_response): """Use blob_download to rewrite and serve unresized image directly.""" state = request_rewriter.RewriterState(environ, '200 OK', [ (blobstore.BLOB_KEY_HEADER, blobkey)], []) blob_download.blobstore_download_rewriter(state) start_response(state.status, list(state.headers.items())) return state.body
def do_blob_range_test(self, blobrange, expected_range, expected_body, test_range_request=False, expect_unsatisfiable=False): """Performs a blob range response test. Args: blobrange: Value of the X-AppEngine-BlobRange response header. expected_range: Expected Content-Range. expected_body: Expected body. test_range_request: If True, tests with a Range request header instead of an X-AppEngine-BlobRange application response header. expect_unsatisfiable: If True, expects 416 Requested Range Not Satisfiable instead of 206 Partial Content. """ blob_key = self.create_blob() environ = {} if test_range_request: environ['HTTP_RANGE'] = blobrange else: environ['HTTP_RANGE'] = 'bytes=2-5' # Should be ignored. headers = [ (blobstore.BLOB_KEY_HEADER, str(blob_key)), ('Content-Type', 'image/jpg'), ('Content-Range', 'bytes 1-2/6'), # Should be ignored. ] if not test_range_request: headers.append((blobstore.BLOB_RANGE_HEADER, blobrange)) state = request_rewriter.RewriterState(environ, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) if expect_unsatisfiable: expected_status = '416 Requested Range Not Satisfiable' else: expected_status = '206 Partial Content' expected_headers = { 'Content-Length': str(len(expected_body)), 'Content-Range': expected_range, } if not expect_unsatisfiable: expected_headers['Content-Type'] = 'image/jpg' expected_allow_large_response = not expect_unsatisfiable self.assertEqual(expected_status, state.status) self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual(expected_body, ''.join(state.body)) self.assertEqual(expected_allow_large_response, state.allow_large_response)
def test_default_content_type(self): """Tests downloads when upload does not specify content-type.""" blob_key = self.create_blob(content_type=None) headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] state = request_rewriter.RewriterState({}, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("200 original message", state.status) expected_headers = {"Content-Length": "6", "Content-Type": cloudstorage_stub._GCS_DEFAULT_CONTENT_TYPE} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual("a blob", "".join(state.body))
def test_rewrite_for_download_missing_blob(self): """Tests downloading a missing blob key.""" environ = {"HTTP_RANGE": "bytes=2-5"} # Should be ignored. headers = [(blobstore.BLOB_KEY_HEADER, "no such blob")] state = request_rewriter.RewriterState(environ, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("500 Internal Server Error", state.status) expected_headers = {"Content-Length": "0"} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual("", "".join(state.body)) self.assertFalse(state.allow_large_response)
def test_rewrite_for_download_missing_blob(self): """Tests downloading a missing blob key.""" environ = {'HTTP_RANGE': 'bytes=2-5'} # Should be ignored. headers = [(blobstore.BLOB_KEY_HEADER, 'no such blob')] state = request_rewriter.RewriterState(environ, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('500 Internal Server Error', state.status) expected_headers = {'Content-Length': '0'} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual('', ''.join(state.body)) self.assertFalse(state.allow_large_response)
def test_rewrite_for_download_not_200(self): """Download requested, but status code is not 200.""" blob_key = self.create_blob() headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] state = request_rewriter.RewriterState({}, "201 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("500 Internal Server Error", state.status) expected_headers = {"Content-Length": "0"} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual("", "".join(state.body)) self.assertFalse(state.allow_large_response)
def test_rewrite_for_download_preserve_user_content_type(self): """Tests that the application's provided Content-Type is preserved.""" blob_key = self.create_blob() headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key)), ("Content-Type", "image/jpg")] state = request_rewriter.RewriterState({}, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("200 original message", state.status) expected_headers = {"Content-Length": "6", "Content-Type": "image/jpg"} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual("a blob", "".join(state.body)) self.assertTrue(state.allow_large_response)
def test_rewrite_for_download_not_200(self): """Download requested, but status code is not 200.""" blob_key = self.create_blob() headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] state = request_rewriter.RewriterState({}, '201 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('500 Internal Server Error', state.status) expected_headers = {'Content-Length': '0'} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual('', ''.join(state.body)) self.assertFalse(state.allow_large_response)
def test_non_download_response(self): """Response is not rewritten if missing download header.""" environ = {"HTTP_RANGE": "bytes=2-5"} # Should be ignored. headers = [(blobstore.BLOB_RANGE_HEADER, "bytes=1-4")] state = request_rewriter.RewriterState(environ, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("200 original message", state.status) # X-AppEngine-BlobRange should not be removed if there is no BlobKey header. self.assertHeadersEqual(headers, state.headers) self.assertEqual("original body", "".join(state.body)) self.assertFalse(state.allow_large_response)
def test_download_range_range_header_empty(self): """Tests that whole blob is downloaded when Range is empty.""" blob_key = self.create_blob() environ = {"HTTP_RANGE": ""} headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key)), ("Content-Type", "image/jpg")] state = request_rewriter.RewriterState(environ, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("200 original message", state.status) expected_headers = {"Content-Length": "6", "Content-Type": "image/jpg"} self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual("a blob", "".join(state.body)) self.assertTrue(state.allow_large_response)
def test_non_download_response(self): """Response is not rewritten if missing download header.""" environ = {'HTTP_RANGE': 'bytes=2-5'} # Should be ignored. headers = [(blobstore.BLOB_RANGE_HEADER, 'bytes=1-4')] state = request_rewriter.RewriterState(environ, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('200 original message', state.status) # X-AppEngine-BlobRange should not be removed if there is no BlobKey header. self.assertHeadersEqual(headers, state.headers) self.assertEqual('original body', ''.join(state.body)) self.assertFalse(state.allow_large_response)
def test_default_content_type(self): """Tests downloads when upload does not specify content-type.""" blob_key = self.create_blob(content_type=None) headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] state = request_rewriter.RewriterState({}, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('200 original message', state.status) expected_headers = { 'Content-Length': '6', 'Content-Type': cloudstorage_stub._GCS_DEFAULT_CONTENT_TYPE, } self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual('a blob', ''.join(state.body))
def test_rewrite_for_download_use_stored_content_type(self, auto_mimetype=False): """Tests that downloads rewrite when using blob's original content-type.""" blob_key = self.create_blob() headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] if auto_mimetype: headers.append(("Content-Type", blob_download._AUTO_MIME_TYPE)) state = request_rewriter.RewriterState({}, "200 original message", headers, "original body") blob_download.blobstore_download_rewriter(state) self.assertEqual("200 original message", state.status) expected_headers = {"Content-Length": "6", "Content-Type": "image/png"} self.assertHeadersEqual(expected_headers, state.headers) # Ensure that Content-Type is a str, not a unicode. self.assertIsInstance(state.headers["Content-Type"], str) self.assertEqual("a blob", "".join(state.body)) self.assertTrue(state.allow_large_response)
def test_rewrite_for_download_preserve_user_content_type(self): """Tests that the application's provided Content-Type is preserved.""" blob_key = self.create_blob() headers = [ (blobstore.BLOB_KEY_HEADER, str(blob_key)), ('Content-Type', 'image/jpg'), ] state = request_rewriter.RewriterState({}, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('200 original message', state.status) expected_headers = { 'Content-Length': '6', 'Content-Type': 'image/jpg', } self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual('a blob', ''.join(state.body)) self.assertTrue(state.allow_large_response)
def test_download_range_range_header_empty(self): """Tests that whole blob is downloaded when Range is empty.""" blob_key = self.create_blob() environ = {'HTTP_RANGE': ''} headers = [ (blobstore.BLOB_KEY_HEADER, str(blob_key)), ('Content-Type', 'image/jpg'), ] state = request_rewriter.RewriterState(environ, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('200 original message', state.status) expected_headers = { 'Content-Length': '6', 'Content-Type': 'image/jpg', } self.assertHeadersEqual(expected_headers, state.headers) self.assertEqual('a blob', ''.join(state.body)) self.assertTrue(state.allow_large_response)
def test_rewrite_for_download_use_stored_content_type( self, auto_mimetype=False): """Tests that downloads rewrite when using blob's original content-type.""" blob_key = self.create_blob() headers = [(blobstore.BLOB_KEY_HEADER, str(blob_key))] if auto_mimetype: headers.append(('Content-Type', blob_download._AUTO_MIME_TYPE)) state = request_rewriter.RewriterState({}, '200 original message', headers, 'original body') blob_download.blobstore_download_rewriter(state) self.assertEqual('200 original message', state.status) expected_headers = { 'Content-Length': '6', 'Content-Type': 'image/png', } self.assertHeadersEqual(expected_headers, state.headers) # Ensure that Content-Type is a str, not a unicode. self.assertIsInstance(state.headers['Content-Type'], str) self.assertEqual('a blob', ''.join(state.body)) self.assertTrue(state.allow_large_response)