phramework / testphase

API response testing tool
https://phramework.github.io/testphase/
Apache License 2.0
3 stars 0 forks source link

Add /response/rules using JSON pointer #13

Open VManolas opened 8 years ago

VManolas commented 8 years ago

in order to write rules for one path. (https://tools.ietf.org/html/rfc6901)

nohponex commented 8 years ago

Great, we can do something like: rules can be litteral string values of json schema (validators), or an array of previous two (interpreted as AND expression)

{
  "request": {
    "url": "article"
  },
  "response": {
    "rules": {
      "/data/0/id": "10",
      "/data/0/attributes/slug": {
        "type": "string",
        "pattern": "/^[a-z-A-Z_\\-]+$/"
      },
      "/data/0/type": [
        "article",
        {
          "type": "string",
          "enum": [
            "article"
          ]
        }
      ]
    }
  }
}
VManolas commented 8 years ago

Could you please explain more the "response" ? I can understand what it means, line by line, based on https://tools.ietf.org/html/rfc6901, but I cannot understand what it does. Thank you.

nohponex commented 8 years ago

So let's say the above GET article request, will return a collection of articles like the following:

{
  "data": [
    {
      "id": "10",
      "type": "article",
      "attributes": {
        "slug": "blog-post",
        "title": "Blog post"
      }
    },
    {
      "id": "33",
      "type": "article",
      "attributes": {
        "slug": "hello-world",
        "title": "Hello world, our first post!!"
      }
    }
  ]
}

We will test the above response against the defined response rules

nohponex commented 8 years ago

Basically if you wanted to write a rule for slut in a resource response you had to write:

{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "attributes": {
          "type": "object",
          "properties": {
            "slug": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

and by using JSON pointers you only need to write:

{
  "/data/attributes/slug": {
    "type": "string"
  }
}

Assuming data and attributes are objects But the ruleObjects can test if data and attributes in correct schema. Anyway the pointer SHOULD throw an incorrect type exception if they are not objects.

nohponex commented 8 years ago

Added at ff24ee15435b57307b9a20eeac12d9433b18985c

WIP example:

{
  "meta": {
    "description": "Get posts",
    "order": -1,
    "incomplete": false
  },
  "request": {
    "url": "http://jsonplaceholder.typicode.com/posts",
    "method": "GET",
    "headers": [
      "Accept: application/json"
    ]
  },
  "response": {
    "statusCode": 200,
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "rules": {
      "/0/userId" : {
        "type": "integer"
      },
      "/0" : {
        "type": "object"
      },
      "/0/title" : {
        "type": "string"
      },
      "/0/id" : 1
    }
  }
}

NOTE perhaps we should allow multiple rules for the same path, in that case /response/rules must be an array with object elements witch specifies path and rule members.