behat-api-extension icon indicating copy to clipboard operation
behat-api-extension copied to clipboard

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

Open milkovsky opened this issue 3 years ago • 6 comments

A proof of concept for https://github.com/imbo/behat-api-extension/issues/105.

milkovsky avatar Nov 22 '21 12:11 milkovsky

Example step:


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

milkovsky avatar Nov 22 '21 12:11 milkovsky

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

milkovsky avatar Mar 16 '23 08:03 milkovsky

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 avatar Mar 16 '23 09:03 christeredvartsen

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)"
    }
    """

brockfanning avatar Apr 14 '23 17:04 brockfanning

@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 avatar Jul 06 '23 13:07 milkovsky

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"
      ]
    }
    """

milkovsky avatar Jul 12 '23 11:07 milkovsky