Browse Source

Improve test coverage for CORS_allow

tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
ea051c85b3
2 changed files with 19 additions and 3 deletions
  1. +3
    -3
      nilmdb/server/serverutil.py
  2. +16
    -0
      tests/test_misc.py

+ 3
- 3
nilmdb/server/serverutil.py View File

@@ -79,12 +79,12 @@ def CORS_allow(methods):
request = cherrypy.request.headers
response = cherrypy.response.headers

if not isinstance(methods, (tuple, list)): # pragma: no cover
if not isinstance(methods, (tuple, list)):
methods = [ methods ]
methods = [ m.upper() for m in methods if m ]
if not methods: # pragma: no cover
if not methods:
methods = [ 'GET', 'HEAD' ]
elif 'GET' in methods and 'HEAD' not in methods: # pragma: no cover
elif 'GET' in methods and 'HEAD' not in methods:
methods.append('HEAD')
response['Allow'] = ', '.join(methods)



+ 16
- 0
tests/test_misc.py View File

@@ -94,3 +94,19 @@ class TestMisc(object):
with assert_raises(OSError):
nilmdb.utils.diskusage.du("/dev/null/bogus")
nilmdb.utils.diskusage.du("super-bogus-does-not-exist")

def tests_cors_allow(self):
# Just to get some test coverage; these code paths aren't actually
# used in current code
cpy = nilmdb.server.serverutil.cherrypy
(req, resp) = (cpy.request, cpy.response)
cpy.request.method = "DELETE"
with assert_raises(cpy.HTTPError):
nilmdb.server.serverutil.CORS_allow(methods="POST")
with assert_raises(cpy.HTTPError):
nilmdb.server.serverutil.CORS_allow(methods=["POST"])
with assert_raises(cpy.HTTPError):
nilmdb.server.serverutil.CORS_allow(methods=["GET"])
with assert_raises(cpy.HTTPError):
nilmdb.server.serverutil.CORS_allow(methods=[])
(cpy.request, cpy.response) = (req, resp)

Loading…
Cancel
Save