Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'Then the response body does not contain JSON' step. #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

milkovsky
Copy link

@milkovsky milkovsky commented Nov 22, 2021

A proof of concept for #105.

@milkovsky
Copy link
Author

milkovsky commented Nov 22, 2021

Example step:


    Then the response body does not contain JSON:
    """
    {
      "foo": "bar"
    }
    """

@milkovsky
Copy link
Author

@christeredvartsen , hey. Do you find this feature unnecessary?

@christeredvartsen
Copy link
Member

I find the feature somewhat confusing. Would you be able to add some tests for it so I can better understand the use case?

@brockfanning
Copy link

I was looking for a step like this, and can explain my use-case: Our API endpoint was adding some unwanted code in a field that is otherwise difficult to predict. For example, the response JSON might be something like:

{
  myField: "lots of unpredictable text etc... SOME UNWANTED CODE ...more unpredictable text..."
}

We fixed the problem, but wanted to write a Behat test to ensure it never happened again. Because the text is unpredictable, we can't use And the response body contains JSON to assert that the unwanted code is not there. So something like this would be ideal:

And the response body does not contain JSON:
    """
    {
        "myField": "@regExp(/SOME UNWANTED CODE/i)"
    }
    """

}
catch (AssertionFailure $e) {
throw new AssertionFailedException(
'Comparator did not return in a correct manner. Marking assertion as failed.'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message should be 'The searched value was found in the JSON response.'.

@milkovsky
Copy link
Author

milkovsky commented Jul 6, 2023

@brockfanning you can add the step from the pull request into your custom behat context, that extends Imbo\BehatApiExtension\Context\ApiContext;. This is what I am doing at the moment.

@milkovsky
Copy link
Author

I find the feature somewhat confusing. Would you be able to add some tests for it so I can better understand the use case?

@christeredvartsen the idea of this feature is to make sure, that unwanted content does not appear in the API.

For example, I would like to test an endpoint, that returns all articles from an author.
I would like to make sure, that the results do not include articles from other authors.
A test could look like this:

    Given I request "/api/articles?author=Peter"
    # The results contain articles from Peter.
    And the response body contains JSON:
    """
    {
      "articles": [
        "title": "Article 1".
        "author": "Peter"
      ]
    }
    """
    # The results do not contain articles from other authors.
    And the response body does not contain JSON:
    """
    {
      "articles": [
        "title": "Article 2".
        "author": "Max"
      ]
    }
    """

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants