Ejemplo n.º 1
0
    def test_guess_when_method_not_set(self):
        self.parser.args = argparse.Namespace()
        self.parser.args.method = None
        self.parser.args.url = 'http://example.com/'
        self.parser.args.items = []
        self.parser.args.ignore_stdin = False
        self.parser.env = TestEnvironment()

        self.parser._guess_method()

        assert self.parser.args.method == 'GET'
        assert self.parser.args.url == 'http://example.com/'
        assert self.parser.args.items == []
Ejemplo n.º 2
0
def test_output_option(httpbin, stdout_isatty):
    output_filename = os.path.join(gettempdir(), test_output_option.__name__)
    url = httpbin + '/robots.txt'

    r = http('--output', output_filename, url,
             env=TestEnvironment(stdout_isatty=stdout_isatty))
    assert r == ''

    expected_body = urlopen(url).read().decode()
    with open(output_filename, 'r') as f:
        actual_body = f.read()

    assert actual_body == expected_body
Ejemplo n.º 3
0
    def test_subtype_based_pygments_lexer_match(self):
        """Test that media subtype is used if type/subtype doesn't
        match any lexer.

        """
        env = TestEnvironment(colors=256)
        r = http('--print=B',
                 '--pretty=all',
                 httpbin('/post'),
                 'Content-Type:text/foo+json',
                 'a=b',
                 env=env)
        assert COLOR in r
 def test_output_file_pretty_not_allowed_on_windows(self, httpbin):
     env = TestEnvironment(is_windows=True)
     output_file = os.path.join(
         tempfile.gettempdir(),
         self.test_output_file_pretty_not_allowed_on_windows.__name__)
     r = http('--output',
              output_file,
              '--pretty=all',
              'GET',
              httpbin.url + '/get',
              env=env,
              error_exit_ok=True)
     assert 'Only terminal output can be colorized on Windows' in r.stderr
def test_pretty_redirected_stream(httpbin):
    """Test that --stream works with prettified redirected output."""
    with open(BIN_FILE_PATH, 'rb') as f:
        env = TestEnvironment(colors=256,
                              stdin=f,
                              stdin_isatty=False,
                              stdout_isatty=False)
        r = http('--verbose',
                 '--pretty=all',
                 '--stream',
                 'GET',
                 httpbin.url + '/get',
                 env=env)
    assert BINARY_SUPPRESSED_NOTICE.decode() in r
    def test_guess_when_method_set_but_invalid_and_data_field(self):
        self.parser.args = argparse.Namespace()
        self.parser.args.method = 'http://example.com/'
        self.parser.args.url = 'data=field'
        self.parser.args.items = []
        self.parser.args.ignore_stdin = False
        self.parser.env = TestEnvironment()
        self.parser._guess_method()

        assert self.parser.args.method == 'POST'
        assert self.parser.args.url == 'http://example.com/'
        assert self.parser.args.items == [
            KeyValue(key='data', value='field', sep='=', orig='data=field')
        ]
Ejemplo n.º 7
0
 def test_redirected_stream(self):
     """Test that --stream works with non-prettified
     redirected terminal output."""
     with open(BIN_FILE_PATH, 'rb') as f:
         env = TestEnvironment(stdout_isatty=False,
                               stdin_isatty=False,
                               stdin=f)
         r = http('--pretty=none',
                  '--stream',
                  '--verbose',
                  'GET',
                  httpbin('/get'),
                  env=env)
     assert BIN_FILE_CONTENT in r
    def test_guess_when_method_set_but_invalid_and_header_field(self):
        self.parser.args = argparse.Namespace()
        self.parser.args.method = 'http://example.com/'
        self.parser.args.url = 'test:header'
        self.parser.args.items = []
        self.parser.args.ignore_stdin = False

        self.parser.env = TestEnvironment()

        self.parser._guess_method()

        assert self.parser.args.method == 'GET'
        assert self.parser.args.url == 'http://example.com/'
        assert self.parser.args.items, [
            KeyValue(key='test', value='header', sep=':', orig='test:header')
        ]
    def test_guess_when_method_set_but_invalid_and_item_exists(self):
        self.parser.args = argparse.Namespace()
        self.parser.args.method = 'http://example.com/'
        self.parser.args.url = 'new_item=a'
        self.parser.args.items = [
            KeyValue(key='old_item', value='b', sep='=', orig='old_item=b')
        ]
        self.parser.args.ignore_stdin = False

        self.parser.env = TestEnvironment()

        self.parser._guess_method()

        assert self.parser.args.items, [
            KeyValue(key='new_item', value='a', sep='=', orig='new_item=a'),
            KeyValue(key='old_item', value='b', sep='=', orig='old_item=b'),
        ]
