Esempio n. 1
0
def test_api_request_by_id_not_exists(httpbin):
    def _test(httpbin):
        requests.get(httpbin.url + "/get/abc")
        requests.get(httpbin.url + "/get/def")

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/request/999")
    assert ret.status_code == 404

    stop_httpdbg()
Esempio n. 2
0
def test_api_requests_one_request(httpbin):
    def _test(httpbin):
        requests.get(httpbin.url + "/get")

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    assert len(reqs) == 1 + 1  # +1 for the request to retreive the requests
    assert reqs[list(reqs.keys())[0]]["url"] == httpbin.url + "/get"
Esempio n. 3
0
def test_api_get_request_get_status_404(httpbin):
    def _test(httpbin):
        requests.get(httpbin.url + "/get/abc")

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = get_request_details(current_httpdbg_port, 0)

    stop_httpdbg()

    assert ret.status_code == 200
    assert ret.json()["url"] == httpbin.url + "/get/abc"
    assert ret.json()["status_code"] == 404
    assert ret.json()["reason"] == "NOT FOUND"
Esempio n. 4
0
def test_run_script(httpbin):
    def _test(httpbin):
        script_to_run = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "demo_run_script.py")
        run_script([script_to_run, httpbin.url])

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    assert len(reqs) == 2 + 1  # +1 for the request to retreive the requests
    assert reqs[list(reqs.keys())[0]]["url"] == httpbin.url + "/get"
    assert reqs[list(reqs.keys())[1]]["url"] == httpbin.url + "/post"
Esempio n. 5
0
def test_api_request_by_id(httpbin):
    def _test(httpbin):
        requests.get(httpbin.url + "/get/abc")
        requests.get(httpbin.url + "/get/def")

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = get_request_details(current_httpdbg_port, 0)
    assert ret.status_code == 200
    assert ret.json()["url"] == httpbin.url + "/get/abc"

    ret = get_request_details(current_httpdbg_port, 1)
    assert ret.status_code == 200
    assert ret.json()["url"] == httpbin.url + "/get/def"

    stop_httpdbg()
Esempio n. 6
0
def test_run_script_with_exception(httpbin, capsys):
    def _test(httpbin):
        script_to_run = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "demo_run_script.py")
        run_script([script_to_run, httpbin.url, "raise_exception"])

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    assert len(reqs) == 1 + 1  # +1 for the request to retreive the requests
    assert reqs[list(reqs.keys())[0]]["url"] == httpbin.url + "/get"

    assert "--raise_exception--" in capsys.readouterr().err
Esempio n. 7
0
def test_run_pytest_with_exception(capsys):
    def _test():
        script_to_run = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "demo_run_pytest.py"
        )
        run_pytest(["pytest", f"{script_to_run}::test_demo_raise_exception"])

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    assert len(reqs) == 0 + 1  # +1 for the request to retreive the requests

    assert "fixture_which_do_not_exists" in capsys.readouterr().out
Esempio n. 8
0
def test_api_get_request_post(httpbin):
    def _test(httpbin):
        requests.get(httpbin.url + "/")
        requests.post(httpbin.url + "/post", data=b"data to post")

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = get_request_details(current_httpdbg_port, 1)

    # headers
    assert ret.status_code == 200
    assert ret.json()["url"] == httpbin.url + "/post"
    assert ret.json()["method"] == "POST"
    assert ret.json()["status_code"] == 200
    assert ret.json()["reason"] == "OK"

    # request
    assert {"name": "Connection", "value": "keep-alive"} in ret.json()["request"][
        "headers"
    ]
    assert ret.json()["request"]["cookies"] == []
    assert ret.json()["request"]["body"]["filename"] == "upload"
    path_to_content = ret.json()["request"]["body"]["path"]
    assert (
        requests.get(f"http://127.0.0.1:{current_httpdbg_port}/{path_to_content}").text
        == "data to post"
    )

    # response
    assert {"name": "Content-Type", "value": "application/json"} in ret.json()[
        "response"
    ]["headers"]
    assert ret.json()["response"]["cookies"] == []
    assert ret.json()["response"]["body"]["filename"] == "download"
    path_to_content = ret.json()["response"]["body"]["path"]
    assert (
        requests.get(
            f"http://127.0.0.1:{current_httpdbg_port}/{path_to_content}"
        ).status_code
        == 200
    )

    stop_httpdbg()
Esempio n. 9
0
def test_run_pytest(httpbin):
    def _test(httpbin):
        os.environ["HTTPDBG_TEST_PYTEST_BASE_URL"] = httpbin.url
        script_to_run = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "demo_run_pytest.py"
        )
        run_pytest(["pytest", f"{script_to_run}::test_demo_pytest"])

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    assert len(reqs) == 3 + 1  # +1 for the request to retreive the requests
    assert reqs[list(reqs.keys())[0]]["url"] == httpbin.url + "/post"
    assert reqs[list(reqs.keys())[1]]["url"] == httpbin.url + "/get"
    assert reqs[list(reqs.keys())[2]]["url"] == httpbin.url + "/put"
Esempio n. 10
0
def test_run_pytest_src(httpbin):
    def _test(httpbin):
        os.environ["HTTPDBG_TEST_PYTEST_BASE_URL"] = httpbin.url
        script_to_run = os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "demo_run_pytest.py"
        )
        run_pytest(["pytest", f"{script_to_run}::test_demo_pytest"])

    stop_httpdbg, current_httpdbg_port = _run_under_httpdbg(_test, httpbin)

    ret = requests.get(f"http://127.0.0.1:{current_httpdbg_port}/requests")
    stop_httpdbg()

    reqs = ret.json()["requests"]

    print(reqs)

    assert (
        reqs[list(reqs.keys())[0]]["src"].get("label")
        == "tests/demo_run_pytest.py::test_demo_pytest"
    )