Exemple #1
0
 def test_param_must_have_valid_name(self):
     for invalid_pattern in [
         "{}",  # no arg_length
         "{.}",  # no period arg name
     ]:
         with self.assertRaises(AssertionError):
             bracket_route_matcher(invalid_pattern)
Exemple #2
0
    def test_response_is_correct(self, test_scope, test_asgi_request,
                                 test_response):
        endpoint_path = "/"
        test_scope["path"] = endpoint_path  # have to make sure the view is hit

        request_named_tuple = asgi_to_http_request(test_asgi_request["body"],
                                                   test_scope)

        result_target = []

        async def middleware_func(view_func: HttpRequestResponseView,
                                  request: HttpRequest) -> HttpResponse:
            self.assertEqual(request, request_named_tuple)
            result_target.append("middleware was run")
            response = await view_func(request)

            result_target.append(test_response)
            return response

        async def view(request: HttpRequest) -> HttpResponse:
            self.assertEqual(request, request_named_tuple)
            return test_response

        _, endpoint = route_http(
            HttpApp(
                routes=((bracket_route_matcher(endpoint_path), view), ),
                middleware=(middleware_func, ),
            ),
            test_scope,
        )

        call_http_endpoint(endpoint, [test_asgi_request])

        self.assertEqual(result_target, ["middleware was run", test_response])
Exemple #3
0
    def test_works_with_brackets(self):
        url_pattern = "/some/{name}/and/{city}/and{_state_thing}"
        matcher = bracket_route_matcher(url_pattern)

        self.assertEqual(
            matcher("/some/keith/and/jackson/andMI1.chag---an"),
            {"name": "keith", "city": "jackson", "_state_thing": "MI1.chag---an"},
        )
Exemple #4
0
    def test_two_and_three_tuple_routes(self, test_response_1,
                                        test_response_2):
        def view_func_1(request: HttpRequest) -> HttpResponse:
            return test_response_1

        def view_func_2(request: HttpRequest) -> HttpResponse:
            return test_response_2

        normalized_routes = normalize_routes([
            ("/path1", {
                "GET": view_func_1
            }),
            ("/path2", {
                "POST": view_func_2,
                "PUT": view_func_1
            }),
            (regex_route_matcher(re.compile(r"^\d+$")), view_func_1),
            (regex_route_matcher(r"^something(?P<year>\d+)$"), {}),
            ("/name/{name}", view_func_2),
            (bracket_route_matcher("/other/{month}"), {"GET",
                                                       "POST"}, view_func_1),
        ])

        for route in normalized_routes:
            assert len(route) == 2
            assert isinstance(route[0], Callable)
            assert isinstance(route[1], (dict, Callable))

        route_1, route_2, route_3, route_4, route_5, route_6 = normalized_routes

        self.assertEqual(route_1[0]("/path1"), {})
        self.assertEqual(route_1[0]("/path12"), None)
        self.assertEqual(route_2[0]("/path2"), {})
        self.assertEqual(route_3[0]("1293123"), {})
        self.assertEqual(route_4[0]("something3203"), {"year": "3203"})
        self.assertEqual(route_5[0]("/name/alice"), {"name": "alice"})
        self.assertEqual(route_6[0]("/other/march"), {"month": "march"})
Exemple #5
0
    def test_end_must_match(self):
        matcher = bracket_route_matcher("/")

        self.assertIsInstance(matcher("/"), dict)
        self.assertIsNone(matcher("/a"))
Exemple #6
0
 def test_does_not_allow_duplicate_names(self):
     with self.assertRaises(AssertionError):
         bracket_route_matcher("/{name}/{name}")
Exemple #7
0
    def test_works_without_brackets(self):
        test_str = "/something"
        matcher = bracket_route_matcher(test_str)

        self.assertEqual({}, matcher(test_str))
Exemple #8
0
def ws_app(routes: Iterable[WSProtoRoute]) -> WSApp:
    return tuple((bracket_route_matcher(matcher),
                  view) if isinstance(matcher, str) else (matcher, view)
                 for matcher, view in routes)
Exemple #9
0
def normalize_route_matcher(matcher: MatcherOrStr) -> RouteMatcher:
    return bracket_route_matcher(matcher) if isinstance(matcher, str) else matcher