Skip to content

Commit

Permalink
Merge pull request #504 from jamieparkinson/development
Browse files Browse the repository at this point in the history
Fix HTTP resolver caching for identifiers containing url-encodable characters
  • Loading branch information
alexwlchan authored May 15, 2020
2 parents 40f4512 + 659cef4 commit b7abd38
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions loris/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def _web_request_url(self, ident):
return (url, self.request_options())

def cache_dir_path(self, ident):
ident = unquote(ident)
return join(
self.cache_root,
CacheNamer.cache_directory_name(ident=ident),
Expand Down
49 changes: 49 additions & 0 deletions tests/resolver_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ def mock_responses():
status=200
)

with open('tests/img/01/04/0001.tif', 'rb') as f:
responses.add(
responses.GET, 'http://sample.sample/01/02/0003',
body=f.read(),
status=200,
content_type='image/tiff'
)

body = (
'II*\x00\x0c\x00\x00\x00\x80\x00 \x0e\x00\x00\x01\x03\x00\x01\x00\x00'
'\x00\x01\x00\x00\x00\x01\x01\x03\x00\x01\x00\x00\x00\x01\x00\x00\x00'
Expand Down Expand Up @@ -309,12 +317,18 @@ def test_simple_http_resolver(self):
self.assertEqual(ii.src_format, 'tif')
self.assertTrue(isfile(ii.src_img_fp))

# Store the number of HTTP requests made after the first resolution
# so that we can check whether one get (erroneously) made rather than
# the cache being used.
calls_after_resolved = len(responses.calls)

# Now we resolve the same identifier a second time, when the cache
# is warm.
ii = self.app.resolver.resolve(self.app, ident, base_uri="")
assert ii.src_img_fp == expected_path
assert ii.src_format == "tif"
assert os.path.isfile(ii.src_img_fp)
assert len(responses.calls) == calls_after_resolved

#Test with a full uri
ident = quote_plus('http://sample.sample/0001')
Expand All @@ -339,6 +353,41 @@ def test_simple_http_resolver(self):
self.assertEqual(ii.src_format, 'tif')
self.assertTrue(isfile(ii.src_img_fp))

# Test with an identifier including url-encodable characters
ident = '01/02/0003'
expected_path = join(self.app.resolver.cache_root, '7e')
expected_path = join(expected_path, 'b5c')
expected_path = join(expected_path, '9cf')
expected_path = join(expected_path, '404')
expected_path = join(expected_path, 'c6d')
expected_path = join(expected_path, 'c34')
expected_path = join(expected_path, 'f5c')
expected_path = join(expected_path, '657')
expected_path = join(expected_path, '9c9')
expected_path = join(expected_path, '016')
expected_path = join(expected_path, '315')
expected_path = join(expected_path, ident) #add identifier to the path
expected_path = join(expected_path, 'loris_cache.tif')

encoded_ident = quote_plus(ident)
ii = self.app.resolver.resolve(self.app, encoded_ident, "")
self.assertEqual(expected_path, ii.src_img_fp)
self.assertEqual(ii.src_format, 'tif')
self.assertTrue(isfile(ii.src_img_fp))

# Store the number of HTTP requests made after the first resolution
# so that we can check whether one get (erroneously) made rather than
# the cache being used.
calls_after_resolved = len(responses.calls)

# Now we resolve the same identifier a second time, when the cache
# is warm.
ii = self.app.resolver.resolve(self.app, encoded_ident, base_uri="")
assert ii.src_img_fp == expected_path
assert ii.src_format == "tif"
assert os.path.isfile(ii.src_img_fp)
assert len(responses.calls) == calls_after_resolved

#Test with a bad identifier
ident = 'DOESNOTEXIST'
self.assertRaises(ResolverException, lambda: self.app.resolver.resolve(self.app, ident, ""))
Expand Down

0 comments on commit b7abd38

Please sign in to comment.