-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp_test.py
61 lines (49 loc) · 1.8 KB
/
http_test.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
import pook
import requests
from grappa_http import should
def test_http_tutorial():
# Activate the HTTP mock engine
pook.on()
# Register a sample mock
pook.get('server.org/foo?bar=baz', reply=200,
response_headers={'Server': 'nginx'},
response_json={'foo': 'bar'})
# Perform HTTP request
res = requests.get('http://server.org/foo?bar=baz')
# Test response status to be OK
res | should.be.ok
# Or alternatively using the status code
res | should.have.status(200)
# Test request URL
res | should.have.url.hostname('server.org')
res | should.have.url.port(80)
res | should.have.url.path('/foo')
res | should.have.url.query.params({'bar': 'baz'})
# Test response body MIME content type
res | should.have.content('json')
# Test response headers
(res | (should.have.header('Content-Type')
.that.should.be.equal('application/json')))
res | should.have.header('Server').that.should.contain('nginx')
# Test response body
res | should.have.body.equal.to('{\n "foo": "bar"\n}')
res | should.have.body.that.contains('foo')
# Test response body length
res | should.have.body.length.of(20)
res | should.have.body.length.higher.than(10)
# Test response JSON body
res | should.have.json.equal.to({'foo': 'bar'})
res | should.have.json.have.key('foo') > should.be.equal.to('bar')
# Validate response JSON bodies using JSONSchema
res | should.implement.jsonschema({
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Response JSON',
'type': 'object',
'required': ['foo'],
'properties': {
'foo': {
'description': 'foo always means foo',
'type': 'string'
}
}
})