dimanche 25 février 2018

JSONPath unable to parse when expression contains hyphen

I am using codeception for testing my JSON API laravel project. I am using codeception's JSONPath functions to check the structure of the responses. I am unable to parse the expression when it contains a hyphen. Json Path uses hyphen as a subtraction operator, but in this case I have used it as a string. I know putting the substring containing hyohen inside double quotes("") inside the whole expression in single quote should do that. but its not working.

here is the function on my api test suite SomeTestCest.php

public function someTest(ApiTester $I)
{
    $I->sendGET('/teachers/122');
    //used double quotes as it contains hyphen
    $I->seeResponseJsonMatchesJsonPath('$.data.[*].relationships."high-schools"');
}

here is a sample response from the get request in the test.

{
    "data": {
        "type": "teachers",
        "id": "122",
        "attributes": {
            "fullname": "Rhoda Frami",
        },
        "relationships": {
            "high-schools": {
                "data": null
            },
            "subjects": {
                "data": null
            }
        },
        "links": {
            "self": "http:\/\/api.project:81\/teachers\/122"
        }
    }
}

when i run the tests using following

php codecept run tests/api/resources/SomeTestCest.php --steps --verbose

it throws an error

There was 1 failure:

---------
1) SomeTestCest:
 Test  tests/api/resources/SomeTestCest.php:someTest
 Step  See response json matches json path "$.data.[*].relationships."high-schools""
 Fail  Received JSON did not match the JsonPath `$.data.[*].relationships."high-schools"`.
Json Response: 
    {
    "data": {
        "type": "teachers",
        "id": "122",
        "attributes": {
            "fullname": "Rhoda Frami",
        },
        "relationships": {
            "high-schools": {
                "data": null
            },
            "subjects": {
                "data": null
            }
        },
        "links": {
            "self": "http:\/\/api.project:81\/teachers\/122"
        }
    }
}

i have tried following ways as well

$I->seeResponseJsonMatchesJsonPath("$.data.[*].relationships.'high-schools'");

$I->seeResponseJsonMatchesJsonPath('$.data.[*].relationships."high-schools"');

$I->seeResponseJsonMatchesJsonPath('$.data.[*].relationships.high-schools');

$I->seeResponseJsonMatchesJsonPath("$.data.[*].relationships.high-schools");

and its either this error. I know its because the hyphen is not parsed as its intended.

There was 1 error:

---------
1) SomeTestCest: 
 Test  tests/api/resources/SomeTestCest.php:SomeTest

  [Flow\JSONPath\JSONPathException] Unable to parse token high-schools in expression: .data.[*].relationships.high-schools 

So i checked with the base package JSONPath that is being used by codeception and found that it was working as intended.

$data = ['people' => [['high-schools' => 'Joe'], ['high-schools' => 'Jane'], ['high-schools' => 'John']]];
$result = (new \Flow\JSONPath\JSONPath($data))->find('$.people.*."high-schools"');
print_r($result);

here is the result

Flow\JSONPath\JSONPath Object
(
    [data:protected] => Array
        (
            [0] => Joe
            [1] => Jane
            [2] => John
        )

    [options:protected] => 0
)
=> true

So my question is how to parse a json string that contains a hyphen(-) to check the structure of json using Codeception with Json Path?

Thank you

Aucun commentaire:

Enregistrer un commentaire