コード例 #1
0
def test_httpie_args_post():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.options['--form'] = None
    c.body_params.update({'name': 'Jane Doe', 'email': '*****@*****.**'})

    args = c.httpie_args('post')
    assert args == [
        '--form',
        'POST',
        'http://localhost/things',
        '[email protected]',
        "name=Jane Doe",
        'Accept:text/html',
        "Authorization:ApiKey 1234",
    ]

    # Test quote option
    args = c.httpie_args('post', quote=True)
    assert args == [
        '--form',
        'POST',
        'http://localhost/things',
        '[email protected]',
        "'name=Jane Doe'",
        'Accept:text/html',
        "'Authorization:ApiKey 1234'",
    ]
コード例 #2
0
ファイル: test_context.py プロジェクト: YComputer/http-prompt
def test_eq():
    c1 = Context('http://localhost')
    c2 = Context('http://localhost')
    assert c1 == c2

    c1.options['--verify'] = 'no'
    assert c1 != c2
コード例 #3
0
ファイル: test_context.py プロジェクト: YComputer/http-prompt
def test_httpie_args_post():
    c = Context('http://localhost/things')
    c.headers.update({
        'Authorization': 'ApiKey 1234',
        'Accept': 'text/html'
    })
    c.options['--form'] = None
    c.body_params.update({
        'name': 'Jane Doe',
        'email': '*****@*****.**'
    })

    args = c.httpie_args('post')
    assert args == [
        '--form', 'POST', 'http://localhost/things',
        '[email protected]', "name=Jane Doe",
        'Accept:text/html', "Authorization:ApiKey 1234",
    ]

    # Test quote option
    args = c.httpie_args('post', quote=True)
    assert args == [
        '--form', 'POST', 'http://localhost/things',
        '[email protected]', "'name=Jane Doe'",
        'Accept:text/html', "'Authorization:ApiKey 1234'",
    ]
コード例 #4
0
ファイル: test_context.py プロジェクト: bahaaldine/api-prompt
def test_update():
    c1 = Context('http://localhost')
    c1.headers['Accept'] = 'application/json'
    c1.querystring_params['flag'] = '1'
    c1.body_params.update({
        'name': 'John Doe',
        'email': '*****@*****.**'
    })

    c2 = Context('http://example.com')
    c2.headers['Content-Type'] = 'text/html'
    c2.body_params['name'] = 'John Smith'

    c1.update(c2)

    assert c1.url == 'http://example.com'
    assert c1.headers == {
        'Accept': 'application/json',
        'Content-Type': 'text/html'
    }
    assert c1.querystring_params == {'flag': '1'}
    assert c1.body_params == {
        'name': 'John Smith',
        'email': '*****@*****.**'
    }
コード例 #5
0
ファイル: test_contextio.py プロジェクト: 3r1ccc/http-prompt
    def test_save_and_load(self):
        c = Context('http://example.com')
        c.headers['Authorization'] = 'apikey'
        c.querystring_params.update({
            'type': 'any',
            'offset': '100'
        })
        c.options.update({
            '--verify': 'no',
            '--style': 'default',
            '--follow': None
        })

        save_context(c)

        c2 = Context('http://example.com')
        load_context(c2)

        self.assertEqual(c2.headers, c.headers)
        self.assertEqual(c2.querystring_params, c.querystring_params)
        self.assertEqual(c2.body_params, c.body_params)

        # Make sure save_context() didn't save '--style'
        self.assertEqual(c2.options, {
            '--verify': 'no',
            '--follow': None
        })
コード例 #6
0
def test_eq():
    c1 = Context('http://localhost')
    c2 = Context('http://localhost')
    assert c1 == c2

    c1.options['--verify'] = 'no'
    assert c1 != c2
