osteel/openapi-httpfoundation-testing
Composer 安装命令:
composer require --dev osteel/openapi-httpfoundation-testing
包简介
Validate HttpFoundation requests and responses against OpenAPI (3+) definitions
README 文档
README
Validate HttpFoundation requests and responses against OpenAPI (3+) definitions.
See this post for more details and this repository for an example use in a Laravel project.
Important
While you can safely use this package for your projects, as long as version 1.0 has not been released "minor" version patches can contain breaking changes. Make sure to check the release section before you upgrade.
Why?
OpenAPI is a specification intended to describe RESTful APIs in a way that can be understood by both humans and machines.
By validating an API's requests and responses against the OpenAPI definition that describes it, we guarantee that the API is used correctly and behaves in accordance with 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 others.
How does it work?
This package is built on top of OpenAPI PSR-7 Message Validator, which validates PSR-7 messages against OpenAPI definitions.
It converts HttpFoundation request and response objects to PSR-7 messages using Symfony's PSR-7 Bridge and Tobias Nyholm's PSR-7 implementation, before passing them on to OpenAPI PSR-7 Message Validator.
Installation
Note
This package supports PHP 8.0 and above.
Via Composer:
composer require --dev osteel/openapi-httpfoundation-testing
Usage
Note
This package is mostly intended to be used as part of an API test suite.
Import the builder class:
use Osteel\OpenApi\Testing\ValidatorBuilder;
Use the builder to create a \Osteel\OpenApi\Testing\Validator object, using one of the available factory methods for YAML or JSON:
// From a local file: $validator = ValidatorBuilder::fromYamlFile($yamlFile)->getValidator(); $validator = ValidatorBuilder::fromJsonFile($jsonFile)->getValidator(); // From a string: $validator = ValidatorBuilder::fromYamlString($yamlString)->getValidator(); $validator = ValidatorBuilder::fromJsonString($jsonString)->getValidator(); // From a URL: $validator = ValidatorBuilder::fromYamlUrl($yamlUrl)->getValidator(); $validator = ValidatorBuilder::fromJsonUrl($jsonUrl)->getValidator(); // Automatic detection (slower): $validator = ValidatorBuilder::fromYaml($yamlFileOrStringOrUrl)->getValidator(); $validator = ValidatorBuilder::fromJson($jsonFileOrStringOrUrl)->getValidator();
Tip
You can also use a dependency injection container to bind the ValidatorBuilder class to the ValidatorBuilderInterface interface it implements and inject the interface instead, which would also be useful for testing and mocking.
You can now validate \Symfony\Component\HttpFoundation\Request and \Symfony\Component\HttpFoundation\Response objects for a given path and method:
$validator->validate($response, '/users', 'post');
Tip
For convenience, objects implementing \Psr\Http\Message\ServerRequestInterface or \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 (DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT 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($response, '/users');
Validating a request object works exactly the same way:
$validator->post($request, '/users');
In the example above, we check that the request matches the OpenAPI definition for a POST request on the /users path.
The validate method returns true in case of success, and throw a \Osteel\OpenApi\Testing\Exceptions\ValidationException exception in case of error.
Caching
This package supports caching to speed up the parsing of OpenAPI definitions. Simply pass your PSR-6 or PSR-16 cache object to the setCache method of the ValidatorBuilder class.
Here is an example using Symfony's Array Cache Adapter:
use Osteel\OpenApi\Testing\ValidatorBuilder; use Symfony\Component\Cache\Adapter\ArrayAdapter; $cache = new ArrayAdapter(); $validator = ValidatorBuilder::fromYamlFile($yamlFile)->setCache($cache)->getValidator();
Extending the package
There are two main extension points – message adapters and cache adapters.
Message adapters
The ValidatorBuilder class uses the HttpFoundationAdapter class as its default HTTP message adapter. This class converts HttpFoundation request and response objects to their PSR-7 counterparts.
If you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the MessageAdapterInterface interface and pass it to the setMessageAdapter method of the ValidatorBuilder class:
$validator = ValidatorBuilder::fromYamlFile($yamlFile) ->setMessageAdapter($yourAdapter) ->getValidator();
Cache adapters
The ValidatorBuilder class uses the Psr16Adapter class as its default cache adapter. This class converts PSR-16 cache objects to their PSR-6 counterparts.
If you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the CacheAdapterInterface interface and pass it to the setCacheAdapter method of the ValidatorBuilder class:
$validator = ValidatorBuilder::fromYamlFile($yamlFile) ->setCacheAdapter($yourAdapter) ->getValidator();
Other interfaces
The ValidatorBuilder and Validator classes are final but they implement the ValidatorBuilderInterface and ValidatorInterface interfaces respectively for which you can provide your own implementations if you need to.
Change log
Please see the Releases section for details.
Contributing
Please see CONTRIBUTING 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.
osteel/openapi-httpfoundation-testing 适用场景与选型建议
osteel/openapi-httpfoundation-testing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.13M 次下载、GitHub Stars 达 120, 最近一次更新时间为 2020 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「api」 「testing」 「validation」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 osteel/openapi-httpfoundation-testing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 osteel/openapi-httpfoundation-testing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 osteel/openapi-httpfoundation-testing 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
A PSR-7 compatible library for making CRUD API endpoints
Bundle Symfony DaplosBundle
http客户端
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 2.13M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 120
- 点击次数: 14
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-02