Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yii2 as dependency of REST module #2562

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

Yii2 as dependency of REST module #2562

moicirk opened this issue Nov 20, 2015 · 2 comments
Labels
Yii

Comments

@moicirk
Copy link

@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
Copy link
Member

@Naktibalda 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
Copy link
Author

@moicirk 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.