コード例 #7
0
    def test_env_and_source_non_ascii(self):
        c = Context()
        c.url = 'http://localhost:8000/api'
        c.headers.update({
            'Accept': 'text/csv',
            'Authorization': 'ApiKey 1234'
        })
        c.querystring_params.update({'page': ['1'], 'limit': ['50']})
        c.body_params.update({'name': '許 功蓋'})
        c.options.update({'--verify': 'no', '--form': None})

        c2 = c.copy()

        filename = self.make_tempfile()
        execute('env > %s' % filename, c)
        execute('rm *', c)

        self.assertFalse(c.headers)
        self.assertFalse(c.querystring_params)
        self.assertFalse(c.body_params)
        self.assertFalse(c.options)

        execute('source %s' % filename, c)

        self.assertEqual(c, c2)
コード例 #8
0
def test_httpie_args_get():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.querystring_params.update({'page': '2', 'limit': '10'})

    args = c.httpie_args('get')
    assert args == [
        'GET',
        'http://localhost/things',
        'limit==10',
        'page==2',
        'Accept:text/html',
        "Authorization:ApiKey 1234",
    ]
コード例 #9
0
ファイル: test_context.py プロジェクト: YComputer/http-prompt
def test_update():
    c1 = Context('http://localhost')
    c1.headers['Accept'] = 'application/json'
    c1.querystring_params['flag'] = '1'
    c1.body_params.update({
        'name': 'John Doe',
        'email': '*****@*****.**'
    })

    c2 = Context('http://example.com')
    c2.headers['Content-Type'] = 'text/html'
    c2.body_params['name'] = 'John Smith'

    c1.update(c2)

    assert c1.url == 'http://example.com'
    assert c1.headers == {
        'Accept': 'application/json',
        'Content-Type': 'text/html'
    }
    assert c1.querystring_params == {'flag': '1'}
    assert c1.body_params == {
        'name': 'John Smith',
        'email': '*****@*****.**'
    }
コード例 #10
0
    def execute_redirection(self, command):
        context = Context('http://httpbin.org')
        filename = self.make_tempfile()
        execute('%s > %s' % (command, filename), context)

        with open(filename, 'rb') as f:
            return f.read()
コード例 #11
0
    def test_save_and_load_context_non_ascii(self):
        c = Context('http://localhost')
        c.headers.update({
            'User-Agent': 'Ö',
            'Authorization': '中文'
        })
        save_context(c)

        c = Context('http://0.0.0.0')
        load_context(c)

        self.assertEqual(c.url, 'http://localhost')
        self.assertEqual(c.headers, {
            'User-Agent': 'Ö',
            'Authorization': '中文'
        })
コード例 #12
0
ファイル: test_context.py プロジェクト: YComputer/http-prompt
def test_httpie_args_get():
    c = Context('http://localhost/things')
    c.headers.update({
        'Authorization': 'ApiKey 1234',
        'Accept': 'text/html'
    })
    c.querystring_params.update({
        'page': '2',
        'limit': '10'
    })

    args = c.httpie_args('get')
    assert args == [
        'GET', 'http://localhost/things',
        'limit==10', 'page==2',
        'Accept:text/html', "Authorization:ApiKey 1234",
    ]
コード例 #13
0
def test_creation_with_longer_url():
    context = Context('http://example.com/a/b/c/index.html')
    assert context.url == 'http://example.com/a/b/c/index.html'
    assert context.options == {}
    assert context.headers == {}
    assert context.querystring_params == {}
    assert context.body_params == {}
    assert not context.should_exit
コード例 #14
0
    def setUp(self):
        self.listener = ExecutionListener({})

        self.response = Response()
        self.response.cookies.update({'username': '******', 'sessionid': 'abcd'})

        self.context = Context('http://localhost')
        self.context.headers['Cookie'] = 'name="John Doe"; sessionid=xyz'
コード例 #15
0
def test_format_raw_json_string_to_http_prompt():
    c = Context('http://localhost/things')
    c.body_json_params.update({
        'bar': 'baz',
    })

    output = t.format_to_http_prompt(c)
    assert output == ("cd http://localhost/things\n" "bar:='\"baz\"'\n")
