diff --git a/mutalyzer/website.py b/mutalyzer/website.py index fc0cce2d4c92994f189969e0211dfafecc911ead..03e946a009d4f57862e9102551c3e537b4bf63e5 100644 --- a/mutalyzer/website.py +++ b/mutalyzer/website.py @@ -270,6 +270,37 @@ class Reference: web.header('Content-Type', 'text/plain') web.header('Content-Disposition', 'attachment; filename="%s"' % file) return handle.read() + + def HEAD(self, file): + """ + Do the same as in the GET case, but don't actually bunzip and send the + file, just check if it exists. + + @arg file: Filename to download from cache. + @type file: string + + This is used by LOVD to quickly check if a reference file is in the + cache. If it isn't, it will resubmit it. + Of course a more proper solution here would be to have some webservice + method which checks if the GenBank file is in the cache *or* can be + reconstructed from the information in the database. Because if the + latter is the case, Mutalyzer will add it to the cache on the fly. + """ + file_path = os.path.join(config.Retriever.cache, '%s.bz2' % file) + if not os.path.isfile(file_path): + # The following is a hack to return a 404 not found status with + # empty body (as is checked by our unit test framework, WebTest). + # Just passing nothing, or the empty string, causes web.py to + # insert some default 'not found' message. + class TrueEmptyString(object): + def __str__(self): + return '' + def __nonzero__( self): + return True + raise web.notfound(message=TrueEmptyString()) + web.header('Content-Type', 'text/plain') + web.header('Content-Disposition', 'attachment; filename="%s"' % file) + return '' #Reference diff --git a/tests/test_website.py b/tests/test_website.py index a6692495772fb79728a99bef8fdd3f7bb2247b61..1121cadd8b1c8030c7aa7b8efce70a1f71ffda95 100644 --- a/tests/test_website.py +++ b/tests/test_website.py @@ -626,11 +626,6 @@ facilisi.""" def test_reference(self): """ Test if reference files are cached. - - @todo: This test doesn't work, since for every request a new - temporary cache directory is created by the webserver instance - and thus the cached file from request i cannot be re-used in - request i+1. """ r = self.app.get('/check') form = r.forms[0] @@ -645,3 +640,28 @@ facilisi.""" assert_equal(r.content_type, 'text/plain') assert_equal(r.content_length, 26427) r.mustcontain('ggaaaaagtc tctcaaaaaa cctgctttat') + + def test_reference_head(self): + """ + Test if reference files are cached, by issuing a HEAD request. + + Note: The WebTest module also checks that the response to a HEAD + request is empty, as it should be. + """ + r = self.app.get('/check') + form = r.forms[0] + form['mutationName'] = 'AB026906.1:c.274G>T' + r = form.submit() + r.mustcontain('0 Errors', + '1 Warning', + 'Raw variant 1: substitution at 7872', + '<a href="#bottom" class="hornav">go to bottom</a>', + '<input value="AB026906.1:c.274G>T" type="text" name="mutationName" style="width:100%">') + r = self.app.head('/Reference/AB026906.1.gb') + assert_equal(r.content_type, 'text/plain') + + def test_reference_head_none(self): + """ + Test if non-existing reference files gives a 404 on a HEAD request. + """ + r = self.app.head('/Reference/AB026906.78.gb', status=404)