edq.cli.http.exchange-server

Start an HTTP test server that serves the specified HTTP exchanges.

 1# pylint: disable=invalid-name
 2
 3"""
 4Start an HTTP test server that serves the specified HTTP exchanges.
 5"""
 6
 7import argparse
 8import os
 9import sys
10
11import edq.core.argparser
12import edq.testing.httpserver
13
14def run_cli(args: argparse.Namespace) -> int:
15    """ Run the CLI. """
16
17    match_options = {
18        'params_to_skip': args.ignore_params,
19        'headers_to_skip': args.ignore_headers,
20    }
21
22    server = edq.testing.httpserver.HTTPTestServer(
23            port = args.port,
24            match_options = match_options,
25            verbose = True,
26            raise_on_404 = False,
27    )
28
29    for path in args.paths:
30        path = os.path.abspath(path)
31
32        if (os.path.isfile(path)):
33            server.load_exchange(path)
34        else:
35            server.load_exchanges_dir(path)
36
37    server.start_and_wait()
38
39    return 0
40
41def main() -> int:
42    """ Get a parser, parse the args, and call run. """
43    return run_cli(_get_parser().parse_args())
44
45def _get_parser() -> argparse.ArgumentParser:
46    """ Get the parser. """
47
48    parser = edq.core.argparser.get_default_parser(__doc__.strip(),
49            include_net = True,
50    )
51
52    parser.add_argument('paths', metavar = 'PATH',
53        type = str, nargs = '+',
54        help = 'Path to exchange files or dirs (which will be recursively searched for all exchange files).')
55
56    parser.add_argument('--port', dest = 'port',
57        action = 'store', type = int, default = None,
58        help = 'The port to run this test server on. If not set, a random open port will be chosen.')
59
60    parser.add_argument('--ignore-param', dest = 'ignore_params',
61        action = 'append', type = str, default = [],
62        help = 'Ignore this parameter during exchange matching.')
63
64    parser.add_argument('--ignore-header', dest = 'ignore_headers',
65        action = 'append', type = str, default = [],
66        help = 'Ignore this header during exchange matching.')
67
68    return parser
69
70if (__name__ == '__main__'):
71    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
15def run_cli(args: argparse.Namespace) -> int:
16    """ Run the CLI. """
17
18    match_options = {
19        'params_to_skip': args.ignore_params,
20        'headers_to_skip': args.ignore_headers,
21    }
22
23    server = edq.testing.httpserver.HTTPTestServer(
24            port = args.port,
25            match_options = match_options,
26            verbose = True,
27            raise_on_404 = False,
28    )
29
30    for path in args.paths:
31        path = os.path.abspath(path)
32
33        if (os.path.isfile(path)):
34            server.load_exchange(path)
35        else:
36            server.load_exchanges_dir(path)
37
38    server.start_and_wait()
39
40    return 0

Run the CLI.

def main() -> int:
42def main() -> int:
43    """ Get a parser, parse the args, and call run. """
44    return run_cli(_get_parser().parse_args())

Get a parser, parse the args, and call run.