Exemple #1
0
def test_call_no_colors(capsys):
    config = CFG(path="",
                 settings={"colors": False},
                 variables={},
                 endpoints={})
    echo = Echo(config=config)

    echo("123")
    captured = capsys.readouterr()
    assert "123\n" == captured.out

    echo(123)
    captured = capsys.readouterr()
    assert "123\n" == captured.out

    echo(True)
    captured = capsys.readouterr()
    assert "True\n" == captured.out

    echo({"a": 1})
    captured = capsys.readouterr()
    assert '{\n    "a": 1\n}\n' == captured.out

    echo("")
    captured = capsys.readouterr()
    assert "\n" == captured.out

    echo(b"JSONDecodeError")
    captured = capsys.readouterr()
    assert "JSONDecodeError\n" == captured.out
Exemple #2
0
def test_call_get(server):
    url = f"{server.address}/endpoint/"
    config = CFG(
        path="",
        settings={},
        variables={},
        endpoints={
            "name": {
                "headers": {
                    "content-type": "application/json"
                },
                "request": f"get {url}",
                "params": {
                    "a": 1,
                    "b": 2
                },
                "cookies": {
                    "ca": 1,
                    "cb": 2
                },
            }
        },
    )
    response = ADZ(config=config)(name="name")
    assert response.method == "GET"
    assert response.url == f"{url}?a=1&b=2"
    assert response.status_code == 200
Exemple #3
0
def test_call_endpoint_does_not_exist(server):
    url = f"{server.address}/endpoint/"
    config = CFG(
        path="",
        settings={},
        variables={},
        endpoints={"name": {
            "request": f"get {url}"
        }},
    )
    response = ADZ(config=config)(name="abc")
    assert response is None
Exemple #4
0
def test_response_to_dict(server):
    url = f"{server.address}/endpoint/"
    config = CFG(
        path="",
        settings={},
        variables={},
        endpoints={"name": {
            "request": f"get {url}"
        }},
    )
    response = ADZ(config=config)(name="name")
    assert list(response.__slots__).sort() == list(
        response.to_dict().keys()).sort()
Exemple #5
0
def test_call_post_files(server, tmpdir):
    file = tmpdir.join("file.json")
    file.write('{"a":1,"b":2}')
    url = f"{server.address}/endpoint/"
    config = CFG(
        path="",
        settings={},
        variables={},
        endpoints={
            "name": {
                "headers": {
                    "content-type": "application/json"
                },
                "request": f"post {url}",
                "files": [str(file)],
            }
        },
    )
    response = ADZ(config=config)(name="name")
    assert response.method == "POST"
    assert response.url == url
    assert response.status_code == 200
Exemple #6
0
def test_call_post(server):
    url = f"{server.address}/endpoint/"
    config = CFG(
        path="",
        settings={},
        variables={},
        endpoints={
            "name": {
                "headers": {
                    "content-type": "application/json"
                },
                "request": f"post {url}",
                "json": {
                    "a": 1
                },
            }
        },
    )
    response = ADZ(config=config)(name="name")
    assert response.method == "POST"
    assert response.url == url
    assert response.status_code == 200
Exemple #7
0
def test_call_colors(capsys):
    config = CFG(path="",
                 settings={"colors": True},
                 variables={},
                 endpoints={})
    echo = Echo(config=config)

    echo("abc")
    captured = capsys.readouterr()
    assert "mabc" in captured.out

    echo(123)
    captured = capsys.readouterr()
    assert "123" in captured.out

    echo(True)
    captured = capsys.readouterr()
    assert "True" in captured.out

    echo([1, 2, 3])
    captured = capsys.readouterr()
    for s in ["    1,", "    2,", "    3"]:
        assert s in captured.out, s
Exemple #8
0
def main(config, details, list_, output, settings, var, colors, response,
         endpoints):
    config = CFG.configure(
        path=config,
        settings=dict(colors=colors, response=response),
        variables=dict([v.split("=", 1) for v in var]),
    )
    if not config:
        return print("Missing configuration file")

    adz = ADZ(config=config)
    echo = Echo(config=config)

    if list_:
        echo()
        echo("  » Endpoints")
        for name, values in config.endpoints.items():
            echo(f"    • {name}\t{values.get('description', '')}")
        return echo()

    if settings:
        echo()
        echo("  » Settings")
        for k, v in config.settings.items():
            echo(f"    • {k}: {v}")
        echo()
        echo(f"  From config file at '{config.path}'")
        return echo()

    if output:
        echo("  » Configuration")
        echo(config.to_dict(), "json")
        return echo()

    for endpoint in endpoints:
        if endpoint not in config.endpoints:
            echo()
            echo(f'"{endpoint}" endpoint does not exist')
            echo()
            continue

        if details:
            echo()
            echo(f'  » Endpoint "{endpoint}"')
            for name, values in config.endpoints[endpoint].items():
                echo(f"    • {name}\t{values}")
            echo()
            continue

        response = adz(name=endpoint)
        echo(f"{response.method} " f"{response.url}")
        echo(
            f"{response.http_version} {response.status_code} {response.status_phrase}"
        )

        for k, v in response.headers.items():
            echo(f" • {k}: {v if 'auth' not in k.lower() else '<hidden>'}")

        if config.settings["response"]:
            echo()
            echo(response.content, "json")
        echo()

    if not endpoints:
        with click.Context(main) as ctx:
            echo(main.get_help(ctx))