HTTP request/response assertion plugin for grappa.
grappa-http extends grappa assertion operators with HTTP protocol testing.
To get started, take a look to the documentation, tutorial and examples.
importpookimportrequestsfromgrappa_httpimportshould# Activate the HTTP mock enginepook.on()
# Register a sample mockpook.get('server.org/foo?bar=baz', reply=200,
response_headers={'Server': 'nginx'},
response_json={'foo': 'bar'})
# Perform HTTP requestres=requests.get('http://server.org/foo?bar=baz')
# Test response status to be OKres|should.be.ok# Or alternatively using the status coderes|should.have.status(200)
# Test request URLres|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 typeres|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 bodyres|should.have.body.equal.to('{\n "foo": "bar"\n}')
res|should.have.body.that.contains('foo')
# Test response body lengthres|should.have.body.length.of(20)
res|should.have.body.length.higher.than(10)
# Test response JSON bodyres|should.have.json.equal.to({'foo': 'bar'})
res|should.have.json.have.key('foo') >should.be.equal.to('bar')
# Validate response JSON bodies using JSONSchemares|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'
}
}
})Full-featured error report example:
Traceback (mostrecentcalllast):
File"grappa-http/tests/http_test.py", line38, intest_http_tutorialres|should.have.body.equal.to('{\n "foo": "baa"\n}')
File"grappa/grappa/test.py", line208, in__ror__returnself.__overload__(value)
File"grappa/grappa/test.py", line196, in__overload__returnself.__call__(subject, overload=True)
File"grappa/grappa/test.py", line73, in__call__returnself._trigger() ifoverloadelseTest(subject)
File"grappa/grappa/test.py", line113, in_triggerraiseerrAssertionError: Oops! Somethingwentwrong!
Thefollowingassertionwasnotsatisfiedsubject"{\n "foo": "bar"\n}"shouldhavebodyequalto"{\n "foo": "baa"\n}"Whatweexpectedaresponsebodydataequalto:
{
"foo": "baa"
}
Whatwegotinsteadaresponsebodywithdata:
{
"foo": "bar"
}
Differencecomparison> {
>-"foo": "bar"> ? ^>+"foo": "baa"> ? ^> }
WhereFile"grappa-http/tests/http_test.py", line38, intest_http_tutorial30|res|should.have.content('json')
31|32|# Test response headers33| (res| (should.have.header('Content-Type')
34| .that.should.be.equal('application/json')))
35|res|should.have.header('Server').that.should.contain('nginx')
36|37|# Test response body38|>res|should.have.body.equal.to('{\n "foo": "baa"\n}')
39|res|should.have.body.that.contains('foo')
40|41|# Test response body length42|res|should.have.body.length.of(20)
43|res|should.have.body.length.higher.than(10)
44|45|# Test response JSON body- Full-featured HTTP response assertions.
- Supports any protocol primitive assertions.
- First-class support for JSON body assertion.
- Built-in JSONSchema validation.
- Full-features request URL validation.
- Featured regular expression based assertion.
- Works with
requestsandaiohttpHTTP clients. - Friendly and detailed assertion error reporting with body diff comparisons.
- Provides both
expectandshouldassertion styles. - Testing framework agnostic. Works with
unittest,nosetests,pytest,behave... - Works with Python 2.6+, 3+, PyPy and possibly other Python implementations.
Using pip package manager:
pip install --upgrade grappa-httpOr install the latest sources from Github:
pip install -e git+git://github.com/grappa-py/http.git#egg=grappa