Skip to content

Commit

Permalink
Merge pull request #101 from w3c/handlers; r=jgraham
Browse files Browse the repository at this point in the history
Add some more tests for handlers.
  • Loading branch information
Ms2ger authored Sep 6, 2016
2 parents 1e15a23 + b0c8fa4 commit 5f2235f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
3 changes: 3 additions & 0 deletions tests/functional/docroot/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Oops...
def main(request, response
return "FAIL"
3 changes: 3 additions & 0 deletions tests/functional/docroot/no_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Oops...
def mian(request, response):
return "FAIL"
1 change: 1 addition & 0 deletions tests/functional/docroot/subdir/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am here to ensure that my containing directory exists.
71 changes: 71 additions & 0 deletions tests/functional/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import pytest
import unittest
import urllib2
import uuid
Expand Down Expand Up @@ -95,6 +96,19 @@ def handler(request, response):
self.assertEqual("9", resp.info()["Content-Length"])
self.assertEqual("test data", resp.read())

def test_tuple_1_rv(self):
@wptserve.handlers.handler
def handler(request, response):
return ()

route = ("GET", "/test/test_tuple_1_rv", handler)
self.server.router.register(*route)

with pytest.raises(urllib2.HTTPError) as cm:
self.request(route[1])

assert cm.value.code == 500

def test_tuple_2_rv(self):
@wptserve.handlers.handler
def handler(request, response):
Expand Down Expand Up @@ -133,6 +147,32 @@ def handler(request, response):
self.assertEqual("test-value", resp.info()["test-header"])
self.assertEqual("test data", resp.read())

def test_tuple_4_rv(self):
@wptserve.handlers.handler
def handler(request, response):
return 202, [("test-header", "test-value")], "test data", "garbage"

route = ("GET", "/test/test_tuple_1_rv", handler)
self.server.router.register(*route)

with pytest.raises(urllib2.HTTPError) as cm:
self.request(route[1])

assert cm.value.code == 500

def test_none_rv(self):
@wptserve.handlers.handler
def handler(request, response):
return None

route = ("GET", "/test/test_none_rv", handler)
self.server.router.register(*route)
resp = self.request(route[1])
assert resp.getcode() == 200
assert "Content-Length" not in resp.info()
assert resp.read() == b""


class TestJSONHandler(TestUsingServer):
def test_json_0(self):
@wptserve.handlers.json_handler
Expand Down Expand Up @@ -192,13 +232,44 @@ def test_tuple_3(self):
self.assertEqual("PASS", resp.info()["X-Test"])
self.assertEqual("PASS", resp.read())

def test_no_main(self):
with pytest.raises(urllib2.HTTPError) as cm:
self.request("/no_main.py")

assert cm.value.code == 500

def test_invalid(self):
with pytest.raises(urllib2.HTTPError) as cm:
self.request("/invalid.py")

assert cm.value.code == 500

def test_missing(self):
with pytest.raises(urllib2.HTTPError) as cm:
self.request("/missing.py")

assert cm.value.code == 404


class TestDirectoryHandler(TestUsingServer):
def test_directory(self):
resp = self.request("/")
self.assertEqual(200, resp.getcode())
self.assertEqual("text/html", resp.info()["Content-Type"])
#Add a check that the response is actually sane

def test_subdirectory_trailing_slash(self):
resp = self.request("/subdir/")
assert resp.getcode() == 200
assert resp.info()["Content-Type"] == "text/html"

def test_subdirectory_no_trailing_slash(self):
with pytest.raises(urllib2.HTTPError) as cm:
self.request("/subdir")

assert cm.value.code == 404


class TestAsIsHandler(TestUsingServer):
def test_as_is(self):
resp = self.request("/test.asis")
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ commands =
[flake8]
ignore = E128,E129,E221,E226,E231,E251,E265,E302,E303,E402,E901,F821,F841
max-line-length = 141
exclude=docs,.git,__pycache__,.tox,.eggs,*.egg
exclude=docs,.git,__pycache__,.tox,.eggs,*.egg,tests/functional/docroot/
21 changes: 9 additions & 12 deletions wptserve/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ def __repr__(self):
return "<%s base_path:%s url_base:%s>" % (self.__class__.__name__, self.base_path, self.url_base)

def __call__(self, request, response):
if not request.url_parts.path.endswith("/"):
url_path = request.url_parts.path

if not url_path.endswith("/"):
raise HTTPException(404)

path = filesystem_path(self.base_path, request, self.url_base)

if not os.path.isdir(path):
raise HTTPException(404, "%s is not a directory" % path)
assert os.path.isdir(path)

response.headers = [("Content-Type", "text/html")]
response.content = """<!doctype html>
Expand All @@ -71,19 +72,18 @@ def __call__(self, request, response):
<ul>
%(items)s
</ul>
""" % {"path": cgi.escape(request.url_parts.path),
"items": "\n".join(self.list_items(request, path))} # flake8: noqa
""" % {"path": cgi.escape(url_path),
"items": "\n".join(self.list_items(url_path, path))} # flake8: noqa

def list_items(self, base_path, path):
assert base_path.endswith("/")

def list_items(self, request, path):
# TODO: this won't actually list all routes, only the
# ones that correspond to a real filesystem path. It's
# not possible to list every route that will match
# something, but it should be possible to at least list the
# statically defined ones
base_path = request.url_parts.path

if not base_path.endswith("/"):
base_path += "/"
if base_path != "/":
link = urlparse.urljoin(base_path, "..")
yield ("""<li class="dir"><a href="%(link)s">%(name)s</a></li>""" %
Expand All @@ -99,9 +99,6 @@ def list_items(self, request, path):
{"link": link, "name": cgi.escape(item), "class": class_})


directory_handler = DirectoryHandler()


class FileHandler(object):
def __init__(self, base_path=None, url_base="/"):
self.base_path = base_path
Expand Down

0 comments on commit 5f2235f

Please sign in to comment.