saritasa/openapi-httpfoundation-testing

Strengthen your API tests by validating HttpFoundation responses against OpenAPI (3.0.x) definitions

1.0.1 2021-01-25 14:55 UTC

This package is auto-updated.

Last update: 2024-04-25 21:48:36 UTC


README

Latest Stable Version License Build Status Scrutinizer Code Quality

Strengthen your API tests by validating HttpFoundation responses against OpenAPI (3.0.x) definitions.

See this article for more details, and this repository for an example use in a Laravel project.

Why?

OpenAPI is a specification intended to describe RESTful APIs in a way that is understood by humans and machines alike.

By validating an API's responses against the OpenAPI definition that describes it, we guarantee that the API's behaviour conforms to the documentation we provide, thus making the OpenAPI definition the single source of truth.

The HttpFoundation component is developed and maintained as part of the Symfony framework. It is used to handle HTTP requests and responses in projects such as Symfony, Laravel, Drupal, and many other major industry players (see the extended list).

How does it work?

This package is built upon the OpenAPI PSR-7 Message Validator package, which validates PSR-7 messages against OpenAPI definitions.

It essentially converts HttpFoundation response objects to PSR-7 messages using Symfony's PSR-7 Bridge and Tobias Nyholm's PSR-7 implementation, before passing them on to the OpenAPI PSR-7 Message Validator.

Install

Via Composer:

$ composer require --dev osteel/openapi-httpfoundation-testing

💡 This package is meant to be used for development only, as part of your API test suite.

Usage

First, import the builder in the class that will perform the validation:

use Osteel\OpenApi\Testing\ResponseValidatorBuilder;

Use the builder to create a Osteel\OpenApi\Testing\ResponseValidator object, feeding it a YAML or JSON OpenAPI definition:

$validator = ResponseValidatorBuilder::fromYaml('my-definition.yaml')->getValidator();

// or

$validator = ResponseValidatorBuilder::fromJson('my-definition.json')->getValidator();

💡 Instead of a file, you can also pass a YAML or JSON string directly.

You can now validate a Symfony\Component\HttpFoundation\Response object for a given path and method:

$validator->validate('/users', 'post', $response);

💡 For convenience, responses implementing Psr\Http\Message\ResponseInterface are also accepted.

In the example above, we check that the response matches the OpenAPI definition for a POST request on the /users path.

Each of OpenAPI's supported HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS and TRACE) also has a shortcut method that calls validate under the hood, meaning the line above could also be written this way:

$validator->post('/users', $response);

The validate method returns true in case of success, and throws PSR-7 message-related exceptions from the underlying OpenAPI PSR-7 Message Validator package in case of error.

Change log

Please see the Releases section for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Credits

People

Special thanks to Pavel Batanov for his advice on structuring the package.

Packages

License

The MIT License (MIT). Please see License File for more information.