douou1891 2014-12-22 12:52
浏览 31

Yii2期望JSON POST的API端点的代码测试

Does anyone have any experience of using Codeception tests within Yii2 to test an API endpoint that expects a JSON request?

Right now, I'm just hoping to do a basic check for a 200 response code and a JSON body.

Currently, my test breaks with "[PHPUnit_Framework_ExceptionWrapper] Bad request."

Edit
It turns out this 'Bad request' error is thrown by my own code, because the $request->getRawBody() is empty. That is, the parameters that I have set in the call to sendPOST() are not making it through into the Yii request object. Without them present, the request is doomed to fail.
/Edit

Edit2
Remove headers from the test (as per comments) and it works.
/Edit2

I'm trying to use the sendPOST() method of the Codeception REST module.

I'm not confident that Codeception\Lib\Connector\Yii2.php is correctly mapping my parameter data from the BrowserKit Request to the corresponding Yii Request. That is, the request that goes into doRequest() has my data, but $app->getRequest() later in that same function lacks it.

Here's my test:

$I = new FunctionalTester($scenario);
$I->haveHttpHeader('Content-Type', 'application/json'); //remove
$I->haveHttpHeader('Accept', 'application/json'); //remove
$I->sendPost(
    '/index-test.php/api/v1/poi/bbox',
    [
        'bbox' => [
            'minLat' => 51.53667954545721,
            'minLng' => -0.14273375272750854,
            'maxLat' => 51.53749870786214,
            'maxLng' => -0.13856828212738037
        ]
    ]
);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();

And here's a CURL representation of a working request:

curl \
--request POST \
--header "Accept: application/json" \
--header "Content-Type: application/json; charset=UTF-8" \
--data '{ "bbox": {"minLng": -0.152573, "minLat": 51.529012, "maxLng": -0.124691, "maxLat": 51.54104} }' \
<endpoint>

Did I do something wrong at an earlier stage? Or, am I approaching this the wrong way? Perhaps there's a better way to send a JSON POST request like this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c