コード例 #16
0
def test_creation():
    context = Context('http://example.com')
    assert context.url == 'http://example.com'
    assert context.options == {}
    assert context.headers == {}
    assert context.querystring_params == {}
    assert context.body_params == {}
    assert not context.should_exit
コード例 #17
0
    def setUp(self):
        self.patchers = [
            ('httpie_main', patch('http_prompt.execution.httpie_main')),
            ('click', patch('http_prompt.execution.click')),
        ]
        for attr_name, patcher in self.patchers:
            setattr(self, attr_name, patcher.start())

        self.context = Context('http://localhost')
コード例 #18
0
ファイル: test_contextio.py プロジェクト: yabqiu/http-prompt
    def test_different_ports(self):
        c = Context('http://example.com')
        c.headers['Authorization'] = 'apikey'
        c.querystring_params.update({'type': 'any', 'offset': '100'})
        c.options.update({
            '--verify': 'no',
            '--style': 'default',
            '--follow': None
        })

        save_context(c)

        c2 = Context('http://example.com:8000')
        load_context(c2)

        self.assertFalse(c2.headers)
        self.assertFalse(c2.querystring_params)
        self.assertFalse(c2.body_params)
        self.assertFalse(c2.options)
コード例 #19
0
    def setUp(self):
        super(ExecutionTestCase, self).setUp()
        self.patchers = [('httpie_main',
                          patch('http_prompt.execution.httpie_main')),
                         ('echo_via_pager',
                          patch('http_prompt.output.click.echo_via_pager')),
                         ('secho', patch('http_prompt.execution.click.secho'))]
        for attr_name, patcher in self.patchers:
            setattr(self, attr_name, patcher.start())

        self.context = Context('http://localhost')
コード例 #20
0
def test_format_to_http_prompt_1():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.querystring_params.update({'page': '2', 'limit': '10'})

    output = t.format_to_http_prompt(c)
    assert output == ("cd http://localhost/things\n"
                      "limit==10\n"
                      "page==2\n"
                      "Accept:text/html\n"
                      "'Authorization:ApiKey 1234'\n")
コード例 #21
0
ファイル: test_contextio.py プロジェクト: yabqiu/http-prompt
    def test_save_and_load(self):
        c = Context('http://example.com')
        c.headers['Authorization'] = 'apikey'
        c.querystring_params.update({'type': 'any', 'offset': '100'})
        c.options.update({
            '--verify': 'no',
            '--style': 'default',
            '--follow': None
        })

        save_context(c)

        c2 = Context('http://example.com')
        load_context(c2)

        self.assertEqual(c2.headers, c.headers)
        self.assertEqual(c2.querystring_params, c.querystring_params)
        self.assertEqual(c2.body_params, c.body_params)

        # Make sure save_context() didn't save '--style'
        self.assertEqual(c2.options, {'--verify': 'no', '--follow': None})
コード例 #22
0
ファイル: test_context.py プロジェクト: yabqiu/http-prompt
def test_json_serializing():
    c = Context('http://example.com/api')
    c.headers.update({
        'Authorization': 'ApiKey 1234',
        'Accept': 'text/html'
    })
    c.options['--form'] = None
    c.body_params.update({
        'name': 'Jane Doe',
        'email': '*****@*****.**'
    })

    json_obj = c.json_obj()

    c2 = c.copy()
    c2.headers['Accept'] = 'application/json'
    c2.body_params['name'] = 'Jane Doe'
    assert c2 != c

    c2.load_from_json_obj(json_obj)
    assert c2 == c
