def test_route_unknown(self): """ Routes not a tuple or `Route` raise `TypeError`. """ matcher = MatchesStructure(args=Equals(('Cannot be adapted to a route', 42))) with ExpectedException(TypeError, matcher): route.router(42)
def test_route_names_not_distinct(self): """ Raise `ValueError` if route names are not unique. """ a = route.route(u'/foo', route.GET, [tracer('a')], u'a') b = route.route(u'/bar', route.GET, [tracer('a')], u'a') matcher = MatchesStructure(args=MatchesListwise( [Equals('Non-unique route names'), Equals((a, b))])) with ExpectedException(ValueError, matcher): route.router(a, b)
def test_route_tuple(self): """ Routes can be specified as tuples. """ interceptor = route.router((u'/foo', route.GET, [tracer('a')])) context = empty_context.set(REQUEST, basic_request(uri=u'/foo')) self.assertThat( execute(context, [interceptor]), succeeded(Traced(Equals(v(('enter', 'a'), ('leave', 'a'))))))
def test_route_unmatched(self): """ If no route matches the request path, no ``ROUTE`` value is set on the context, and no route interceptors are enqueued. """ interceptor = route.router( route.route(u'/foo', route.GET, [tracer('a')])) context = empty_context.set(REQUEST, basic_request(uri=u'/bar')) self.assertThat( execute(context, [interceptor]), succeeded(MatchesAll(Equals(context), Not(Contains(ROUTE)))))
def test_path_specificity(self): """ Ensure that more specific routes have a higher priority than less specific routes. """ interceptor = route.router( route.route(u'/bar', route.GET, [tracer('b')], u'b'), route.route(u'/bar/:id/*rest', route.GET, [tracer('d')], u'd'), route.route(u'/foo', route.GET, [tracer('a')], u'a'), route.route(u'/bar/:id', route.GET, [tracer('c')], u'c')) req = lambda uri: execute( empty_context.set(REQUEST, basic_request(uri=uri)), [interceptor]) self.assertThat( req(u'/foo'), succeeded(Traced(Equals(v(('enter', 'a'), ('leave', 'a')))))) self.assertThat( req(u'/bar'), succeeded(Traced(Equals(v(('enter', 'b'), ('leave', 'b')))))) self.assertThat( req(u'/bar/1'), succeeded(Traced(Equals(v(('enter', 'c'), ('leave', 'c')))))) self.assertThat( req(u'/bar/1/pa/th'), succeeded(Traced(Equals(v(('enter', 'd'), ('leave', 'd'))))))
from pyrsistent import m from fugue.interceptors.http import route from fugue.interceptors.http.route import GET from fugue.adapters.twisted import twisted_adapter_resource # Define a helper to construct HTTP 200 responses. def ok(body): return m(status=200, body=body.encode('utf-8')) # Define the handler behaviour. def greet(request): name = request['path_params']['name'] return ok(u'Hello, {}!'.format(name)) # Declare the route. interceptor = route.router((u'/greet/:name', GET, greet)) # Create a Twisted Web resource that will execute the interceptor chain. resource = twisted_adapter_resource([interceptor]) # Run the script from a Fugue checkout: # twistd -n web --resource-script=examples/twisted_greet.py