Esempio n. 1
0
def test_mod_response():
    router = Router()
    route1a = router.get("https://foo.bar") % 404
    route1b = router.get("https://foo.bar") % dict(status_code=201)
    route2 = router.get("https://ham.spam/egg/") % MockResponse(202)
    route3 = router.post("https://fox.zoo/") % httpx.Response(
        401, json={"error": "x"})

    request = httpx.Request("GET", "https://foo.bar")
    matched_route, response = router.match(request)
    assert response.status_code == 404
    assert matched_route is route1a

    request = httpx.Request("GET", "https://foo.bar")
    matched_route, response = router.match(request)
    assert response.status_code == 201
    assert matched_route is route1b
    assert route1a is route1b

    request = httpx.Request("GET", "https://ham.spam/egg/")
    matched_route, response = router.match(request)
    assert response.status_code == 202
    assert matched_route is route2

    request = httpx.Request("POST", "https://fox.zoo/")
    matched_route, response = router.match(request)
    assert response.status_code == 401
    assert response.json() == {"error": "x"}
    assert matched_route is route3
Esempio n. 2
0
def test_mod_response():
    router = Router()
    route1a = router.get("https://foo.bar/baz/") % 409
    route1b = router.get("https://foo.bar/baz/") % 404
    route2 = router.get("https://foo.bar") % dict(status_code=201)
    route3 = router.post("https://fox.zoo/") % httpx.Response(
        401, json={"error": "x"})

    request = httpx.Request("GET", "https://foo.bar/baz/")
    matched_route, response = router.match(request)
    assert response.status_code == 404
    assert matched_route is route1b
    assert route1a is route1b

    request = httpx.Request("GET", "https://foo.bar/")
    matched_route, response = router.match(request)
    assert response.status_code == 201
    assert matched_route is route2

    request = httpx.Request("POST", "https://fox.zoo/")
    matched_route, response = router.match(request)
    assert response.status_code == 401
    assert response.json() == {"error": "x"}
    assert matched_route is route3

    with pytest.raises(ValueError, match="Route can only"):
        router.route() % []
Esempio n. 3
0
def test_pass_through():
    router = Router(assert_all_mocked=False)
    route = router.route(method="GET").pass_through()

    request = httpx.Request("GET", "https://foo.bar/baz/")
    matched_route, response = router.match(request)

    assert matched_route is route
    assert matched_route.is_pass_through
    assert response is request

    route.pass_through(False)
    matched_route, response = router.match(request)

    assert matched_route is route
    assert not matched_route.is_pass_through
    assert response is not None
Esempio n. 4
0
def test_pass_through():
    router = Router(assert_all_mocked=False)
    route = router.get("https://foo.bar/", path="/baz/").pass_through()

    request = httpx.Request("GET", "https://foo.bar/baz/")
    matched_route, response = router.match(request)

    assert matched_route is route
    assert matched_route.is_pass_through
    assert response is request

    with pytest.raises(PassThrough):
        router.handler(request)

    route.pass_through(False)
    matched_route, response = router.match(request)

    assert matched_route is route
    assert not matched_route.is_pass_through
    assert response is not None
Esempio n. 5
0
def test_base_url(url, expected):
    router = Router(base_url="https://foo.bar/", assert_all_mocked=False)
    route = router.route(method="GET", url="/baz/").respond(201)

    request = httpx.Request("GET", url)
    matched_route, response = router.match(request)

    assert bool(matched_route is route) is expected
    if expected:
        assert bool(response.status_code == 201) is expected
    else:
        assert not response

    response = router.resolve(request)
    assert bool(response.status_code == 201) is expected
Esempio n. 6
0
def test_match_and_resolve(args, kwargs, expected):
    router = Router(assert_all_mocked=False)
    route = router.route(*args, **kwargs).respond(status_code=201)

    request = httpx.Request("GET",
                            "https://foo.bar/baz/",
                            cookies={
                                "foo": "bar",
                                "ham": "spam"
                            })
    matched_route, response = router.match(request)

    assert bool(matched_route is route) is expected
    if expected:
        assert bool(response.status_code == 201) is expected
    else:
        assert not response

    response = router.resolve(request)
    assert bool(response.status_code == 201) is expected