Ejemplo n.º 10
0
 def test_force_pretty(self, httpbin):
     env = TestEnvironment(stdout_isatty=False, colors=256)
     r = http('--pretty=all', 'GET', httpbin.url + '/get', env=env, )
     assert COLOR in r
Ejemplo n.º 11
0
 def test_dont_expand_longer_ipv6_as_shorthand(self):
     args = parser.parse_args(
         args=['::ffff:c000:0280'],
         env=TestEnvironment()
     )
     assert args.url == 'http://::ffff:c000:0280'
Ejemplo n.º 12
0
 def test_dont_expand_full_ipv6_as_shorthand(self):
     args = parser.parse_args(
         args=['0000:0000:0000:0000:0000:0000:0000:0001'],
         env=TestEnvironment()
     )
     assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
Ejemplo n.º 13
0
 def test_expand_localhost_shorthand_with_slash(self):
     args = parser.parse_args(args=[':/'], env=TestEnvironment())
     assert args.url == 'http://localhost/'
Ejemplo n.º 14
0
 def test_expand_localhost_shorthand_with_port_and_path(self):
     args = parser.parse_args(args=[':3000/path'], env=TestEnvironment())
     assert args.url == 'http://localhost:3000/path'
Ejemplo n.º 15
0
 def test_implicit_POST_stdin(self, httpbin):
     with open(FILE_PATH) as f:
         env = TestEnvironment(stdin_isatty=False, stdin=f)
         r = http('--form', httpbin.url + '/post', env=env)
     assert HTTP_OK in r
Ejemplo n.º 16
0
def test_POST_stdin(httpbin_both):
    with open(FILE_PATH) as f:
        env = TestEnvironment(stdin=f, stdin_isatty=False)
        r = http('--form', 'POST', httpbin_both + '/post', env=env)
    assert HTTP_OK in r
    assert FILE_CONTENT in r
Ejemplo n.º 17
0
 def test_binary_included_and_correct_when_suitable(self):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('GET', self.url, env=env)
     assert r == self.bindata
 def setUpClass(cls):
     cls.test_environment = TestEnvironment(
         test_running_dir=cls._TESTS_RUNNING_DIR,
         config_name=cls._CONFIG_NAME)
Ejemplo n.º 19
0
 def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('GET', httpbin.url + '/get', env=env)
     assert 'HTTP/' not in r
Ejemplo n.º 20
0
 def test_binary_suppresses_when_not_terminal_but_pretty(self):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('--pretty=all', 'GET', self.url, env=env)
     assert BINARY_SUPPRESSED_NOTICE.decode() in r
Ejemplo n.º 21
0
 def test_request_body_from_file_by_path_no_field_name_allowed(
         self, httpbin):
     env = TestEnvironment(stdin_isatty=True)
     r = http('POST', httpbin.url + '/post', 'field-name@' + FILE_PATH_ARG,
              env=env, error_exit_ok=True)
     assert 'perhaps you meant --form?' in r.stderr
Ejemplo n.º 22
0
def test_default_options(httpbin):
    env = TestEnvironment()
    env.config['default_options'] = ['--form']
    env.config.save()
    r = http(httpbin.url + '/post', 'foo=bar', env=env)
    assert r.json['form'] == {"foo": "bar"}
Ejemplo n.º 23
0
 def test_request_body_from_file_by_path_no_data_items_allowed(
         self, httpbin):
     env = TestEnvironment(stdin_isatty=False)
     r = http('POST', httpbin.url + '/post', '@' + FILE_PATH_ARG, 'foo=bar',
              env=env, error_exit_ok=True)
     assert 'cannot be mixed' in r.stderr
Ejemplo n.º 24
0
 def test_binary_file_path(self, httpbin):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('--print=B', 'POST', httpbin.url + '/post',
              '@' + BIN_FILE_PATH_ARG, env=env, )
     assert r == BIN_FILE_CONTENT
Ejemplo n.º 25
0
 def test_binary_file_form(self, httpbin):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('--print=B', '--form', 'POST', httpbin.url + '/post',
              'test@' + BIN_FILE_PATH_ARG, env=env)
     assert bytes(BIN_FILE_CONTENT) in bytes(r)
Ejemplo n.º 26
0
 def test_pretty_enabled_by_default(self, httpbin):
     env = TestEnvironment(colors=256)
     r = http('GET', httpbin.url + '/get', env=env)
     assert COLOR in r
Ejemplo n.º 27
0
 def test_print_overridable_when_stdout_redirected(self, httpbin):
     env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
     assert HTTP_OK in r