예제 #1
0
 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)
예제 #2
0
 def test_after(self):
     """
     Interceptor only has a leave function.
     """
     interceptors = [tracer('a'), after(tracing('leave', 'b')), tracer('c')]
     self.assertThat(
         execute(empty_context, interceptors),
         succeeded(
             Traced(
                 Equals(
                     v(('enter', 'a'), ('enter', 'c'), ('leave', 'c'),
                       ('leave', 'b'), ('leave', 'a'))))))
예제 #3
0
 def test_utf8(self):
     """
     Byte paths are decoded as UTF-8, as a convenience.
     """
     interceptor = tracer('a')
     path = u'/\N{SNOWMAN}'
     self.assertThat(
         route.route(path.encode('utf-8'), route.GET, interceptor),
         MatchesStructure(path=Equals(path)))
예제 #4
0
 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'))))))
예제 #5
0
 def test_match_path_mixed(self):
     """
     Match a path that mixes wildcards and identifiers.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo/:a/*rest', route.GET, [tracer('a')])))
     self.assertThat(
         router.find_route(basic_request(uri=u'/foo/1/2/bar')),
         MatchesStructure.byEquality(path_params=m(a=u'1', rest=u'2/bar')))
예제 #6
0
 def test_match_path_wildcard(self):
     """
     Match a path with wildcards.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo/*rest', route.GET, [tracer('a')])))
     self.assertThat(
         router.find_route(basic_request(uri=u'/foo/1/2/bar')),
         MatchesStructure.byEquality(path_params=m(rest=u'1/2/bar')))
예제 #7
0
 def test_bare_interceptor(self):
     """
     Bare interceptors are placed into a pvector. The interceptor name
     becomes the route name.
     """
     interceptor = tracer('a')
     self.assertThat(
         route.route(u'/foo', route.GET, interceptor),
         MatchesStructure(name=Equals(interceptor.name),
                          interceptors=Equals(v(interceptor))))
예제 #8
0
 def test_unrooted(self):
     """
     Routes with paths that are not rooted raise `ValueError`.
     """
     matcher = MatchesStructure(args=MatchesListwise([
         Equals('Route must be a rooted path'),
         Equals(URL.from_text(u'foo'))
     ]))
     with ExpectedException(ValueError, matcher):
         route.route(u'foo', route.GET, [tracer('a')])
예제 #9
0
 def test_match_path_identifiers(self):
     """
     Match a path with identifiers. Identifier values are stored in
     ``path_params``.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo/:a/:b', route.GET, [tracer('a')])))
     self.assertThat(
         router.find_route(basic_request(uri=u'/foo/1/2')),
         MatchesStructure.byEquality(path_params=m(a=u'1', b=u'2')))
예제 #10
0
 def test_match_path_basic(self):
     """
     Match a basic path with no identifiers or wildcards.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo', route.GET, [tracer('a')])))
     self.assertThat(router.find_route(basic_request(uri=u'/foo')),
                     Not(Is(None)))
     self.assertThat(router.find_route(basic_request(uri=u'/bar')),
                     Is(None))
예제 #11
0
 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)))))
예제 #12
0
 def test_match_request_method_any(self):
     """
     Any request method matches the ``ANY`` method.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo', route.ANY, [tracer('a')])))
     self.assertThat(router.find_route(basic_request(uri=u'/foo')),
                     Not(Is(None)))
     self.assertThat(
         router.find_route(basic_request(uri=u'/foo', method=route.POST)),
         Not(Is(None)))
예제 #13
0
 def test_match_request_method(self):
     """
     Match the request method.
     """
     router = route.LinearSearchRouter(
         v(route.route(u'/foo', route.GET, [tracer('a')])))
     self.assertThat(router.find_route(basic_request(uri=u'/foo')),
                     Not(Is(None)))
     self.assertThat(
         router.find_route(basic_request(uri=u'/foo', method=route.POST)),
         Is(None))
예제 #14
0
    def test_error_handler(self):
        """
        Interceptor only has an error function.
        """
        def _swallow(marker):
            return lambda context, error: tracing('error', (
                marker, 'from', error.failure.value.source))(context)

        interceptors = [
            tracer('a'),
            error_handler(_swallow('b')),
            thrower('c'),
            tracer('d')
        ]
        self.assertThat(
            execute(empty_context, interceptors),
            succeeded(
                Traced(
                    Equals(
                        v(('enter', 'a'), ('error', ('b', 'from', 'c')),
                          ('leave', 'a'))))))
예제 #15
0
 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'))))))
예제 #16
0
    def test_iterable_interceptors(self):
        """
        Iterables have each item processed and wrapped, if necessary. The last
        interceptor / function's name becomes the route name.
        """
        def func(_):
            return 42

        trace = tracer('a')
        self.assertThat(
            route.route(u'/foo', route.GET, [trace, func]),
            MatchesStructure(name=Equals('func'),
                             interceptors=MatchesListwise([
                                 Equals(trace),
                                 EnterStage(
                                     ContainsDict({RESPONSE: Equals(42)}))
                             ])))