imbo / behat-api-extension

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

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

Open milkovsky opened 3 years ago

milkovsky commented 3 years ago

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

milkovsky commented 3 years ago

Example step:


    Then the response body does not contain JSON:
    """
    {
      "foo": "bar"
    }
    """
milkovsky commented 1 year ago

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

christeredvartsen commented 1 year ago

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 commented 1 year ago

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)"
    }
    """
milkovsky commented 1 year ago

@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 commented 1 year ago

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