Skip to content

Commit

Permalink
Multipart HTTP form
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Feb 28, 2021
1 parent a41627d commit f0d6ba7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 26 additions & 7 deletions tests/test_cli_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@


def test_http(socketclass_mock, requests_mock, dbfile_mock, httpcli):

# Simple Get
s, o, e = httpcli('get', 'foo.com')
assert e == ''
assert o == 'bar'
Expand All @@ -18,32 +20,49 @@ def test_http(socketclass_mock, requests_mock, dbfile_mock, httpcli):
sock.recvfrom.assert_called_once()
requests_mock.assert_called_with('GET', 'http://10.0.0.2')

# Url
s, o, e = httpcli('get', 'foo.com/bar')
assert s == 0
requests_mock.assert_called_with('GET', 'http://10.0.0.2/bar')

# Plain HTTP Content
s, o, e = httpcli('set', 'foo.com', '1')
assert s == 0
requests_mock.assert_called_with('SET', 'http://10.0.0.2', data='1')

s, o, e = httpcli('set', 'foo.com', 'bar=baz', '?qux=quux')
# HTTP Errors
requests_mock.return_value.status_code = 400
s, o, e = httpcli('get', 'foo.com')
assert s == 1


def test_http_urlencoded(socketclass_mock, requests_mock, dbfile_mock, httpcli):
s, o, e = httpcli('set', 'foo.com', 'bar=baz')
assert s == 0
requests_mock.assert_called_with(
'SET', 'http://10.0.0.2',
data=[('bar', 'baz')],
params=[('qux', 'quux')]
)

s, o, e = httpcli('set', 'foo.com', '@bar=baz.txt')
# With query string
s, o, e = httpcli('set', 'foo.com', 'bar=baz', '?qux=quux')
assert s == 0
requests_mock.assert_called_with(
'SET', 'http://10.0.0.2',
files=[('bar', 'baz.txt')],
data=[('bar', 'baz')],
params=[('qux', 'quux')]
)

requests_mock.return_value.status_code = 400
s, o, e = httpcli('get', 'foo.com')
assert s == 1

def test_http_multipart(socketclass_mock, requests_mock, dbfile_mock, httpcli):
barfile = mock.mock_open(read_data='baz content')
with mock.patch('uns.cli.open', barfile) as bar:
s, o, e = httpcli('set', 'foo.com', '@bar=baz.txt')
assert s == 0
requests_mock.assert_called_with(
'SET', 'http://10.0.0.2',
files=[('bar', bar.return_value)],
)


def test_http_binary_response(socketclass_mock, requests_mock, dbfile_mock,
Expand Down
2 changes: 1 addition & 1 deletion uns/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def __call__(self, args):
if k[0] == '?':
query.append((k[1:], v))
elif k[0] == '@':
files.append((k[1:], v))
files.append((k[1:], open(v)))
else:
fields.append((k, v))

Expand Down

0 comments on commit f0d6ba7

Please sign in to comment.