summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-12-15 16:56:33 +0200
committerLars Wirzenius <liw@liw.fi>2017-12-15 16:56:33 +0200
commit4cb311fe68585f3286e7ec013e1c82c242d9310d (patch)
treea958c9ae3d1bfe0e1ff68c291a8bc5ce600a4176
parentb5b953029a26e1e8701d75149581497bdcf26a96 (diff)
parent04c80ff3559577ea48e71b14b39df350f937f24a (diff)
downloadick2-4cb311fe68585f3286e7ec013e1c82c242d9310d.tar.gz
Merge: yarn implements cleanups
-rw-r--r--yarns/900-implements.yarn72
-rw-r--r--yarns/lib.py16
2 files changed, 26 insertions, 62 deletions
diff --git a/yarns/900-implements.yarn b/yarns/900-implements.yarn
index afddb3b..1a42198 100644
--- a/yarns/900-implements.yarn
+++ b/yarns/900-implements.yarn
@@ -34,76 +34,45 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
path = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = get(url + path, token)
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, get, url + path, token=token)
IMPLEMENTS WHEN (\S+) retrieves (\S+) from blob service
user = get_next_match()
path = get_next_match()
token = get_token(user)
url = vars['bsurl']
- status, content_type, headers, body = get_blob(url + path, token)
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body.encode('hex')
+ http(vars, get_blob, url + path, token=token)
IMPLEMENTS WHEN (\S+) makes request GET (\S+) with an invalid token
user = get_next_match()
path = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = get(url + path, 'invalid')
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, get, url + path, token='invalid')
IMPLEMENTS WHEN (\S+) makes request POST (\S+) with a valid token and body (.+)
user = get_next_match()
path = get_next_match()
- body_text = get_next_match()
- print('path', path)
- print('body', body_text)
+ body = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = post(url + path, body_text, token)
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, post, url + path, body=body, token=token)
IMPLEMENTS WHEN (\S+) makes request POST (\S+) with an invalid token and body (.+)
user = get_next_match()
path = get_next_match()
- body_text = get_next_match()
- print('path', path)
- print('body', body_text)
+ body = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = post(url + path, body_text, 'invalid')
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body.encode('hex')
+ http(vars, post, url + path, body=body, token='invalid')
IMPLEMENTS WHEN (\S+) makes request PUT (\S+) with a valid token and body (.+)
user = get_next_match()
path = get_next_match()
- body_text = get_next_match()
- print('user', user)
- print('path', path)
- print('body', body_text)
+ body = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = put(url + path, body_text, token)
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, put, url + path, body=body, token=token)
IMPLEMENTS WHEN (\S+) sends blob (\S+) to blob service as (\S+)
user = get_next_match()
@@ -112,34 +81,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
body = cat(filename)
token = get_token(user)
url = vars['bsurl']
- status, content_type, headers, body = put_blob(url + path, body, token)
- vars['status_code'] = status
+ http(vars, put_blob, url + path, body=body, token=token)
IMPLEMENTS WHEN (\S+) makes request PUT (\S+) with an invalid token
user = get_next_match()
path = get_next_match()
- body_text = '{}'
- print('user', user)
- print('path', path)
- print('body', body_text)
+ body = '{}'
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = put(url + path, body_text, 'invalid')
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, put, url + path, body=body, token='invalid')
IMPLEMENTS WHEN (\S+) makes request DELETE (\S+)
user = get_next_match()
path = get_next_match()
token = get_token(user)
url = vars['url']
- status, content_type, headers, body = delete(url + path, token)
- vars['status_code'] = status
- vars['content_type'] = content_type
- vars['headers'] = headers
- vars['body'] = body
+ http(vars, delete, url + path, token=token)
## HTTP response inspection
@@ -150,7 +107,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
IMPLEMENTS THEN body matches (.+)
expected_text = get_next_match()
expected = json.loads(expected_text)
- print('actual body', repr(vars['body']))
actual = json.loads(vars['body'])
assertEqual(expected, actual)
@@ -162,7 +118,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
IMPLEMENTS THEN body is the same as the blob (\S+)
filename = get_next_match()
blob = cat(filename)
- body = vars['body'].decode('hex')
+ body = vars['body']
assertEqual(body, blob)
IMPLEMENTS THEN version in body matches version from setup.py
diff --git a/yarns/lib.py b/yarns/lib.py
index 6575e6d..b19ccf3 100644
--- a/yarns/lib.py
+++ b/yarns/lib.py
@@ -96,6 +96,14 @@ def get_token(user):
return cat(filename)
+def http(vars, func, url, **kwargs):
+ status, content_type, headers, body = func(url, **kwargs)
+ vars['status_code'] = status
+ vars['content_type'] = content_type
+ vars['headers'] = headers
+ vars['body'] = body
+
+
def get(url, token):
headers = {
'Authorization': 'Bearer {}'.format(token),
@@ -112,21 +120,21 @@ def get_blob(url, token):
return r.status_code, r.headers['Content-Type'], dict(r.headers), r.content
-def post(url, body_text, token):
+def post(url, body, token):
headers = {
'Authorization': 'Bearer {}'.format(token),
'Content-Type': 'application/json',
}
- r = requests.post(url, headers=headers, data=body_text, verify=False)
+ r = requests.post(url, headers=headers, data=body, verify=False)
return r.status_code, r.headers['Content-Type'], dict(r.headers), r.text
-def put(url, body_text, token):
+def put(url, body, token):
headers = {
'Authorization': 'Bearer {}'.format(token),
'Content-Type': 'application/json',
}
- r = requests.put(url, headers=headers, data=body_text, verify=False)
+ r = requests.put(url, headers=headers, data=body, verify=False)
return r.status_code, r.headers['Content-Type'], dict(r.headers), r.text