warmans/http-testcase
Composer 安装命令:
composer require warmans/http-testcase
包简介
PHPUnit testcase for e2e testing libraries that talk to web services.
README 文档
README
PHPUnit test case to assist in behaviour/integration testing HTTP clients libraries. The testcase can start a http server and enqueue a set of responses (a session) which can are replayed for HTTP requests to the server. This is roughly similar to the testcase provided by Guzzle but without any external (NPM) dependencies.
The HTTP server is written in golang. The source is available here: https://github.com/warmans/http-playback
Test Case API
The testcase exposes the following methods:
startServer($port)
Start a server on the given port and return a Server instance.
getServer($port)
Get a Server instance for a server previously started using startServer().
stopServers()
Tell all running servers to exit. Note that shutdown functions are automatically registered for Server instances so omitting a tearDown call to stopServers should not leave servers running.
Server API
Interactions with http playback servers are simplified using the Server instance which is returned from calls to
startServer() and getServer()
start()
Start the server (generally only called by HttpTestCase::startServer()).
stop()
Stop the server (generally only called by HttpTestCase::startServer()).
enqueue($session, $status = 200, $body = "", $headers = array(), $wait = 0)
Add a response to a HTTP session. A session is just a named list of responses held within the server. Using named sessions allows the tester to simulate different endpoints without requiring exact paths. If you don't care about segregated response queues for different pages just always use the same session name.
getReplayUri($session, $path)
If you've enqueued some responses and just want the full playback URI you can use this method. e.g.
$server->enqueue("foo", 200); //setup a session
$server->getReplayUri("foo", "/bar/baz"); // http://localhost:8080/p/foo/bar/baz - will return the configured response
getOutput()
Get the logfile output from the server as a string. Note that a server log is created in sys_get_temp_dir().'/http-testcase.log'
isRunning()
Check if the server is currently running.
Examples
A test case will look like this (this one can be run from the project if dev dependencies are installed root using
./vendor/bin/phpunit example)
use HttpTestCase\HttpTestCase; class Test extends HttpTestCase { public static function setUpBeforeClass() { self::startServer('8081'); } public static function tearDownAfterClass() { self::stopServers(); } /** * Just send a HTTP GET request somewhere. */ protected function sendGet($host) { $ch = curl_init($host); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); return curl_exec($ch); } /** * Enqueue and request a single response. */ public function testHttpServerReturnsConfiguredResponse() { $server = self::getServer('8081'); $server->enqueue(1, 200, 'bar'); $this->assertEquals('bar', $this->sendGet('http://localhost:8081/p/1/')); } /** * Enqueue some responses then send some GET requests to the server and assert they were returned in the * expected order. */ public function testHttpServerReturnsMultipleResponses() { $server = self::getServer('8081'); $server->enqueue(1, 200, 'bar'); $server->enqueue(1, 200, 'baz'); $server->enqueue(1, 200, 'cat'); $this->assertEquals('bar', $this->sendGet('http://localhost:8081/p/1/')); $this->assertEquals('baz', $this->sendGet('http://localhost:8081/p/1/')); $this->assertEquals('cat', $this->sendGet('http://localhost:8081/p/1/')); } /** * Enqueue 1000 responses then request them all. */ public function testOneWayLoad() { $requests = 1000; //in $server = self::getServer('8081'); for ($i = 0; $i < $requests; $i++) { $server->enqueue(1, 200, "foo", array()); } //out for ($i = 0; $i < $requests; $i++) { if ('foo' !== ($res = $this->sendGet($server->getReplayUri(1)))) { $this->fail('bad response: '.$res); } } } /** * Enqueue and request 1000 responses */ public function testAlternatingLoad() { $requests = 1000; $server = self::getServer('8081'); for ($i = 0; $i < $requests; $i++) { //in $server->enqueue(1, 200, "foo", array()); //out if ('foo' !== ($res = $this->sendGet($server->getReplayUri(1)))) { $this->fail('bad response: '.$res); } } } }
warmans/http-testcase 适用场景与选型建议
warmans/http-testcase 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 413 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 warmans/http-testcase 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 warmans/http-testcase 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 413
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-16