コード例 #23
0
def test_spec():
    c = Context('http://localhost',
                spec={
                    'paths': {
                        '/users': {
                            'get': {
                                'parameters': [{
                                    'name': 'username',
                                    'in': 'path'
                                }, {
                                    'name': 'since',
                                    'in': 'query'
                                }, {
                                    'name': 'Accept'
                                }]
                            }
                        },
                        '/orgs/{org}': {
                            'get': {
                                'parameters': [{
                                    'name': 'org',
                                    'in': 'path'
                                }, {
                                    'name': 'featured',
                                    'in': 'query'
                                }, {
                                    'name': 'X-Foo',
                                    'in': 'header'
                                }]
                            }
                        }
                    }
                })
    assert c.url == 'http://localhost'

    root_children = list(sorted(c.root.children))
    assert len(root_children) == 2
    assert root_children[0].name == 'orgs'
    assert root_children[1].name == 'users'

    orgs_children = list(sorted(root_children[0].children))
    assert len(orgs_children) == 1

    org_children = list(sorted(list(orgs_children)[0].children))
    assert len(org_children) == 2
    assert org_children[0].name == 'X-Foo'
    assert org_children[1].name == 'featured'

    users_children = list(sorted(root_children[1].children))
    assert len(users_children) == 2
    assert users_children[0].name == 'Accept'
    assert users_children[1].name == 'since'
コード例 #24
0
def test_format_to_httpie_get():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.querystring_params.update({
        'page': '2',
        'limit': '10',
        'name': ['alice', 'bob bob']
    })

    output = t.format_to_httpie(c, method='get')
    assert output == ("http GET http://localhost/things "
                      "limit==10 name==alice 'name==bob bob' page==2 "
                      "Accept:text/html 'Authorization:ApiKey 1234'\n")
コード例 #25
0
ファイル: test_contextio.py プロジェクト: 3r1ccc/http-prompt
    def test_different_ports(self):
        c = Context('http://example.com')
        c.headers['Authorization'] = 'apikey'
        c.querystring_params.update({
            'type': 'any',
            'offset': '100'
        })
        c.options.update({
            '--verify': 'no',
            '--style': 'default',
            '--follow': None
        })

        save_context(c)

        c2 = Context('http://example.com:8000')
        load_context(c2)

        self.assertFalse(c2.headers)
        self.assertFalse(c2.querystring_params)
        self.assertFalse(c2.body_params)
        self.assertFalse(c2.options)
コード例 #26
0
def test_format_to_httpie_post():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.options.update({'--verify': 'no', '--form': None})
    c.body_params.update({
        'full name': 'Jane Doe',
        'email': '*****@*****.**'
    })

    output = t.format_to_httpie(c, method='post')
    assert output == ("http --form --verify=no POST http://localhost/things "
                      "[email protected] 'full name=Jane Doe' "
                      "Accept:text/html 'Authorization:ApiKey 1234'\n")
コード例 #27
0
def test_extract_args_for_httpie_main_post():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.options.update({'--verify': 'no', '--form': None})
    c.body_params.update({
        'full name': 'Jane Doe',
        'email': '*****@*****.**'
    })

    args = t.extract_args_for_httpie_main(c, method='post')
    assert args == [
        '--form', '--verify', 'no', 'POST', 'http://localhost/things',
        '[email protected]', 'full name=Jane Doe', 'Accept:text/html',
        'Authorization:ApiKey 1234'
    ]
コード例 #28
0
def test_extract_raw_json_args_for_httpie_main_post():
    c = Context('http://localhost/things')
    c.body_json_params.update({
        'enabled': True,
        'items': ['foo', 'bar'],
        'object': {
            'id': 10,
            'name': 'test'
        }
    })

    args = t.extract_args_for_httpie_main(c, method='post')
    assert args == [
        'POST', 'http://localhost/things', 'enabled:=true',
        'items:=["foo", "bar"]', 'object:={"id": 10, "name": "test"}'
    ]
コード例 #29
0
 def setUp(self):
     self.context = Context('http://localhost',
                            spec={
                                'paths': {
                                    '/users': {},
                                    '/users/{username}': {},
                                    '/users/{username}/events': {},
                                    '/users/{username}/orgs': {},
                                    '/orgs': {},
                                    '/orgs/{org}': {},
                                    '/orgs/{org}/events': {},
                                    '/orgs/{org}/members': {}
                                }
                            })
     self.completer = HttpPromptCompleter(self.context)
     self.completer_event = None
