前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在 Slim 中伪造Request来进行你的HTTP测试吧

在 Slim 中伪造Request来进行你的HTTP测试吧

作者头像
seth-shi
发布2023-12-18 14:44:53
1300
发布2023-12-18 14:44:53
举报
文章被收录于专栏:seth-shi的专栏seth-shi的专栏
  • 代码需要做HTTP测试,Laravel中有自带这方面的功能。现在使用slim就得自己动手丰衣足食。
  • 网上找了许多例子,关于这方便的比较少。然后就想到了查看Laravel的源码
  • 看了一下,发现其实是自己伪造一个Request对象,然后执行返回结果
  • 然后自己也参考这个在slim中实现

构建好测试文件

  • composer.json加入以下内容自动加载,并执行composer dump-auto
代码语言:javascript
复制
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    }
  • 根目录新建配置文件phpunit.xml内容如下
代码语言:javascript
复制
<phpunit bootstrap="tests/bootstrap.php">
    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>
</phpunit>
  • tests/bootstrap.php文件内容如下
代码语言:javascript
复制
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->get('/api/v1/users', function (Request $request, Response $response) {

    $data = [['name' => 'Bob', 'age' => 40]];
    $payload = json_encode($data);

    $response->getBody()->write($payload);
    return $response
        ->withHeader('Content-Type', 'application/json');
});

// 这里不要运行 app
// $app->run();
// 并且声明一个函数得到 App 对象
function getApplication()
{
    global $app;

    return $app;
}
  • 创建测试文件tests/HomeTest.php写入一下内容
代码语言:javascript
复制
<?php

namespace Tests;
use Nyholm\Psr7\Uri;
use PHPUnit\Framework\TestCase;
use Slim\Factory\ServerRequestCreatorFactory;

class HomeTest extends TestCase
{
    public function testHello()
    {
        $name = 'Bob';

        $serverRequestCreator = ServerRequestCreatorFactory::create();
        $request = $serverRequestCreator->createServerRequestFromGlobals();

        $uri = new Uri();
        $request = $request->withUri($uri->withPath("/hello/{$name}"));

        $response = getApplication()->handle($request);
        $responseContent = (string)$response->getBody();

        $this->assertEquals($responseContent, "Hello, {$name}");
    }

    public function testJsonApi()
    {
        $serverRequestCreator = ServerRequestCreatorFactory::create();
        $request = $serverRequestCreator->createServerRequestFromGlobals();

        // 因为 Uri 和 Request 对象都是不可以修改的,所以都需要新建
        $uri = new Uri();
        $request = $request->withUri($uri->withPath('api/v1/users'));
        // 如果需要伪造查询参数可以这样子做
        // $request = $request->withQueryParams([]);
        // 使用全局函数拿到 App, 传入伪造的 Request,得到处理之后的 Response
        $response = getApplication()->handle($request);

        // 需要用 (string) 强转,不要直接 $response->getBody()->getContents()
        // 区别就是强转,在实现类把读取指针重置到了第一位,防止得不到完整的内容
        $responseContent = (string)$response->getBody();

        $this->assertJson($responseContent);
    }
}
  • 最后的最后,执行phpunit得到测试结果
代码语言:javascript
复制
$ phpunit
PHPUnit 7.5.17 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 45 ms, Memory: 4.00 MB

OK (2 tests, 2 assertions)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构建好测试文件
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档