Ejemplo n.º 1
0
 def test_binary_stdin(self, httpbin):
     with open(BIN_FILE_PATH, 'rb') as stdin:
         env = _TestEnvironment(stdin=stdin,
                                stdin_isatty=False,
                                stdout_isatty=False)
         r = http('--print=B', 'POST', httpbin.url + '/post', env=env)
         assert r == BIN_FILE_CONTENT
Ejemplo n.º 2
0
 def test_ignore_stdin(self, httpbin):
     with open(FILE_PATH) as f:
         env = _TestEnvironment(stdin=f, stdin_isatty=False)
         r = http('--ignore-stdin', '--verbose', httpbin.url + '/get',
                  env=env)
     assert HTTP_OK in r
     assert 'GET /get HTTP' in r, "Don't default to POST."
     assert FILE_CONTENT not in r, "Don't send stdin data."
Ejemplo n.º 3
0
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
        httpbin):
    env = _TestEnvironment(stdout_isatty=False)
    r = http('--check-status', '--headers',
             'GET', httpbin.url + '/status/301',
             env=env, error_exit_ok=True)
    assert '301 MOVED PERMANENTLY' in r
    assert r.exit_status == ExitStatus.ERROR_HTTP_3XX
    assert '301 moved permanently' in r.stderr.lower()
Ejemplo n.º 4
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.º 5
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.º 6
0
 def test_actual_download(self, httpbin_both, httpbin):
     robots_txt = '/robots.txt'
     body = urlopen(httpbin + robots_txt).read().decode()
     env = _TestEnvironment(stdin_isatty=True, stdout_isatty=False)
     r = http('--download', httpbin_both.url + robots_txt, env=env)
     assert 'Downloading' in r.stderr
     assert '[K' in r.stderr
     assert 'Done' in r.stderr
     assert body == r
Ejemplo n.º 7
0
def test_only_username_in_url(url):
    """
    https://github.com/jakubroztocil/httpie/issues/242

    """
    args = httpie.cli.parser.parse_args(args=[url], env=_TestEnvironment())
    assert args.auth
    assert args.auth.username == 'username'
    assert args.auth.password == ''
Ejemplo n.º 8
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.º 9
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.º 10
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.º 11
0
    def env(self):
        """
        Return an environment.

        Each environment created withing a test method
        will share the same config_dir. It is necessary
        for session files being reused.

        """
        return _TestEnvironment(config_dir=self.config_dir)
Ejemplo n.º 12
0
 def test_format_option(self, httpbin):
     env = _TestEnvironment(colors=256)
     r = http('--print=B',
              '--pretty=format',
              'GET',
              httpbin.url + '/get',
              'a=b',
              env=env)
     # Tests that the JSON data is formatted.
     assert r.strip().count('\n') == 2
     assert COLOR not in r
Ejemplo n.º 13
0
def test_encoded_stream(httpbin):
    """Test that --stream works with non-prettified
    redirected terminal output."""
    with open(BIN_FILE_PATH, 'rb') as f:
        env = _TestEnvironment(stdin=f, stdin_isatty=False)
        r = http('--pretty=none',
                 '--stream',
                 '--verbose',
                 'GET',
                 httpbin.url + '/get',
                 env=env)
    assert BINARY_SUPPRESSED_NOTICE.decode() in r
Ejemplo n.º 14
0
    def test_subtype_based_pygments_lexer_match(self, httpbin):
        """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.url + '/post',
                 'Content-Type:text/foo+json',
                 'a=b',
                 env=env)
        assert COLOR in r
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
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.º 17
0
def test_migrate_implicit_content_type():
    config = _TestEnvironment().config

    config['implicit_content_type'] = 'json'
    config.save()
    config.load()
    assert 'implicit_content_type' not in config
    assert not config['default_options']

    config['implicit_content_type'] = 'form'
    config.save()
    config.load()
    assert 'implicit_content_type' not in config
    assert config['default_options'] == ['--form']
Ejemplo n.º 18
0
def test_redirected_stream(httpbin):
    """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.url + '/get',
                 env=env)
    assert BIN_FILE_CONTENT in r
Ejemplo n.º 19
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.º 20
0
    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.º 21
0
    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.º 22
0
    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')
        ]
Ejemplo n.º 23
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.º 24
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.º 25
0
 def test_expand_localhost_shorthand_with_slash(self):
     args = parser.parse_args(args=[':/'], env=_TestEnvironment())
     assert args.url == 'http://localhost/'
Ejemplo n.º 26
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.º 27
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.º 28
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.º 29
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
Ejemplo n.º 30
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