コード例 #30
0
def test_format_to_http_prompt_2():
    c = Context('http://localhost/things')
    c.headers.update({'Authorization': 'ApiKey 1234', 'Accept': 'text/html'})
    c.options.update({'--verify': 'no', '--form': None})
    c.body_params.update({
        'full name': 'Jane Doe',
        'email': '*****@*****.**'
    })

    output = t.format_to_http_prompt(c)
    assert output == ("--form\n"
                      "--verify=no\n"
                      "cd http://localhost/things\n"
                      "[email protected]\n"
                      "'full name=Jane Doe'\n"
                      "Accept:text/html\n"
                      "'Authorization:ApiKey 1234'\n")
コード例 #31
0
def test_override():
    """Parameters can be defined at path level
    """
    c = Context('http://localhost',
                spec={
                    'paths': {
                        '/users': {
                            'parameters': [{
                                'name': 'username',
                                'in': 'query'
                            }, {
                                'name': 'Accept',
                                'in': 'header'
                            }],
                            'get': {
                                'parameters': [{
                                    'name': 'custom1',
                                    'in': 'query'
                                }]
                            },
                            'post': {
                                'parameters': [
                                    {
                                        'name': 'custom2',
                                        'in': 'query'
                                    },
                                ]
                            },
                        },
                        '/orgs': {
                            'parameters': [{
                                'name': 'username',
                                'in': 'query'
                            }, {
                                'name': 'Accept',
                                'in': 'header'
                            }],
                            'get': {}
                        }
                    }
                })
    assert c.url == 'http://localhost'

    root_children = list(sorted(c.root.children))
    # one path
    assert len(root_children) == 2
    assert root_children[0].name == 'orgs'
    assert root_children[1].name == 'users'

    orgs_methods = list(sorted(list(root_children)[0].children))
    # path parameters are used even if no method parameter
    assert len(orgs_methods) == 2
    assert next(filter(lambda i: i.name == 'username', orgs_methods),
                None) is not None
    assert next(filter(lambda i: i.name == 'Accept', orgs_methods),
                None) is not None

    users_methods = list(sorted(list(root_children)[1].children))
    # path and methods parameters are merged
    assert len(users_methods) == 4
    assert next(filter(lambda i: i.name == 'username', users_methods),
                None) is not None
    assert next(filter(lambda i: i.name == 'custom1', users_methods),
                None) is not None
    assert next(filter(lambda i: i.name == 'custom2', users_methods),
                None) is not None
    assert next(filter(lambda i: i.name == 'Accept', users_methods),
                None) is not None
コード例 #32
0
ファイル: test_context.py プロジェクト: bahaaldine/api-prompt
def test_creation_with_longer_url():
    context = Context('http://example.com/a/b/c/index.html')
    assert context.url == 'http://example.com/a/b/c/index.html'
コード例 #33
0
ファイル: test_context.py プロジェクト: YComputer/http-prompt
def test_copy():
    c1 = Context('http://localhost')
    c2 = c1.copy()
    assert c1 == c2
    assert c1 is not c2
コード例 #34
0
def test_copy():
    c1 = Context('http://localhost')
    c2 = c1.copy()
    assert c1 == c2
    assert c1 is not c2
コード例 #35
0
ファイル: test_completer.py プロジェクト: yabqiu/http-prompt
 def setUp(self):
     self.context = Context()
     self.completer = HttpPromptCompleter(self.context)
     self.completer_event = None
コード例 #36
0
 def execute_pipe(self, command):
     context = Context('http://httpbin.org')
     execute(command, context)
コード例 #37
0
def test_extract_httpie_options():
    c = Context('http://localhost')
    c.options.update({'--verify': 'no', '--form': None})

    output = t._extract_httpie_options(c, excluded_keys=['--form'])
    assert output == ['--verify', 'no']