2

I am trying to use Codeception to function test my app's RESTful API. I am using HttpBasicAuth with a custom auth function that validates username and password.

The endpoint I am trying to test is a simple GET /users. The functional.suite.yml looks like this:

class_name: FunctionalTester
modules:
  enabled: 
    - Filesystem 
    - Yii2 
    - REST:
        depends: PhpBrowser
    - tests\codeception\_support\FixtureHelper
  config: 
    Yii2:
        configFile: 'codeception/config/functional.php'
    REST:
        url: 'http://localhost:8888/v1/'

My UserApiCept.php starts this way:

<?php 
$I = new FunctionalTester($scenario);
$I->wantTo('test the user REST API');
$I->amHttpAuthenticated('goodname', 'goodpassword');
$I->sendGET('users/1');

When I run the suite I get a 401 (Unauthorized HTTP Exception). In --debug, request headers are empty. However, when I run the GET in Postman (providing the username and password), the GET returns the JSON response I expect. So I know the API works.

It appears that there is an issue with amHttpAuthenticated, which I assume is supposed to provide the authorization header, right?

Any advice on how I can troubleshoot this?

Mahalo, Joe

1 Answer 1

0

In case anyone else has this problem, I had the dependency wrong:

class_name: FunctionalTester
modules:
  enabled: 
    - Filesystem 
    - Yii2 
    - REST:
        depends: Yii2
...

When I initially did the build after adding REST, I got the error message: "This module depends on Codeception\Lib\InnerBrowser" and then provided an example using

depends: PhpBrowser

Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.