-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
75 lines (61 loc) · 2.21 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from klein import Klein
from twisted.internet import defer
from twisted.trial.unittest import TestCase
from txwebtest import TestClient
from urlparse import parse_qs
class Tests(TestCase):
def setUp(self):
self.app = TestClient(create_app().resource())
@defer.inlineCallbacks
def test_status_check(self):
yield self.app.get('/names/4', status=404)
try:
yield self.app.get('/names/4', status=200)
self.fail()
except AssertionError:
pass
@defer.inlineCallbacks
def test_post_with_body(self):
resp = yield self.app.post('/names', 'name=Ann', status=201)
new_item_path = resp.get_header('Location')
resp = yield self.app.get(new_item_path, status=200)
assert resp.text == 'Ann'
@defer.inlineCallbacks
def test_put_with_body(self):
yield self.app.put('/names/4', 'name=Ann', status=200)
resp = yield self.app.get('/names/4', status=200)
assert resp.text == 'Ann'
@defer.inlineCallbacks
def test_delete(self):
yield self.app.put('/names/4', 'name=Ann', status=200)
yield self.app.delete('/names/4', status=200)
yield self.app.get('/names/4', status=404)
def create_app():
''' A simple Klein app that associates ints with names. '''
app = Klein()
results = {}
@app.route('/names', methods=['POST'])
def post(request):
name = request.args['name'][0]
item_id = max(results.keys()) + 1 if results else 1
results[item_id] = name
request.setHeader('Location', '/names/%s' % item_id)
request.setResponseCode(201)
return name
@app.route('/names/<int:item_id>', methods=['GET'])
def get(request, item_id):
try:
return results[item_id]
except KeyError:
request.setResponseCode(404)
@app.route('/names/<int:item_id>', methods=['PUT'])
def put(request, item_id):
data = request.content.read()
args = parse_qs(data)
name = args['name'][0]
results[item_id] = name
return ''
@app.route('/names/<int:item_id>', methods=['DELETE'])
def delete(request, item_id):
results.pop(item_id, None)
return app