Ejemplo n.º 1
0
def test_python_requests_post__response(
    args_group,
    assert_request_args,
    create_request_args_files,
):
    result = generate_http_request_code(
        'python',
        'requests',
        'POST',
        **combination_arguments_to_kwargs(args_group['arguments']),
    )

    if 'import requests' not in result:
        result = 'import requests\n\n%s' % result

    # Create files, if needed
    files = create_request_args_files(args_group)

    namespace = {}
    exec(result, namespace)
    assert_request_args(args_group['arguments'], namespace['req'].json())

    for f in files:
        f.close()
        os.remove(f.name)
Ejemplo n.º 2
0
def test_bash_curl_post(args_group):
    with open(args_group['filename']) as f:
        expected_result = f.read()

    result = generate_http_request_code(
        'bash', 'curl', 'POST',
        **combination_arguments_to_kwargs(args_group['arguments']),
    )

    assert result == expected_result
Ejemplo n.º 3
0
def test_python_requests_get(args_group):
    with open(args_group['filename']) as f:
        expected_result = f.read()

    result = generate_http_request_code(
        'python',
        'requests',
        'GET',
        **combination_arguments_to_kwargs(args_group['arguments']),
    )

    assert result == expected_result
Ejemplo n.º 4
0
def test_javascript_fetch_post(args_group):
    with open(args_group['filename']) as f:
        expected_result = f.read()

    result = generate_http_request_code(
        'javascript',
        'fetch',
        'POST',
        **combination_arguments_to_kwargs(args_group['arguments']),
    )

    assert result == expected_result
Ejemplo n.º 5
0
def test_python_requests_get__response(args_group, assert_request_args):
    result = generate_http_request_code(
        'python',
        'requests',
        'GET',
        **combination_arguments_to_kwargs(args_group['arguments']),
    )

    if 'import requests' not in result:
        result = 'import requests\n\n%s' % result
    namespace = {}
    exec(result, namespace)
    assert_request_args(args_group['arguments'], namespace['req'].json())
def main(args=[]):
    opts = parse_options(args=args)

    if os.path.exists(opts.directory):
        sys.stderr.write('The directory \'%s\' exists.\n' % opts.directory)
    os.makedirs(opts.directory, exist_ok=True)

    try:
        for args_group in get_argument_combinations(method=opts.method):
            result = generate_http_request_code(
                language=opts.language,
                impl=opts.implementation,
                method=opts.method,
                **combination_arguments_to_kwargs(args_group['arguments']),
            )

            fpath = os.path.join(opts.directory, args_group['filename'])
            with open(fpath, 'w') as f:
                f.write(result)
    except Exception as err:
        shutil.rmtree(opts.directory)
        raise err

    return 0