Beispiel #1
0
 def test23_IIIFHandler_file(self):
     """Test IIIFHandler.file property."""
     # No auth
     c = Config()
     c.api_version = '2.1'
     # Generator
     c.klass_name = 'gen'
     c.generator_dir = os.path.join(os.path.dirname(__file__),
                                    '../iiif/generators')
     i = IIIFHandler(prefix='/p',
                     identifier='sierpinski_carpet',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     self.assertEqual(os.path.basename(i.file), 'sierpinski_carpet.py')
     # Image
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     i = IIIFHandler(prefix='/p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     self.assertEqual(os.path.basename(i.file), 'starfish.jpg')
     # Failure
     i = IIIFHandler(prefix='/p',
                     identifier='no-image',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     self.assertRaises(IIIFError, lambda: i.file)
Beispiel #2
0
 def test22_IIIFHandler_json_mime_type(self):
     """Test IIIFHandler.json_mime_type property."""
     # No auth, no test for API 1.0
     c = Config()
     c.api_version = '1.0'
     i = IIIFHandler(prefix='/p',
                     identifier='i',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     self.assertEqual(i.json_mime_type, "application/json")
     # No auth, connecg for API 1.1
     c = Config()
     c.api_version = '1.1'
     i = IIIFHandler(prefix='/p',
                     identifier='i',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         self.assertEqual(i.json_mime_type, "application/json")
     environ['HTTP_ACCEPT'] = 'application/ld+json'
     with self.test_app.request_context(environ):
         self.assertEqual(i.json_mime_type, "application/ld+json")
     environ['HTTP_ACCEPT'] = 'text/plain'
     with self.test_app.request_context(environ):
         self.assertEqual(i.json_mime_type, "application/json")
Beispiel #3
0
 def test26_IIIFHandler_image_request_response(self):
     """Test IIIFHandler.image_request_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         # request too long
         self.assertRaises(IIIFError, i.image_request_response, 'x' * 2000)
         # bad path
         self.assertRaises(IIIFError, i.image_request_response, 'a/b')
         self.assertRaises(IIIFError, i.image_request_response, '/')
         self.assertRaises(IIIFError, i.image_request_response, 'starfish')
         # normal
         resp = i.image_request_response('full/full/0/default')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertEqual(len(resp.data), 3523302)
     # PIL manipularor and degraded
     i = IIIFHandler(prefix='p',
                     identifier='starfish-deg',
                     config=c,
                     klass=IIIFManipulatorPIL,
                     auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.image_request_response('full/full/0/default.jpg')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertTrue(len(resp.data) > 1000000)
         self.assertTrue(len(resp.data) < 2000000)
     # Conneg for v1.1
     c.api_version = '1.1'
     i = IIIFHandler(prefix='p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulatorPIL,
                     auth=None)
     environ = WSGI_ENVIRON()
     environ['HTTP_ACCEPT'] = 'image/png'
     with self.test_app.request_context(environ):
         resp = i.image_request_response('full/full/0/native')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertTrue(len(resp.data) > 1000000)
         self.assertEqual(resp.mimetype, 'image/png')
Beispiel #4
0
 def test26_IIIFHandler_image_information_response(self):
     """Test IIIFHandler.image_information_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulator,
                     auth=IIIFAuthBasic())
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertIn(b'default', jsonb)
         self.assertNotIn(b'native', jsonb)
         self.assertIn(b'starfish', jsonb)
         self.assertIn(b'scaleFactors', jsonb)
         self.assertIn(b'login', jsonb)
     # v1.1, auto scale factors
     c.api_version = '1.1'
     c.scale_factors = ['auto']
     i = IIIFHandler(prefix='p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertNotIn(b'default', jsonb)
         self.assertIn(b'native', jsonb)
         self.assertIn(b'starfish', jsonb)
         self.assertNotIn(b'scaleFactors', jsonb)
     # degraded
     c.api_version = '2.1'
     i = IIIFHandler(prefix='p',
                     identifier='starfish-deg',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertIn(b'starfish-deg', jsonb)
Beispiel #5
0
 def test27_IIIFHandler_error_response(self):
     """Test IIIFHandler.error_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p', identifier='starfish', config=c,
                     klass=IIIFManipulator, auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.error_response(IIIFError(999, 'bwaa'))
         self.assertEqual(resp.status_code, 999)
Beispiel #6
0
 def test25_IIIFHandler_make_response(self):
     """Test IIIFHandler.make_response."""
     c = Config()
     c.api_version = '2.1'
     i = IIIFHandler(prefix='/p', identifier='iii', config=c,
                     klass=IIIFManipulator, auth=None)
     with self.test_app.app_context():
         resp = i.make_response('hello1')
         self.assertEqual(resp.response[0], b'hello1')
         self.assertEqual(resp.headers['Access-control-allow-origin'], '*')
     # Add a custom header
     i = IIIFHandler(prefix='/p', identifier='iii', config=c,
                     klass=IIIFManipulator, auth=None)
     with self.test_app.app_context():
         resp = i.make_response('hello2', headers={'Special-header': 'ba'})
         self.assertEqual(resp.response[0], b'hello2')
         self.assertEqual(resp.headers['Special-header'], 'ba')
         self.assertEqual(resp.headers['Access-control-allow-origin'], '*')
Beispiel #7
0
 def test27_IIIFHandler_error_response(self):
     """Test IIIFHandler.error_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p',
                     identifier='starfish',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.error_response(IIIFError(999, 'bwaa'))
         self.assertEqual(resp.status_code, 999)
Beispiel #8
0
 def test21_IIIFHandler_init(self):
     """Test IIIFHandler class init."""
     # No auth
     c = Config()
     c.api_version = '2.1'
     i = IIIFHandler(prefix='/p',
                     identifier='i',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     self.assertTrue(i.manipulator.api_version, '2.1')
     # Basic auth
     a = IIIFAuthBasic()
     c.host = 'example.org'
     c.port = 80
     c.prefix = '/p'
     i = IIIFHandler(prefix='/p',
                     identifier='i',
                     config=c,
                     klass=IIIFManipulator,
                     auth=a)
     self.assertTrue(i.manipulator.api_version, '2.1')
Beispiel #9
0
 def test24_IIIFHandler_add_compliance_header(self):
     """Test IIIFHandler.add_compliance_header property."""
     # No auth
     c = Config()
     c.api_version = '2.1'
     i = IIIFHandler(prefix='/p',
                     identifier='iii',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     i.add_compliance_header()
     self.assertNotIn('Link', i.headers)
     i = IIIFHandler(prefix='/p',
                     identifier='iii',
                     config=c,
                     klass=IIIFManipulatorPIL,
                     auth=None)
     i.add_compliance_header()
     self.assertIn('/level2', i.headers['Link'])
Beispiel #10
0
 def test24_IIIFHandler_add_compliance_header(self):
     """Test IIIFHandler.add_compliance_header property."""
     # No auth
     c = Config()
     c.api_version = '2.1'
     i = IIIFHandler(prefix='/p', identifier='iii', config=c,
                     klass=IIIFManipulator, auth=None)
     i.add_compliance_header()
     self.assertNotIn('Link', i.headers)
     i = IIIFHandler(prefix='/p', identifier='iii', config=c,
                     klass=IIIFManipulatorPIL, auth=None)
     i.add_compliance_header()
     self.assertIn('/level2', i.headers['Link'])
Beispiel #11
0
 def test26_IIIFHandler_image_request_response(self):
     """Test IIIFHandler.image_request_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p', identifier='starfish', config=c,
                     klass=IIIFManipulator, auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         # request too long
         self.assertRaises(IIIFError, i.image_request_response, 'x' * 2000)
         # bad path
         self.assertRaises(IIIFError, i.image_request_response, 'a/b')
         self.assertRaises(IIIFError, i.image_request_response, '/')
         self.assertRaises(IIIFError, i.image_request_response, 'starfish')
         # normal
         resp = i.image_request_response('full/full/0/default')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertEqual(len(resp.data), 3523302)
     # PIL manipularor and degraded
     i = IIIFHandler(prefix='p', identifier='starfish-deg', config=c,
                     klass=IIIFManipulatorPIL, auth=None)
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.image_request_response('full/full/0/default.jpg')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertTrue(len(resp.data) > 1000000)
         self.assertTrue(len(resp.data) < 2000000)
     # Conneg for v1.1
     c.api_version = '1.1'
     i = IIIFHandler(prefix='p', identifier='starfish', config=c,
                     klass=IIIFManipulatorPIL, auth=None)
     environ = WSGI_ENVIRON()
     environ['HTTP_ACCEPT'] = 'image/png'
     with self.test_app.request_context(environ):
         resp = i.image_request_response('full/full/0/native')
         resp.direct_passthrough = False  # avoid Flask complaint when reading .data
         self.assertTrue(len(resp.data) > 1000000)
         self.assertEqual(resp.mimetype, 'image/png')
Beispiel #12
0
 def test26_IIIFHandler_image_information_response(self):
     """Test IIIFHandler.image_information_response()."""
     c = Config()
     c.api_version = '2.1'
     c.klass_name = 'dummy'
     c.image_dir = os.path.join(os.path.dirname(__file__), '../testimages')
     c.tile_height = 512
     c.tile_width = 512
     c.scale_factors = [1, 2]
     c.host = 'example.org'
     c.port = 80
     i = IIIFHandler(prefix='p', identifier='starfish', config=c,
                     klass=IIIFManipulator, auth=IIIFAuthBasic())
     environ = WSGI_ENVIRON()
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertIn(b'default', jsonb)
         self.assertNotIn(b'native', jsonb)
         self.assertIn(b'starfish', jsonb)
         self.assertIn(b'scaleFactors', jsonb)
         self.assertIn(b'login', jsonb)
     # v1.1, auto scale factors
     c.api_version = '1.1'
     c.scale_factors = ['auto']
     i = IIIFHandler(prefix='p', identifier='starfish', config=c,
                     klass=IIIFManipulator, auth=None)
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertNotIn(b'default', jsonb)
         self.assertIn(b'native', jsonb)
         self.assertIn(b'starfish', jsonb)
         self.assertNotIn(b'scaleFactors', jsonb)
     # degraded
     c.api_version = '2.1'
     i = IIIFHandler(prefix='p', identifier='starfish-deg', config=c,
                     klass=IIIFManipulator, auth=None)
     with self.test_app.request_context(environ):
         resp = i.image_information_response()
         jsonb = resp.response[0]
         self.assertIn(b'starfish-deg', jsonb)
Beispiel #13
0
 def test25_IIIFHandler_make_response(self):
     """Test IIIFHandler.make_response."""
     c = Config()
     c.api_version = '2.1'
     i = IIIFHandler(prefix='/p',
                     identifier='iii',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     with self.test_app.app_context():
         resp = i.make_response('hello1')
         self.assertEqual(resp.response[0], b'hello1')
         self.assertEqual(resp.headers['Access-control-allow-origin'], '*')
     # Add a custom header
     i = IIIFHandler(prefix='/p',
                     identifier='iii',
                     config=c,
                     klass=IIIFManipulator,
                     auth=None)
     with self.test_app.app_context():
         resp = i.make_response('hello2', headers={'Special-header': 'ba'})
         self.assertEqual(resp.response[0], b'hello2')
         self.assertEqual(resp.headers['Special-header'], 'ba')
         self.assertEqual(resp.headers['Access-control-allow-origin'], '*')