Yii2 as dependency of REST module #2562

Closed
moicirk opened this Issue Nov 20, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@moicirk

moicirk commented Nov 20, 2015

I use REST module to test my Yii2 application rest controllers so my functional tests config looks like this:

modules:
    enabled:
      - Yii2
      - REST

    config:
        Yii2:
            configFile: 'codeception/config/functional.php'
        REST:
            depends: Yii2

So REST module uses Yii2Connector as a client to send requests. Considering that REST module encodes parameters to JSON string Yii2 client gets only raw body message here:

$yiiRequest = $app->getRequest();
$yiiRequest->setRawBody($request->getContent());
$yiiRequest->setBodyParams($_POST); //$_POST here is an empty array
$yiiRequest->setQueryParams($_GET);

Now, if we look at the yii\web\Request body parameters parsing we see this:

public function getBodyParams()
{
    if ($this->_bodyParams === null) {
        ....
    }
    return $this->_bodyParams;
}

and _bodyParams here is an empty array, so I cannot get my payload and parse it as a json. Am I doing everything right?

My test example:

$I->haveHttpHeader('Content-Type', 'application/json');
$I->wantTo('Update one record');
$I->sendPUT('/api/controller', $data);
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson($data);

Record is not updated because raw body was not parsed

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Nov 20, 2015

Member

You missed the parsing part in getBodyParams:
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L394

* Request parameters are determined using the parsers configured in [[parsers]] property. * If no parsers are configured for the current [[contentType]] it uses the PHP functionmb_parse_str()* to parse the [[rawBody|request body]].

Have you configured a parser for application/json?

Member

Naktibalda commented Nov 20, 2015

You missed the parsing part in getBodyParams:
https://github.com/yiisoft/yii2/blob/master/framework/web/Request.php#L394

* Request parameters are determined using the parsers configured in [[parsers]] property. * If no parsers are configured for the current [[contentType]] it uses the PHP functionmb_parse_str()* to parse the [[rawBody|request body]].

Have you configured a parser for application/json?

@Naktibalda Naktibalda added the Yii label Nov 20, 2015

@moicirk

This comment has been minimized.

Show comment
Hide comment
@moicirk

moicirk Nov 21, 2015

My request component config:

'request' => [
    'cookieValidationKey' => 'mycookievalidationkey',
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

The problem is not in parsing itself. The Request never reached this point because in moment when getBodyParams is called the _bodyParams variable equals an empty array which is not NULL

moicirk commented Nov 21, 2015

My request component config:

'request' => [
    'cookieValidationKey' => 'mycookievalidationkey',
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

The problem is not in parsing itself. The Request never reached this point because in moment when getBodyParams is called the _bodyParams variable equals an empty array which is not NULL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment