Esempio n. 1
0
async def generate_har(httpserver: HTTPServer, headers) -> dataclasses.har.Har:
    httpserver.expect_oneshot_request("/foo", method="GET").respond_with_data(
        response_data="", headers=headers)

    async with page_with_har_tracer() as (page, tracer):
        await page.goto(httpserver.url_for("/foo"))

        har = await tracer.flush()
        return har
Esempio n. 2
0
async def generate_har_with_unusual_cookies(
        httpserver: HTTPServer, cookie_value: str) -> dataclasses.har.Har:
    set_cookie = f"cookie_name={cookie_value}"
    httpserver.expect_oneshot_request("/foo", method="GET").respond_with_data(
        response_data="", headers={"set-cookie": set_cookie})

    async with page_with_har_tracer() as (page, tracer):
        await page.goto(httpserver.url_for("/foo"))
        har = await tracer.flush()
        return har
Esempio n. 3
0
async def generate_har(httpserver: HTTPServer) -> dataclasses.har.Har:
    httpserver.expect_oneshot_request("/foo", method="GET").respond_with_data(
        response_data="", status=200)
    httpserver.expect_oneshot_request("/post",
                                      method="POST").respond_with_json(
                                          response_json={},
                                          status=200,
                                      )

    async with page_with_har_tracer() as (page, tracer):
        await page.goto(httpserver.url_for("/foo"))
        await page.evaluate(
            """fetch('./post', { method: 'POST', body: 'Hello' })""")

        har = await tracer.flush()
        return har
Esempio n. 4
0
async def generate_har_with_post_params(
        httpserver: HTTPServer) -> dataclasses.har.Har:
    httpserver.expect_oneshot_request("/foo", method="GET").respond_with_data(
        response_data="", status=200)
    httpserver.expect_oneshot_request("/post",
                                      method="POST").respond_with_json(
                                          response_json={},
                                          status=200,
                                      )

    async with page_with_har_tracer() as (page, tracer):
        await page.goto(httpserver.url_for("/foo"))

        await page.set_content(
            "<form method='POST' action='/post'><input type='text' name='foo' value='bar'><input type='number' name='baz' value='123'><input type='submit'></form>"
        )
        await page.click("input[type=submit]")

        har = await tracer.flush()
        return har
async def test_redirect(httpserver: HTTPServer):
    har = await generate_har(httpserver)

    entries = har.log.entries
    assert len(entries) == 2

    first = entries[0]
    assert first.response.status == 302
    assert first.response.redirect_url == httpserver.url_for("/bar")

    second = entries[1]
    assert second.response.status == 200
Esempio n. 6
0
async def test_configuration_initial(
    github_server: httpserver.HTTPServer,
    redis_cache: utils.RedisCache,
) -> None:
    github_server.expect_oneshot_request(
        f"{BASE_URL}/pulls/1", ).respond_with_json(
            GH_PULL,
            status=200,
        )

    github_server.expect_oneshot_request(
        f"{BASE_URL}/contents/.mergify.yml", ).respond_with_data(status=404)

    github_server.expect_oneshot_request(
        f"{BASE_URL}/contents/.mergify/config.yml", ).respond_with_data(
            status=404)

    github_server.expect_oneshot_request(
        f"{BASE_URL}/contents/.github/mergify.yml", ).respond_with_data(
            status=404)

    github_server.expect_oneshot_request(
        f"{BASE_URL}/contents/.mergify.yml",
        query_string={
            "ref": GH_PULL["head"]["sha"]
        },
    ).respond_with_json(
        github_types.GitHubContentFile({
            "type":
            "file",
            "content":
            FAKE_MERGIFY_CONTENT,
            "path":
            ".mergify.yml",
            "sha":
            github_types.SHAType("739e5ec79e358bae7a150941a148b4131233ce2c"),
        }),
        status=200,
    )

    github_server.expect_oneshot_request(
        f"{BASE_URL}/commits/{GH_PULL['head']['sha']}/check-runs"
    ).respond_with_json({"check_runs": []}, status=200)

    github_server.expect_oneshot_request(f"{BASE_URL}/check-runs",
                                         method="POST").respond_with_json(
                                             {}, status=200)

    async with github.AsyncGithubInstallationClient(
            github.get_auth(GH_OWNER["login"])) as client:
        installation = context.Installation(
            GH_OWNER["id"],
            GH_OWNER["login"],
            subscription.Subscription(redis_cache, 0, False, "", frozenset(),
                                      0),
            client,
            redis_cache,
        )
        repository = context.Repository(installation, GH_REPO)
        ctxt = await repository.get_pull_request_context(
            github_types.GitHubPullRequestNumber(1))

        main_config_file = await repository.get_mergify_config_file()
        assert main_config_file is None

        changed = await engine._check_configuration_changes(
            ctxt, main_config_file)
        assert changed

    github_server.check_assertions()
async def test_har_tracer(httpserver: HTTPServer, test_html: str):
    status = 200
    headers = {"content-type": "text/html"}
    har = await generate_har(
        httpserver, response_data=test_html, status=status, headers=headers
    )

    # assert log
    assert har.log.version == "1.2"

    # assert creator
    assert har.log.creator.version == __version__
    assert har.log.creator.name == "playwright-har-tracer"

    # assert browser
    assert har.log.browser.name == "chromium"

    # assert pages
    assert len(har.log.pages) == 1

    # assert page
    page = har.log.pages[0]
    assert page.id == "page_0"
    assert page.title == "Document"
    assert page.started_date_time < datetime.now(timezone.utc)
    assert page.page_timings.on_content_load is not None
    assert float(page.page_timings.on_content_load) > 0.0
    assert page.page_timings.on_load is not None
    assert float(page.page_timings.on_load) > 0.0

    # assert entries
    entries = har.log.entries
    assert len(entries) == 1

    entry = entries[0]
    # assert request
    assert entry.request.method == "GET"
    assert entry.request.url == httpserver.url_for("/foo")
    assert entry.request.http_version == "HTTP/1.1"
    headers = headers_to_dict(entry.request.headers)
    assert "user-agent" in headers
    assert entry.request.body_size == 0

    # assert response
    assert entry.response.status == status
    assert entry.response.status_text == "OK"
    assert entry.response.http_version == "HTTP/1.1"
    assert len(entry.response.headers) > 0
    headers = headers_to_dict(entry.response.headers)
    assert headers.get("content-type") == "text/html"

    # assert content
    assert entry.response.content.encoding == "base64"
    assert entry.response.content.mime_type == "text/html"
    assert entry.response.content.text is not None
    assert base64.b64decode(entry.response.content.text).decode() == test_html

    # assert server IP address and port
    assert entry.server_ip_address == "127.0.0.1"
    assert entry._server_port == httpserver.port

    # assert security details
    assert entry._security_details is None