-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature storm httpapi params (#1856)
add params support to storm http library
- Loading branch information
Showing
3 changed files
with
123 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,41 +8,77 @@ async def test_storm_http_get(self): | |
addr, port = await core.addHttpsPort(0) | ||
root = await core.auth.getUserByName('root') | ||
await root.setPasswd('root') | ||
text = ''' | ||
$hdr = ( | ||
("User-Agent", "Storm HTTP Stuff"), | ||
) | ||
$url = $lib.str.format("https://root:[email protected]:{port}/api/v1/model", port=$port) | ||
|
||
for ($name, $fdef) in $lib.inet.http.get($url, headers=$hdr, ssl_verify=$(0)).json().result.forms { | ||
[ test:str=$name ] | ||
} | ||
core.addHttpApi('/api/v0/test', s_test.HttpReflector, {'cell': core}) | ||
url = f'https://root:[email protected]:{port}/api/v0/test' | ||
opts = {'vars': {'url': url}} | ||
|
||
# Header and params as dict | ||
q = ''' | ||
$params=$lib.dict(key=valu, foo=bar) | ||
$hdr = ( | ||
("User-Agent", "Storm HTTP Stuff"), | ||
) | ||
$resp = $lib.inet.http.get($url, headers=$hdr, params=$params, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
opts = {'vars': {'port': port}} | ||
nodes = await core.nodes(text, opts=opts) | ||
self.len(1, await core.nodes('test:str=inet:ipv4')) | ||
resp = await core.callStorm(q, opts=opts) | ||
data = resp.get('result') | ||
self.eq(data.get('params'), {'key': ('valu',), 'foo': ('bar',)}) | ||
self.eq(data.get('headers').get('User-Agent'), 'Storm HTTP Stuff') | ||
|
||
# params as list of key/value pairs | ||
q = ''' | ||
$params=((foo, bar), (key, valu)) | ||
$resp = $lib.inet.http.get($url, params=$params, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
resp = await core.callStorm(q, opts=opts) | ||
data = resp.get('result') | ||
self.eq(data.get('params'), {'key': ('valu',), 'foo': ('bar',)}) | ||
|
||
# params as a urlencoded string | ||
q = ''' | ||
$params="foo=bar&key=valu&foo=baz" | ||
$resp = $lib.inet.http.get($url, params=$params, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
resp = await core.callStorm(q, opts=opts) | ||
data = resp.get('result') | ||
self.eq(data.get('params'), {'key': ('valu',), 'foo': ('bar', 'baz')}) | ||
|
||
# Bad param | ||
q = ''' | ||
$params=(1138) | ||
$resp = $lib.inet.http.get($url, params=$params, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
msgs = await core.stormlist(q, opts=opts) | ||
self.stormIsInErr('Error during http get - Invalid query type', msgs) | ||
|
||
async def test_storm_http_request(self): | ||
|
||
async with self.getTestCore() as core: | ||
addr, port = await core.addHttpsPort(0) | ||
root = await core.auth.getUserByName('root') | ||
await root.setPasswd('root') | ||
text = ''' | ||
$hdr = ( | ||
core.addHttpApi('/api/v0/test', s_test.HttpReflector, {'cell': core}) | ||
url = f'https://root:[email protected]:{port}/api/v0/test' | ||
opts = {'vars': {'url': url}} | ||
q = ''' | ||
$params=$lib.dict(key=valu, foo=bar) | ||
$hdr = ( | ||
("User-Agent", "Storm HTTP Stuff"), | ||
) | ||
$url = $lib.str.format("https://root:[email protected]:{port}/api/v1/model", port=$port) | ||
for ($name, $fdef) in $lib.inet.http.request(GET, $url, headers=$hdr, ssl_verify=$(0)).json().result.forms { | ||
[ test:str=$name ] | ||
} | ||
) | ||
$resp = $lib.inet.http.request(GET, $url, headers=$hdr, params=$params, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
opts = {'vars': {'port': port}} | ||
nodes = await core.nodes(text, opts=opts) | ||
self.len(1, await core.nodes('test:str=inet:ipv4')) | ||
resp = await core.callStorm(q, opts=opts) | ||
data = resp.get('result') | ||
self.eq(data.get('params'), {'key': ('valu',), 'foo': ('bar',)}) | ||
self.eq(data.get('headers').get('User-Agent'), 'Storm HTTP Stuff') | ||
|
||
async def test_storm_http_post_api(self): | ||
async def test_storm_http_post(self): | ||
|
||
async with self.getTestCore() as core: | ||
addr, port = await core.addHttpsPort(0) | ||
|
@@ -73,6 +109,19 @@ async def test_storm_http_post_api(self): | |
self.len(1, nodes) | ||
self.assertIn('vertex', [u.name for u in core.auth.users()]) | ||
|
||
core.addHttpApi('/api/v0/test', s_test.HttpReflector, {'cell': core}) | ||
url = f'https://root:[email protected]:{port}/api/v0/test' | ||
opts = {'vars': {'url': url, 'buf': b'1234'}} | ||
q = ''' | ||
$params=$lib.dict(key=valu, foo=bar) | ||
$resp = $lib.inet.http.post($url, params=$params, body=$buf, ssl_verify=$lib.false) | ||
return ( $resp.json() ) | ||
''' | ||
resp = await core.callStorm(q, opts=opts) | ||
data = resp.get('result') | ||
self.eq(data.get('params'), {'key': ('valu',), 'foo': ('bar',)}) | ||
self.eq(data.get('body'), 'MTIzNA==') | ||
|
||
async def test_storm_http_post_file(self): | ||
|
||
async with self.getTestCore() as core: | ||
|
@@ -107,4 +156,4 @@ async def test_storm_http_post_file(self): | |
self.len(1, errs) | ||
err = errs[0] | ||
self.eq(err[0], 'StormRuntimeError') | ||
self.isin('Error during http post - data and json parameters can not be used at the same time', err[1].get('mesg')) | ||
self.isin('Error during http POST - data and json parameters can not be used at the same time', err[1].get('mesg')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters