jakzal/BehatRestExtension

Expected response should handle placeholders

jakzal opened this issue · 4 comments

At the moment differ is very simple and it can only compare exact responses:

  Scenario: Debugging request content and headers
    When the client requests POST "/debug" with:
    """
    X-Debug: 1
    X-Debug-Token: abcdef

    [
      {
        "name": "SymfonyCon 2013",
        "location": "Warsaw"
      }
    ]
    """
    Then the response should be a 200 with json:
    """
    {
      "headers": {
        "X-Debug": "1",
        "X-Debug-Token": "abcdef"
      },
      "content": [
        {
          "name": "SymfonyCon 2013",
          "location": "Warsaw"
        }
      ]
    }
    """

In some cases, especially with generated values, it would be better to use placeholder matching (regexp).

One way would be to use a "named placeholders", like @integer@, @string@, @date@ which would map to a regular expression:

  Scenario: Debugging request content and headers
    When the client requests POST "/debug" with:
    """
    X-Debug: 1
    X-Debug-Token: abcdef

    [
      {
        "name": "SymfonyCon 2013",
        "location": "Warsaw"
      }
    ]
    """
    Then the response should be a 200 with json:
    """
    {
      "headers": {
        "X-Debug": @integer@,
        "X-Debug-Token": "@string@"
      },
      "content": [
        {
          "name": "SymfonyCon 2013",
          "location": "Warsaw"
        }
      ]
    }
    """

The advantage of using this approach over using regular expressions directly, would be avoiding repetition and option to define custom matchers.

Perhaps considering porting some parts of https://github.com/chancancode/json_expressions would be sufficient?

Type maching solves many problems but sometimes it good to have possibility to match for example values from range or just use a simple wildcard. Maybe expression language could be used here?
Something like

    Then the response should be a 200 with json:
    """
    {
      "headers": {
        "X-Debug": expr(value > 1 && value < 10),
        "X-Debug-Token": "@string@"
      },
      "content": [
        {
          "name": "SymfonyCon 2013",
          "location": "Warsaw"
        }
      ]
    }
    """

@norzechowicz this is kind of nice idea, but i would say asseting types + wildcard should be biggest priority, since you basicly can diff everything you will need.