Search code examples
laraveltestinghttp-headersfunctional-testingcodeception

Codeception not setting headers for AJAX


I have the following functional test set up

$I = new FunctionalTester($scenario);
$I->wantTo('get a list of users');

$key = $I->amApiAuthenticated();
$I->seeInSession('key');
$I->setHeader('X-API-KEY', $key);    
$I->sendAjaxGetRequest('/api/users');
$I->seeResponseCodeIs(200);

But the test fails because the X-API-KEY is not set (results in a 403), this is the request as dumped to a log file from Laravel:

[2015-05-07 00:26:10] production.INFO: GET /api/users HTTP/1.1
Accept:           text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:   ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language:  en-us,en;q=0.5
Host:             localhost
User-Agent:       Symfony2 BrowserKit
X-Requested-With: XMLHttpRequest

Am I missing something here? Is there really no way to set headers like this? I've tried both REST and PhpBrowser ajax methods, and none of them work.

I've also tried to rename it to HTTP_X_API_KEY but it still does not show up in the request.

Any help would be much appreciated!


Solution

  • Sorry about the delay.

    I removed the dependency for PhpBrowser, and included the REST component instead, in addition to adjust how i grabbed the header field (using Request::header() instead of FormRequest passed to the controller method).