|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from hyper.cli import _ARGUMENT_DEFAULTS |
| 3 | +from hyper.cli import main, parse_argument |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize('argv', [ |
| 9 | + ['example.com'], |
| 10 | + ['example.com', '/home'], |
| 11 | + ['-v', 'example.com'], |
| 12 | + ['-n', 'example.com'], |
| 13 | +], ids=[ |
| 14 | + 'specified host', |
| 15 | + 'specified host with path', |
| 16 | + 'specified host with "-v/--verbose" option', |
| 17 | + 'specified host with "-n/--nullout" option', |
| 18 | +]) |
| 19 | +def test_cli_normal(monkeypatch, argv): |
| 20 | + monkeypatch.setattr('hyper.cli.HTTP20Connection', DummyConnection) |
| 21 | + main(argv) |
| 22 | + assert True |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize('argv', [ |
| 26 | + [], |
| 27 | + ['-h'], |
| 28 | +], ids=[ |
| 29 | + 'specified no argument', |
| 30 | + 'specified "-h" option', |
| 31 | +]) |
| 32 | +def test_cli_with_system_exit(argv): |
| 33 | + with pytest.raises(SystemExit): |
| 34 | + main(argv) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize('argv', [ |
| 38 | + {'host': 'example.com'}, |
| 39 | + {'host': 'example.com', 'path': '/home'}, |
| 40 | + {'host': 'example.com', 'encoding': 'latin-1'}, |
| 41 | + {'host': 'example.com', 'nullout': True}, |
| 42 | + {'host': 'example.com', 'method': 'POST'}, |
| 43 | + {'host': 'example.com', 'verbose': True}, |
| 44 | +], ids=[ |
| 45 | + 'specified host', |
| 46 | + 'specified host with path', |
| 47 | + 'specified host with "-e/--encoding" option', |
| 48 | + 'specified host with "-m/--method" option', |
| 49 | + 'specified host with "-n/--nullout" option', |
| 50 | + 'specified host with "-v/--verbose" option', |
| 51 | +]) |
| 52 | +def test_cli_parse_argument(argv): |
| 53 | + d = _ARGUMENT_DEFAULTS.copy() |
| 54 | + d.update(argv) |
| 55 | + _argv = ['-e', d['encoding'], '-m', d['method']] |
| 56 | + for key, value in d.items(): |
| 57 | + if isinstance(value, bool) and value is True: |
| 58 | + _argv.append('--%s' % key) |
| 59 | + _argv.extend([d['host'], d['path']]) |
| 60 | + |
| 61 | + args = parse_argument(_argv) |
| 62 | + for key in d.keys(): |
| 63 | + assert getattr(args, key) == d[key] |
| 64 | + |
| 65 | + |
| 66 | +def test_cli_with_main(monkeypatch): |
| 67 | + monkeypatch.setattr('sys.argv', ['./hyper']) |
| 68 | + import imp |
| 69 | + import hyper.cli |
| 70 | + with pytest.raises(SystemExit): |
| 71 | + imp.load_source('__main__', hyper.cli.__file__) |
| 72 | + |
| 73 | + |
| 74 | +# mock for testing |
| 75 | +class DummyResponse(object): |
| 76 | + def read(self): |
| 77 | + return b'<html>dummy</html>' |
| 78 | + |
| 79 | + |
| 80 | +class DummyConnection(object): |
| 81 | + def __init__(self, host): |
| 82 | + self.host = host |
| 83 | + |
| 84 | + def request(self, method, path): |
| 85 | + return method, path |
| 86 | + |
| 87 | + def getresponse(self): |
| 88 | + return DummyResponse() |
0 commit comments