Skip to content

Commit

Permalink
Merge branch 'gauntlet_to_dispatcher_2'
Browse files Browse the repository at this point in the history
  • Loading branch information
pjz committed Dec 6, 2012
2 parents 579aaee + 572ca52 commit 75e0a01
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
2 changes: 0 additions & 2 deletions aspen/gauntlet.py → aspen/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,3 @@ def dispatch(request, pure_dispatch=False):
if not request.fs.startswith(startdir):
raise Response(404)


run = run_through = dispatch
3 changes: 3 additions & 0 deletions aspen/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def _set_cloexec():

execute = _do_execv

def clear_changes():
global extras
extras = set()

def if_changes(filename):
extras.add(filename)
Expand Down
2 changes: 1 addition & 1 deletion aspen/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if aspen.logging.LOGGING_THRESHOLD == -1:
# Suppress aspen's logging during tests.
aspen.logging.LOGGING_THRESHOLD = 3
from aspen import resources, Response
from aspen import resources
from aspen.http.request import Request
from aspen.website import Website
from aspen.testing.fsfix import fix, attach_teardown, FSFIX, mk, teardown
Expand Down
3 changes: 3 additions & 0 deletions aspen/testing/fsfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def teardown():
- remove '.aspen' from sys.path
- remove 'foo' from sys.modules
- clear out sys.path_importer_cache
- clear out execution.extras
"""
os.chdir(CWD)
Expand All @@ -89,6 +90,8 @@ def teardown():
sys.path = sys.path[1:]
if 'foo' in sys.modules:
del sys.modules['foo']
import aspen.execution
aspen.execution.clear_changes()

teardown() # start clean

Expand Down
4 changes: 2 additions & 2 deletions aspen/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from os.path import join, isfile

import aspen
from aspen import gauntlet, resources, sockets
from aspen import dispatcher, resources, sockets
from aspen.http.request import Request
from aspen.http.response import Response
from aspen.configuration import Configurable
Expand Down Expand Up @@ -84,7 +84,7 @@ def run_inbound(self, request):
"""
self.hooks.inbound_early.run(request)
self.check_auth(request)
gauntlet.run(request) # sets request.fs
dispatcher.dispatch(request) # sets request.fs
request.socket = sockets.get(request)
self.hooks.inbound_late.run(request)

Expand Down
41 changes: 17 additions & 24 deletions tests/test_gauntlet.py → tests/test_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from aspen import gauntlet, Response
from aspen import dispatcher, Response
from aspen.http.request import Request
from aspen.testing import assert_raises, handle, NoException, StubRequest
from aspen.testing import attach_teardown, fix, mk
Expand All @@ -15,10 +15,10 @@ def assert_raises_404(func, *args):
# =======

def check_index(path, *a):
"""Given a uripath, return a filesystem path per gauntlet.index.
"""Given a uripath, return a filesystem path per dispatcher
"""
request = StubRequest.from_fs(path, *a)
gauntlet.run_through(request)
dispatcher.dispatch(request)
return request

def test_index_is_found():
Expand Down Expand Up @@ -108,10 +108,10 @@ def test_configure_aspen_py_setting_works_with_only_comma():
# =======================

def check_indirect_negotiation(path):
"""Given an urlpath, return a filesystem path per gauntlet.indirect_negotiation.
"""Given an urlpath, return a filesystem path per dispatcher.dispatch
"""
request = StubRequest.from_fs(path)
gauntlet.run_through(request)
dispatcher.dispatch(request)
return request

def test_indirect_negotiation_can_passthrough_renderered():
Expand Down Expand Up @@ -171,10 +171,10 @@ def test_indirect_negotation_doesnt_do_dirs():
# =============

