Skip to main content
grappa logo

Build Status PyPI Coverage Status Documentation Status Stability Code Climate Python Versions Say Thanks

About

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.

Status

grappa-http is still beta quality software.

Showcase

import pook
import requests
from grappa_http import should

# 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'
        }
    }
})

Full-featured error report example:

Traceback (most recent call last):
  File "grappa-http/tests/http_test.py", line 38, in test_http_tutorial
    res | should.have.body.equal.to('{\n    "foo": "baa"\n}')
  File "grappa/grappa/test.py", line 208, in __ror__
    return self.__overload__(value)
  File "grappa/grappa/test.py", line 196, in __overload__
    return self.__call__(subject, overload=True)
  File "grappa/grappa/test.py", line 73, in __call__
    return self._trigger() if overload else Test(subject)
  File "grappa/grappa/test.py", line 113, in _trigger
    raise err
AssertionError: Oops! Something went wrong!

  The following assertion was not satisfied
    subject "{\n    "foo": "bar"\n}" should have body equal to "{\n    "foo": "baa"\n}"

  What we expected
    a response body data equal to:
        {
            "foo": "baa"
        }

  What we got instead
    a response body with data:
        {
            "foo": "bar"
        }

  Difference comparison
    >   {
    > -     "foo": "bar"
    > ?               ^
    > +     "foo": "baa"
    > ?               ^
    >   }

  Where
    File "grappa-http/tests/http_test.py", line 38, in test_http_tutorial

    30|       res | should.have.content('json')
    31|
    32|       # Test response headers
    33|       (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 body
    38| >     res | should.have.body.equal.to('{\n    "foo": "baa"\n}')
    39|       res | should.have.body.that.contains('foo')
    40|
    41|       # Test response body length
    42|       res | should.have.body.length.of(20)
    43|       res | should.have.body.length.higher.than(10)
    44|
    45|       # Test response JSON body

Features

  • 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 requests and aiohttp HTTP clients.

  • Friendly and detailed assertion error reporting with body diff comparisons.

  • Provides both expect and should assertion styles.

  • Testing framework agnostic. Works with unittest, nosetests, pytest, behave…

  • Works with Python 2.6+, 3+, PyPy and possibly other Python implementations.

Supported HTTP clients

Installation

Using pip package manager:

pip install --upgrade grappa-http

Or install the latest sources from Github:

pip install -e git+git://github.com/grappa-py/http.git#egg=grappa

History

v0.1.3 / 2017-04-26

  • fix(adapters): use issubclass for adapter valid interface.

v0.1.2 / 2017-04-25

  • feat(api): add use_adapter() API for custom adapter registering

  • feat(jsonschema): update example which uses satisfy attribute operator

v0.1.1 / 2017-03-29

  • feat(requirements): upgrade grappa to latest version

  • feat(operators): add output size limit

  • feat(docs): add thanks badge

  • refactor(docs): update description

  • feat(examples): add featured example

  • fix(package): read version from grappa_http package

  • fix(docs): use grappa_http package

0.1.0 (2017-03-25)

  • First version (beta)

Metadata

Release files for grappa-http 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grappa-http 0.1.3
File Size Uploaded
grappa-http-0.1.3.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grappa-http 0.1.3
File Interpreter ABI Platform
grappa_http-0.1.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 42.5 kB

Release files / grappa-http-0.1.3.tar.gz

Download URL grappa-http-0.1.3.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f08439ca8ecd4fad66b7faa55d831db7655c8abb99927f316873df3432b0d645
BLAKE2b-256 checksum
How to use checksums
885d0b215be0999ea3f86885395f8a7453b4e3e30c468a6d9dc38d7b1ad2e921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / grappa_http-0.1.3-py2.py3-none-any.whl

Download URL grappa_http-0.1.3-py2.py3-none-any.whl
Size 27.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4441dbeb7155d5bd2e43447bd98c9b7a408c2cd33e3841696915eeacd6fc0b67
BLAKE2b-256 checksum
How to use checksums
c875e9eedb777a5a662149c4d1bf5ccdc86c1bdbf4c48656d945fddc35e6b6eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page