Can not get REST tests to work


(Jono Wozere) #1

Hi

I feel like I’ve tried a 100 different variations of this to try and get REST working but not managed to get it yet.

I seem to get either

or

This is my config:


class_name: ApiTester

modules:

    enabled:

      - REST:

          depends: PhpBrowser

          url: /api/

          part: Json

      - Yii2:

          part: [orm, fixtures]

          configFile: 'config/web.php'

I have this in my urlManager:


 [

                'class' => 'yii\rest\UrlRule',

                'controller' => ['api/report'],

             ],



These tests work in postman, the URL I’m trying to access is/api/reports. Which is just the inherited RESTful controller route. I think this is a problem with urlManager not being loaded. But I know the config file is loaded.


    public function userIsNotAuthenticated(\ApiTester $I)

    {

        $I->amGoingTo('Test a non-authenticated person can\'t log in');

        $I->sendGET(Url::toRoute('report/index')); // Unable to resolve the relative route: report/index. No active controller is available.

        // $I->sendGET('reports'); //doesn't work cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

        $I->seeResponseCodeIs(\Codeception\Util\HttpCode::UNAUTHORIZED); // 401

    }



Not sure what i’ve done wrong here?


(Jono Wozere) #2

I had to make the url absolute.


$I->sendGET(Url::toRoute('/api/reports', true)); 

But I still can’t authenticate. I’ve tried,




$I->amHttpAuthenticated('my_email', 'my_password');

$I->haveHttpHeader('Authorization', 'Basic super long token goes here');



The Authorization header works for postman requests :unsure:


(Stefano Mtangoo) #3

did you solve this?


(Jono Wozere) #4

Yes




$I->haveHttpHeader('Authorization', 'Basic ' . base64_encode('whatever') );