POST request has $_SERVER['REQUEST_METHOD'] = 'GET' in Yii2 codeception testing

217 Views Asked by At

I'm trying to write some acceptance tests for yii2 application.

I my SiteController I have some action, which include the following piece of code:

if (!Yii::$app->request->isPost) {
        throw new NotFoundHttpException('Unexpected GET method');
    }

When I'm trying to test this action - it's always FAILED, because my POST requests don't passed this check Yii::$app->request->isPost . They always have $_SERVER['REQUEST_METHOD'] = 'GET' instead of POST.

I tried this variants:

$I->sendPOST($url, $options)

$I->sendAjaxPostRequest($url, $options)

Also I tried to make custom actions in Helper like this

public function makePOST($url, $params = []) {
    $this->getModule('PhpBrowser')->_loadPage('POST', $url, $params);
}

And then call it from my test. In all cases I'm getting GET request instead of POST...

Please help me to understand why it happens.

1

There are 1 best solutions below

0
Artem Pakhomov On

Maybe the reason is CSRF?

Yii2 documentation CSRF

Warning: Disabling CSRF will allow any site to send POST requests to your site. It is important to implement extra validation such as checking an IP address or a secret token in this case.