You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2026-04-24 19:53:55 +02:00
Issue #54 Method suggestion proposal
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
from httpie.cliparse import HTTPieArgumentParser, KeyValue
|
||||
from mock import Mock
|
||||
|
||||
__author__ = 'vladimir'
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class HTTPieArgumentParserTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.HTTPieArgumentParserStub = type(HTTPieArgumentParser.__name__, (HTTPieArgumentParser,), {})
|
||||
self.HTTPieArgumentParserStub.__init__ = lambda self: None
|
||||
self.httpie_argument_parser = self.HTTPieArgumentParserStub()
|
||||
|
||||
def test_suggest_when_method_set_and_valid(self):
|
||||
args = Mock()
|
||||
args.method = 'GET'
|
||||
args.url = 'http://example.com/'
|
||||
args.items = []
|
||||
|
||||
self.httpie_argument_parser.suggest_method(args)
|
||||
|
||||
self.assertEquals(args.method, 'GET')
|
||||
self.assertEquals(args.url, 'http://example.com/')
|
||||
self.assertEquals(args.items, [])
|
||||
|
||||
def test_suggest_when_method_not_set(self):
|
||||
args = Mock()
|
||||
args.method = None
|
||||
args.url = 'http://example.com/'
|
||||
args.items = []
|
||||
|
||||
self.httpie_argument_parser.suggest_method(args)
|
||||
|
||||
self.assertEquals(args.method, 'GET')
|
||||
self.assertEquals(args.url, 'http://example.com/')
|
||||
self.assertEquals(args.items, [])
|
||||
|
||||
def test_suggest_when_method_set_but_invalid_and_data_field(self):
|
||||
args = Mock()
|
||||
args.method = 'http://example.com/'
|
||||
args.url = 'data=field'
|
||||
args.items = []
|
||||
|
||||
self.httpie_argument_parser.suggest_method(args)
|
||||
|
||||
self.assertEquals(args.method, 'POST')
|
||||
self.assertEquals(args.url, 'http://example.com/')
|
||||
self.assertEquals(args.items, [KeyValue(key='data', value='field', sep='=', orig='data=field')])
|
||||
|
||||
def test_suggest_when_method_set_but_invalid_and_header_field(self):
|
||||
args = Mock()
|
||||
args.method = 'http://example.com/'
|
||||
args.url = 'test:header'
|
||||
args.items = []
|
||||
|
||||
self.httpie_argument_parser.suggest_method(args)
|
||||
|
||||
self.assertEquals(args.method, 'GET')
|
||||
self.assertEquals(args.url, 'http://example.com/')
|
||||
self.assertEquals(args.items, [KeyValue(key='test', value='header', sep=':', orig='test:header')])
|
||||
|
||||
def test_suggest_when_method_set_but_invalid_and_item_exists(self):
|
||||
args = Mock()
|
||||
args.method = 'http://example.com/'
|
||||
args.url = 'new_item=a'
|
||||
args.items = [KeyValue(key='old_item', value='b', sep='=', orig='old_item=b')]
|
||||
|
||||
self.httpie_argument_parser.suggest_method(args)
|
||||
|
||||
self.assertEquals(args.items, [
|
||||
KeyValue(key='new_item', value='a', sep='=', orig='new_item=a'),
|
||||
KeyValue(key='old_item', value='b', sep='=', orig='old_item=b'),
|
||||
])
|
||||
+32
-3
@@ -144,11 +144,40 @@ class TestHTTPie(BaseTest):
|
||||
self.assertIn('"User-Agent": "HTTPie', response)
|
||||
self.assertIn('"Foo": "bar"', response)
|
||||
|
||||
def test_get_suggestion(self):
|
||||
|
||||
|
||||
class TestHTTPieSuggestion(BaseTest):
|
||||
def test_get(self):
|
||||
http('http://httpbin.org/get')
|
||||
|
||||
def test_post_suggestion(self):
|
||||
http('http://httpbin.org/post', 'hello=world')
|
||||
def test_post(self):
|
||||
r = http('http://httpbin.org/post', 'hello=world')
|
||||
self.assertIn('"hello": "world"', r)
|
||||
|
||||
def test_verbose(self):
|
||||
r = http('--verbose', 'http://httpbin.org/get', 'test-header:__test__')
|
||||
self.assertEqual(r.count('__test__'), 2)
|
||||
|
||||
def test_verbose_form(self):
|
||||
r = http('--verbose', '--form', 'http://httpbin.org/post', 'foo=bar', 'baz=bar')
|
||||
self.assertIn('foo=bar&baz=bar', r)
|
||||
|
||||
def test_json(self):
|
||||
response = http('http://httpbin.org/post', 'foo=bar')
|
||||
self.assertIn('"foo": "bar"', response)
|
||||
response2 = http('-j', 'GET', 'http://httpbin.org/headers')
|
||||
self.assertIn('"Accept": "application/json"', response2)
|
||||
response3 = http('-j', 'GET', 'http://httpbin.org/headers', 'Accept:application/xml')
|
||||
self.assertIn('"Accept": "application/xml"', response3)
|
||||
|
||||
def test_form(self):
|
||||
response = http('--form', 'http://httpbin.org/post', 'foo=bar')
|
||||
self.assertIn('"foo": "bar"', response)
|
||||
|
||||
def test_headers(self):
|
||||
response = http('http://httpbin.org/headers', 'Foo:bar')
|
||||
self.assertIn('"User-Agent": "HTTPie', response)
|
||||
self.assertIn('"Foo": "bar"', response)
|
||||
|
||||
|
||||
class TestPrettyFlag(BaseTest):
|
||||
|
||||
Reference in New Issue
Block a user