def check_virtual_paths(path):
"""Given an urlpath, return a filesystem path per gauntlet.virtual_paths.
"""Given an urlpath, return a filesystem path per dispatcher.dispatch
"""
request = StubRequest.from_fs(path)
gauntlet.run_through(request)
dispatcher.dispatch(request)
return request

def test_virtual_path_can_passthrough():
Expand Down Expand Up @@ -308,18 +308,11 @@ def test_virtual_path_and_indirect_neg_ext():
# ==============

def check_trailing_slash(path):
"""Given an urlpath, return a filesystem path per trailing slash logic.
We used to have a single function for this in the gauntlet, but that wasn't
sufficient, and the logic is now spread through a couple functions. It
should be done by virtual_paths, however.
"""
request = StubRequest.from_fs(path)
gauntlet.run_through(request)
dispatcher.dispatch(request)
return request

def test_gauntlet_passes_through_files():
def test_dispatcher_passes_through_files():
mk(('foo/index.html', "Greetings, program!"))
expected = fix('/foo/537.html')
assert_raises_404(check_trailing_slash, '/foo/537.html')
Expand All @@ -330,20 +323,20 @@ def test_trailing_slash_passes_dirs_with_slash_through():
actual = check_trailing_slash('/foo/').fs
assert actual == expected, actual

def test_gauntlet_passes_through_virtual_dir_with_trailing_slash():
def test_dispatcher_passes_through_virtual_dir_with_trailing_slash():
mk(('%foo/index.html', "Greetings, program!"))
expected = fix('/%foo/index.html')
actual = check_trailing_slash('/foo/').fs
assert actual == expected, actual + " isn't " + expected

def test_gauntlet_redirects_dir_without_trailing_slash():
def test_dispatcher_redirects_dir_without_trailing_slash():
mk('foo')
response = assert_raises(Response, check_trailing_slash, '/foo')
expected = (301, '/foo/')
actual = (response.code, response.headers['Location'])
assert actual == expected, actual

def test_gauntlet_redirects_virtual_dir_without_trailing_slash():
def test_dispatcher_redirects_virtual_dir_without_trailing_slash():
mk('%foo')
response = assert_raises(Response, check_trailing_slash, '/foo')
expected = (301, '/foo/')
Expand Down Expand Up @@ -427,19 +420,19 @@ def test_virtual_path_docs_6():

def test_intercept_socket_protects_direct_access():
request = Request(uri="/foo.sock")
assert_raises(Response, gauntlet.intercept_socket, request)
assert_raises(Response, dispatcher.dispatch, request)

def test_intercept_socket_intercepts_handshake():
request = Request(uri="/foo.sock/1")
gauntlet.intercept_socket(request)
dispatcher.intercept_socket(request)

expected = ('/foo.sock', '1')
actual = (request.line.uri.path.decoded, request.socket)
assert actual == expected, actual

def test_intercept_socket_intercepts_transported():
request = Request(uri="/foo.sock/1/websocket/46327hfjew3?foo=bar")
gauntlet.intercept_socket(request)
dispatcher.intercept_socket(request)

expected = ('/foo.sock', '1/websocket/46327hfjew3')
actual = (request.line.uri.path.decoded, request.socket)
Expand Down Expand Up @@ -505,15 +498,15 @@ def test_file_with_no_extension_matches():
def test_aspen_favicon_doesnt_get_clobbered_by_virtual_path():
mk('%value')
request = StubRequest.from_fs('/favicon.ico')
gauntlet.run_through(request)
dispatcher.dispatch(request)
expected = {}
actual = request.line.uri.path
assert actual == expected, actual

def test_robots_txt_also_shouldnt_be_redirected():
mk('%value')
request = StubRequest.from_fs('/robots.txt')
err = assert_raises(Response, gauntlet.run_through, request)
err = assert_raises(Response, dispatcher.dispatch, request)
actual = err.code
assert actual == 404, actual

Expand Down
2 changes: 1 addition & 1 deletion tests/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_startup_basically_works():
execution.install(website)
expected = set()
actual = execution.extras
assert actual == expected, actual
assert actual == expected, repr(actual) + " instead of " + repr(expected)


attach_teardown(globals())

0 comments on commit 75e0a01

Please sign in to comment.