<s> """<STR_LIT>""" <EOL> import json <EOL> import argparse <EOL> import pytest <EOL> from requests . exceptions import InvalidSchema <EOL> from httpie import input <EOL> from httpie . input import KeyValue , KeyValueArgType , DataDict <EOL> from httpie import ExitStatus <EOL> from httpie . cli import parser <EOL> from utils import TestEnvironment , http , HTTP_OK <EOL> from fixtures import ( <EOL> FILE_PATH_ARG , JSON_FILE_PATH_ARG , <EOL> JSON_FILE_CONTENT , FILE_CONTENT , FILE_PATH <EOL> ) <EOL> class TestItemParsing : <EOL> key_value = KeyValueArgType ( * input . SEP_GROUP_ALL_ITEMS ) <EOL> def test_invalid_items ( self ) : <EOL> items = [ '<STR_LIT>' ] <EOL> for item in items : <EOL> pytest . raises ( argparse . ArgumentTypeError , self . key_value , item ) <EOL> def test_escape_separator ( self ) : <EOL> items = input . parse_items ( [ <EOL> self . key_value ( r'<STR_LIT>' ) , <EOL> self . key_value ( r'<STR_LIT>' ) , <EOL> self . key_value ( r'<STR_LIT>' ) , <EOL> self . key_value ( r'<STR_LIT>' % FILE_PATH_ARG ) , <EOL> ] ) <EOL> headers = dict ( items . headers . _store . values ( ) ) <EOL> assert headers == { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> assert items . data == { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> assert '<STR_LIT>' in items . files <EOL> @ pytest . mark . parametrize ( ( '<STR_LIT:string>' , '<STR_LIT:key>' , '<STR_LIT>' , '<STR_LIT:value>' ) , [ <EOL> ( '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT:=>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:path>' , '<STR_LIT:=>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:=>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> def test_backslash_before_non_special_character_does_not_escape ( <EOL> self , string , key , sep , value ) : <EOL> expected = KeyValue ( orig = string , key = key , sep = sep , value = value ) <EOL> actual = self . key_value ( string ) <EOL> assert actual == expected <EOL> def test_escape_longsep ( self ) : <EOL> items = input . parse_items ( [ <EOL> self . key_value ( r'<STR_LIT>' ) , <EOL> ] ) <EOL> assert items . params == { '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> def test_valid_items ( self ) : <EOL> items = input . parse_items ( [ <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' + FILE_PATH_ARG ) , <EOL> self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' + FILE_PATH_ARG ) , <EOL> self . key_value ( '<STR_LIT>' + JSON_FILE_PATH_ARG ) , <EOL> ] ) <EOL> headers = dict ( items . headers . _store . values ( ) ) <EOL> assert headers == { '<STR_LIT>' : '<STR_LIT:value>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> raw_json_embed = items . data . pop ( '<STR_LIT>' ) <EOL> assert raw_json_embed == json . loads ( JSON_FILE_CONTENT ) <EOL> items . data [ '<STR_LIT>' ] = items . data [ '<STR_LIT>' ] . strip ( ) <EOL> assert dict ( items . data ) == { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:string>" : "<STR_LIT:value>" , <EOL> "<STR_LIT:bool>" : True , <EOL> "<STR_LIT:list>" : [ "<STR_LIT:a>" , <NUM_LIT:1> , { } , False ] , <EOL> "<STR_LIT>" : { "<STR_LIT:a>" : "<STR_LIT:b>" } , <EOL> "<STR_LIT>" : FILE_CONTENT , <EOL> } <EOL> assert items . params == { '<STR_LIT>' : '<STR_LIT:value>' } <EOL> assert '<STR_LIT:file>' in items . files <EOL> assert ( items . files [ '<STR_LIT:file>' ] [ <NUM_LIT:1> ] . read ( ) . strip ( ) . decode ( '<STR_LIT:utf8>' ) <EOL> == FILE_CONTENT ) <EOL> def test_multiple_file_fields_with_same_field_name ( self ) : <EOL> items = input . parse_items ( [ <EOL> self . key_value ( '<STR_LIT>' + FILE_PATH_ARG ) , <EOL> self . key_value ( '<STR_LIT>' + FILE_PATH_ARG ) , <EOL> ] ) <EOL> assert len ( items . files [ '<STR_LIT>' ] ) == <NUM_LIT:2> <EOL> def test_multiple_text_fields_with_same_field_name ( self ) : <EOL> items = input . parse_items ( <EOL> [ self . key_value ( '<STR_LIT>' ) , <EOL> self . key_value ( '<STR_LIT>' ) ] , <EOL> data_class = DataDict <EOL> ) <EOL> assert items . data [ '<STR_LIT>' ] == [ '<STR_LIT:a>' , '<STR_LIT:b>' ] <EOL> assert list ( items . data . items ( ) ) == [ <EOL> ( '<STR_LIT>' , '<STR_LIT:a>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:b>' ) , <EOL> ] <EOL> class TestQuerystring : <EOL> def test_query_string_params_in_url ( self , httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT:GET>' , httpbin . url + '<STR_LIT>' ) <EOL> path = '<STR_LIT>' <EOL> url = httpbin . url + path <EOL> assert HTTP_OK in r <EOL> assert '<STR_LIT>' % path in r <EOL> assert '<STR_LIT>' % url in r <EOL> def test_query_string_params_items ( self , httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT:GET>' , httpbin . url + '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = '<STR_LIT>' <EOL> url = httpbin . url + path <EOL> assert HTTP_OK in r <EOL> assert '<STR_LIT>' % path in r <EOL> assert '<STR_LIT>' % url in r <EOL> def test_query_string_params_in_url_and_items_with_duplicates ( self , <EOL> httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT:GET>' , <EOL> httpbin . url + '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = '<STR_LIT>' <EOL> url = httpbin . url + path <EOL> assert HTTP_OK in r <EOL> assert '<STR_LIT>' % path in r <EOL> assert '<STR_LIT>' % url in r <EOL> class TestLocalhostShorthand : <EOL> def test_expand_localhost_shorthand ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT::>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_expand_localhost_shorthand_with_slash ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_expand_localhost_shorthand_with_port ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_expand_localhost_shorthand_with_path ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_expand_localhost_shorthand_with_port_and_slash ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_expand_localhost_shorthand_with_port_and_path ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_dont_expand_shorthand_ipv6_as_shorthand ( self ) : <EOL> args = parser . parse_args ( args = [ '<STR_LIT>' ] , env = TestEnvironment ( ) ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_dont_expand_longer_ipv6_as_shorthand ( self ) : <EOL> args = parser . parse_args ( <EOL> args = [ '<STR_LIT>' ] , <EOL> env = TestEnvironment ( ) <EOL> ) <EOL> assert args . url == '<STR_LIT>' <EOL> def test_dont_expand_full_ipv6_as_shorthand ( self ) : <EOL> args = parser . parse_args ( <EOL> args = [ '<STR_LIT>' ] , <EOL> env = TestEnvironment ( ) <EOL> ) <EOL> assert args . url == '<STR_LIT>' <EOL> class TestArgumentParser : <EOL> def setup_method ( self , method ) : <EOL> self . parser = input . HTTPieArgumentParser ( ) <EOL> def test_guess_when_method_set_and_valid ( self ) : <EOL> self . parser . args = argparse . Namespace ( ) <EOL> self . parser . args . method = '<STR_LIT:GET>' <EOL> self . parser . args . url = '<STR_LIT>' <EOL> self . parser . args . items = [ ] <EOL> self . parser . args . ignore_stdin = False <EOL> self . parser . env = TestEnvironment ( ) <EOL> self . parser . _guess_method ( ) <EOL> assert self . parser . args . method == '<STR_LIT:GET>' <EOL> assert self . parser . args . url == '<STR_LIT>' <EOL> assert self . parser . args . items == [ ] <EOL> def test_guess_when_method_not_set ( self ) : <EOL> self . parser . args = argparse . Namespace ( ) <EOL> self . parser . args . method = None <EOL> self . parser . args . url = '<STR_LIT>' <EOL> self . parser . args . items = [ ] <EOL> self . parser . args . ignore_stdin = False <EOL> self . parser . env = TestEnvironment ( ) <EOL> self . parser . _guess_method ( ) <EOL> assert self . parser . args . method == '<STR_LIT:GET>' <EOL> assert self . parser . args . url == '<STR_LIT>' <EOL> assert self . parser . args . items == [ ] <EOL> def test_guess_when_method_set_but_invalid_and_data_field ( self ) : <EOL> self . parser . args = argparse . Namespace ( ) <EOL> self . parser . args . method = '<STR_LIT>' <EOL> self . parser . args . url = '<STR_LIT>' <EOL> self . parser . args . items = [ ] <EOL> self . parser . args . ignore_stdin = False <EOL> self . parser . env = TestEnvironment ( ) <EOL> self . parser . _guess_method ( ) <EOL> assert self . parser . args . method == '<STR_LIT:POST>' <EOL> assert self . parser . args . url == '<STR_LIT>' <EOL> assert self . parser . args . items == [ <EOL> KeyValue ( key = '<STR_LIT:data>' , <EOL> value = '<STR_LIT>' , <EOL> sep = '<STR_LIT:=>' , <EOL> orig = '<STR_LIT>' ) <EOL> ] <EOL> def test_guess_when_method_set_but_invalid_and_header_field ( self ) : <EOL> self . parser . args = argparse . Namespace ( ) <EOL> self . parser . args . method = '<STR_LIT>' <EOL> self . parser . args . url = '<STR_LIT>' <EOL> self . parser . args . items = [ ] <EOL> self . parser . args . ignore_stdin = False <EOL> self . parser . env = TestEnvironment ( ) <EOL> self . parser . _guess_method ( ) <EOL> assert self . parser . args . method == '<STR_LIT:GET>' <EOL> assert self . parser . args . url == '<STR_LIT>' <EOL> assert self . parser . args . items , [ <EOL> KeyValue ( key = '<STR_LIT:test>' , <EOL> value = '<STR_LIT>' , <EOL> sep = '<STR_LIT::>' , <EOL> orig = '<STR_LIT>' ) <EOL> ] <EOL> def test_guess_when_method_set_but_invalid_and_item_exists ( self ) : <EOL> self . parser . args = argparse . Namespace ( ) <EOL> self . parser . args . method = '<STR_LIT>' <EOL> self . parser . args . url = '<STR_LIT>' <EOL> self . parser . args . items = [ <EOL> KeyValue ( <EOL> key = '<STR_LIT>' , value = '<STR_LIT:b>' , sep = '<STR_LIT:=>' , orig = '<STR_LIT>' ) <EOL> ] <EOL> self . parser . args . ignore_stdin = False <EOL> self . parser . env = TestEnvironment ( ) <EOL> self . parser . _guess_method ( ) <EOL> assert self . parser . args . items , [ <EOL> KeyValue ( key = '<STR_LIT>' , value = '<STR_LIT:a>' , sep = '<STR_LIT:=>' , orig = '<STR_LIT>' ) , <EOL> KeyValue ( <EOL> key = '<STR_LIT>' , value = '<STR_LIT:b>' , sep = '<STR_LIT:=>' , orig = '<STR_LIT>' ) , <EOL> ] <EOL> class TestNoOptions : <EOL> def test_valid_no_options ( self , httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:GET>' , httpbin . url + '<STR_LIT>' ) <EOL> assert '<STR_LIT>' not in r <EOL> def test_invalid_no_options ( self , httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT:GET>' , httpbin . url + '<STR_LIT>' , <EOL> error_exit_ok = True ) <EOL> assert r . exit_status == <NUM_LIT:1> <EOL> assert '<STR_LIT>' in r . stderr <EOL> assert '<STR_LIT>' not in r <EOL> class TestIgnoreStdin : <EOL> def test_ignore_stdin ( self , httpbin ) : <EOL> with open ( FILE_PATH ) as f : <EOL> env = TestEnvironment ( stdin = f , stdin_isatty = False ) <EOL> r = http ( '<STR_LIT>' , '<STR_LIT>' , httpbin . url + '<STR_LIT>' , <EOL> env = env ) <EOL> assert HTTP_OK in r <EOL> assert '<STR_LIT>' in r , "<STR_LIT>" <EOL> assert FILE_CONTENT not in r , "<STR_LIT>" <EOL> def test_ignore_stdin_cannot_prompt_password ( self , httpbin ) : <EOL> r = http ( '<STR_LIT>' , '<STR_LIT>' , httpbin . url + '<STR_LIT>' , <EOL> error_exit_ok = True ) <EOL> assert r . exit_status == ExitStatus . ERROR <EOL> assert '<STR_LIT>' in r . stderr <EOL> class TestSchemes : <EOL> def test_custom_scheme ( self ) : <EOL> with pytest . raises ( InvalidSchema ) : <EOL> http ( '<STR_LIT>' ) </s>
