imbo / behat-api-extension

API extension for Behat, used to ease testing of JSON-based APIs
MIT License
107 stars 42 forks source link

Fix sprintf() if JSON response contains some % sign #93

Closed symfonyaml closed 3 years ago

symfonyaml commented 4 years ago

Problem

if the JSON response contains few percentage sign %, then the Assert\Assertion class throw a PHP error because the sprintf if corrupted.

For example in there

Reproduce

if the JSON response is exactly like this

{
    "1": {
        "stringValue": "1\u00a0%"
    },
    "2": {
        "stringValue": "2\u00a0%"
    },
    "3": {
        "stringValue": "3\u00a0%"
    }
}

with behat test assert

And the response body is a JSON array of length 1

There is an error

Warning: sprintf(): Too few arguments in
vendor/beberlei/assert/lib/Assert/Assertion.php line 1882
christeredvartsen commented 4 years ago

Thanks for the PR. Could you add a test or two that verifies this behaviour as well?

christeredvartsen commented 4 years ago

I can't seem to reproduce this error. I have added the following test:

Scenario: Reproduce bug from issue #93
Given a file named "features/bug-93.feature" with:
    """
    Feature: Get data from endpoint
        Scenario: Check the response
            When I request "/bug-93"
            Then the response body is a JSON array of length 1
    """
When I run "behat features/bug-93.feature"
Then it should fail with:
    """
    The response body does not contain a valid JSON array. (InvalidArgumentException)

    1 scenario (1 failed)
    2 steps (1 passed, 1 failed)
    """

The /bug-93 endpoint returns the following JSON:

{
    "1": {
        "stringValue": "1\u00a0%"
    },
    "2": {
        "stringValue": "2\u00a0%"
    },
    "3": {
        "stringValue": "3\u00a0%"
    }
}

Am I missing something?

christeredvartsen commented 3 years ago

Closing with because of lack of feedback. Feel free to re-open if you have some more information to add to the issue.