Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active December 10, 2015 12:28
Show Gist options
  • Save voluntas/4434698 to your computer and use it in GitHub Desktop.
Save voluntas/4434698 to your computer and use it in GitHub Desktop.
import requests
class Spam(object):
def __init__(self, response):
self.response = response
def expect_status_code(self, status_code):
assert self.response.status_code == status_code
def expect_header(self, key, content):
assert self.response.headers.get(key) == content
def expect_header_contains(self, key, content):
assert self.response.headers.has_key(key)
assert content in self.response.headers.get(key)
def expect_json(self, json):
assert self.response.json == json
def expect_json_types(self, json):
pass
def expect_body_contains(self, content):
assert self.response.text == content
def expect_json_length(self, length):
pass
def head(url, **kwargs):
return Spam(requests.head(url, **kwargs))
def get(url, **kwargs):
return Spam(requests.get(url, **kwargs))
def post(url, data=None, **kwargs):
return Spam(requests.post(url, data, **kwargs))
def put(url, data=None, **kwargs):
return Spam(requests.post(url, data, **kwargs))
def delete(url, **kwargs):
return Spam(requests.delete(url, **kwargs))
def options(url, **kwargs):
return Spam(requests.options(url, **kwargs))
def patch(url, data=None, **kwargs):
return Spam(requests.patch(url, data, **kwargs))
import spam
def test_users_keylist():
"""
$ http GET localhost:8080/users/
HTTP/1.1 200 OK
Content-Length: 21
Content-Type: application/json
Date: Sat, 29 Dec 2012 07:42:32 GMT
Server: MochiWeb/1.1 WebMachine/1.9.2 (someone had painted it blue)
{
"keys": [
"voluntas"
]
}
"""
s = spam.get('http://localhost:8000/users/')
s.expect_status_code(200)
s.expect_header('Content-Type', 'application/json')
s.expect_header_contain('Content-Type', 'json')
s.expect_json({'keys': ['voluntas']})
def test_users_get():
"""
$ http GET localhost:8080/users/voluntas
HTTP/1.1 200 OK
Content-Length: 40
Content-Type: application/json
Date: Sat, 29 Dec 2012 07:42:34 GMT
Server: MochiWeb/1.1 WebMachine/1.9.2 (someone had painted it blue)
{
"key": "voluntas",
"value": "v"
}
"""
s = spam.get('http://localhost:8000/users/voluntas')
s.expect_status_code(200)
s.expect_header('Content-Type', 'application/json')
s.expect_json({'key': 'voluntas', 'value': 'v'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment