Behat / WebApiExtension

Behat extension to test web APIs
http://extensions.behat.org
MIT License
111 stars 114 forks source link

Call to a member function json() on null #34

Open soullivaneuh opened 9 years ago

soullivaneuh commented 9 years ago

Behat config:

default:
    calls:
        error_reporting: 14335 # E_ALL & ~E_USER_DEPRECATED
    extensions:
        Behat\Symfony2Extension: ~
        Behat\WebApiExtension: ~
        Behat\MinkExtension:
            sessions:
                default:
                    symfony2: ~
    suites:
        default:
            contexts:
             - Behat\WebApiExtension\Context\WebApiContext
             - \FeatureContext

Behat feature:

Feature: api/security
  In order to fully or partially access the api
  I need to have the good rights

  Scenario: API as anonymous
    Given I am on "/api/user"
    And the response status code should be 401
    And the response should contain json:
    """
    {
       "error" : "invalid_grant",
       "error_description" : "No API key given."
    }
    """

Result:

$ behat features/api/security.feature 
Feature: api/security
  In order to fully or partially access the api
  I need to have the good rights

  Scenario: API as anonymous                   # features/api/security.feature:5
    Given I am on "/api/user"                  # FeatureContext::visit()
    And the response status code should be 401 # FeatureContext::assertResponseStatus()
PHP Fatal error:  Call to a member function json() on null in project/vendor/behat/web-api-extension/src/Context/WebApiContext.php on line 244
[...]

Is that an issue or a misconfiguration?

Thanks.

soullivaneuh commented 9 years ago

I think I have a clue:

/**
 * @var \GuzzleHttp\Message\ResponseInterface
 */
private $response;

AFAIK, if Guzzle got a 4xx or a 5xx error, it throw an exception instead of returning the response.

I don't know if guzzle is the best for it or if we just have to configure it, but a 401 code with response could be possible:

$ curl http://localhost:8000/api/user | json_pp 
{
   "error" : "invalid_grant",
   "error_description" : "No API key given."
}