Ejemplo n.º 1
0
def test_migrate_implicit_content_type():
    config = MockEnvironment().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.º 2
0
 def test_request_body_from_file_by_path_no_field_name_allowed(
         self, httpbin):
     env = MockEnvironment(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.º 3
0
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
        httpbin):
    env = MockEnvironment(stdout_isatty=False)
    r = http('--check-status', '--headers',
             'GET', httpbin.url + '/status/301',
             env=env, tolerate_error_exit_status=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_raw_body():
    r = http(
        '--print=B',
        '--offline',
        'pie.dev',
        'AAA=BBB',
        env=MockEnvironment(stdout_isatty=False),
    )
    assert_output_matches(r, RAW_BODY)
Ejemplo n.º 5
0
 def test_actual_download(self, httpbin_both, httpbin):
     robots_txt = '/robots.txt'
     body = urlopen(httpbin + robots_txt).read().decode()
     env = MockEnvironment(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.º 6
0
 def test_binary_file_form(self, httpbin):
     env = MockEnvironment(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.º 7
0
 def test_force_pretty(self, httpbin):
     env = MockEnvironment(stdout_isatty=False, colors=256)
     r = http(
         '--pretty=all',
         'GET',
         httpbin.url + '/get',
         env=env,
     )
     assert COLOR in r
Ejemplo n.º 8
0
 def test_ignore_stdin(self, httpbin):
     env = MockEnvironment(
         stdin=StdinBytesIO(FILE_PATH.read_bytes()),
         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.º 9
0
def test_only_username_in_url(url):
    """
    https://github.com/jakubroztocil/httpie/issues/242

    """
    args = httpie.cli.parser.parse_args(args=[url], env=MockEnvironment())
    assert args.auth
    assert args.auth.username == 'username'
    assert args.auth.password == ''
Ejemplo n.º 10
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 = MockEnvironment(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.º 11
0
 def test_request_body_from_file_by_path_no_data_items_allowed(
         self, httpbin):
     env = MockEnvironment(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
    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 MockEnvironment(config_dir=self.config_dir)
Ejemplo n.º 13
0
 def test_binary_file_path(self, httpbin):
     env = MockEnvironment(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.º 14
0
def test_redirected_headers_multipart_no_separator():
    r = http(
        '--print=HB',
        '--multipart',
        '--offline',
        'pie.dev',
        'AAA=BBB',
        env=MockEnvironment(stdout_isatty=False),
    )
    assert_output_matches(r, RAW_REQUEST)
Ejemplo n.º 15
0
 def test_quiet(self, httpbin, argument_name):
     env = MockEnvironment(stdin_isatty=True,
                           stdout_isatty=True,
                           devnull=io.BytesIO())
     r = http(argument_name, 'GET', httpbin.url + '/get', env=env)
     assert env.stdout is env.devnull
     assert env.stderr is env.devnull
     assert HTTP_OK in r.devnull
     assert r == ''
     assert r.stderr == ''
Ejemplo n.º 16
0
def test_chunked_stdin():
    r = http('--verbose',
             '--chunked',
             HTTPBIN_WITH_CHUNKED_SUPPORT + '/post',
             env=MockEnvironment(
                 stdin=StdinBytesIO(FILE_PATH.read_bytes()),
                 stdin_isatty=False,
             ))
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count(FILE_CONTENT) == 2
Ejemplo n.º 17
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.request_items = []
     self.parser.args.ignore_stdin = False
     self.parser.env = MockEnvironment()
     self.parser._guess_method()
     assert self.parser.args.method == 'GET'
     assert self.parser.args.url == 'http://example.com/'
     assert self.parser.args.request_items == []
Ejemplo n.º 18
0
 def test_format_option(self, httpbin):
     env = MockEnvironment(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.º 19
0
def test_verbose_redirected_stdout_separator(httpbin):
    """

    <https://github.com/httpie/httpie/issues/1006>
    """
    r = http(
        '-v',
        httpbin.url + '/post',
        'a=b',
        env=MockEnvironment(stdout_isatty=False),
    )
    assert '}HTTP/' not in r
Ejemplo n.º 20
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 = MockEnvironment(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.º 21
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 = MockEnvironment(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.º 22
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=MockEnvironment(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.º 23
0
 def test_multiple_request_bodies_from_file_by_path(self, httpbin):
     env = MockEnvironment(stdin_isatty=True)
     r = http(
         '--verbose',
         'POST',
         httpbin.url + '/post',
         '@' + FILE_PATH_ARG,
         '@' + FILE_PATH_ARG,
         env=env,
         tolerate_error_exit_status=True,
     )
     assert r.exit_status == ExitStatus.ERROR
     assert 'from multiple files' in r.stderr
Ejemplo n.º 24
0
 def test_output_file_pretty_not_allowed_on_windows(self, httpbin):
     env = MockEnvironment(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.º 25
0
def test_chunked_stdin_multiple_chunks():
    stdin_bytes = FILE_PATH.read_bytes() + b'\n' + FILE_PATH.read_bytes()
    r = http('--verbose',
             '--chunked',
             HTTPBIN_WITH_CHUNKED_SUPPORT + '/post',
             env=MockEnvironment(
                 stdin=StdinBytesIO(stdin_bytes),
                 stdin_isatty=False,
                 stdout_isatty=True,
             ))
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count(FILE_CONTENT) == 4
Ejemplo n.º 26
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.request_items = []
     self.parser.args.ignore_stdin = False
     self.parser.env = MockEnvironment()
     self.parser._guess_method()
     assert self.parser.args.method == 'POST'
     assert self.parser.args.url == 'http://example.com/'
     assert self.parser.args.request_items == [
         KeyValueArg(key='data', value='field', sep='=', orig='data=field')
     ]
Ejemplo n.º 27
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.request_items = [
         KeyValueArg(key='old_item', value='b', sep='=', orig='old_item=b')
     ]
     self.parser.args.ignore_stdin = False
     self.parser.env = MockEnvironment()
     self.parser._guess_method()
     assert self.parser.args.request_items, [
         KeyValueArg(key='new_item', value='a', sep='=', orig='new_item=a'),
         KeyValueArg(key='old_item', value='b', sep='=', orig='old_item=b'),
     ]
Ejemplo n.º 28
0
def test_compress_stdin(httpbin_both):
    with open(FILE_PATH) as f:
        env = MockEnvironment(stdin=f, stdin_isatty=False)
        r = http(
            '--compress',
            '--compress',
            'PATCH',
            httpbin_both + '/patch',
            env=env,
        )
    assert HTTP_OK in r
    assert r.json['headers']['Content-Encoding'] == 'deflate'
    assert_decompressed_equal(r.json['data'], FILE_CONTENT.strip())
    assert not r.json['json']
Ejemplo n.º 29
0
def test_pretty_redirected_stream(httpbin):
    """Test that --stream works with prettified redirected output."""
    env = MockEnvironment(
        colors=256,
        stdin=StdinBytesIO(BIN_FILE_PATH.read_bytes()),
        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
Ejemplo n.º 30
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.request_items = []
     self.parser.args.ignore_stdin = False
     self.parser.env = MockEnvironment()
     self.parser._guess_method()
     assert self.parser.args.method == 'GET'
     assert self.parser.args.url == 'http://example.com/'
     assert self.parser.args.request_items, [
         KeyValueArg(key='test',
                     value='header',
                     sep=':',
                     orig='test:header')
     ]
Ejemplo n.º 31
0
def test_form_POST_file_redirected_stdin(httpbin):
    """
    <https://github.com/httpie/httpie/issues/840>

    """
    with open(FILE_PATH) as f:
        r = http(
            '--form',
            'POST',
            httpbin + '/post',
            f'file@{FILE_PATH}',
            tolerate_error_exit_status=True,
            env=MockEnvironment(
                stdin=StdinBytesIO(FILE_PATH.read_bytes()),
                stdin_isatty=False,
            ),
        )
    assert r.exit_status == ExitStatus.ERROR
    assert 'cannot be mixed' in r.stderr
Ejemplo n.º 32
0
def test_default_options(httpbin):
    env = MockEnvironment()
    env.config['default_options'] = ['--form']
    env.config.save()
    r = http(httpbin.url + '/post', 'foo=bar', env=env)
    assert r.json['form'] == {"foo": "bar"}