Let us see, how various types of responses can be validated in vREST.

  1. My API returns the static response.
  2. I just want to validate the schema of my response, not the actual content.
  3. My API returns some dynamic properties like _id, createdOn etc. and I want to ignore them during response validation.
  4. My API returns a very large response and I am interested in validating only a small part of my API response.
  5. My API returns some response in which some part of the response can be obtained from the responses of previous test cases.
  6. My API returns dynamic response and none of the above fit to my needs.

Scenario 1: My API returns the static response.

If your API returns the static response, then just set the Expected Body with the static response and Expected Status Code.

Let us suppose, your API returns the following static JSON response,

 

{
  "vREST": [
    "A simple and intuitive tool to quickly validate your REST APIs.",
    "Deliver zero defect web applications with very less effort in API testing.",
    "Works in hosted mode (http://vrest.io).",
    "No skilled resources required to validate your web application.",
    "Quickly generate documentation for your API specifications.",
    "De-couple your frontend implementation with backend implementation with easy to use Mock Server.",
    "Ease of maintenance over a span of releases."
  ]
}

 

For such scenarios, simply specify your static response body in Expected Body and set the Expected Status Code also as specified in the following image. Validator used for this scenario is "Default Validator".

Scenario 2: I just want to validate the schema of my response, not the actual content.

Let us consider a scenario, where response contains random data (unpredictable data) or list of some unordered items. Only the schema/structure of the response is predictable. In such scenario, Default Schema Validator can be used. 

e.g. let's suppose an API returns a list of random quotes. Since it returns a random list, so we cannot predict the outcome of this API. But what we know is the structure/schema of this response.

In the following response,

  1. Every item in the quotes list have fields like "quote", "author", "designation", "organization".
  2. And each field is of type String.

 

{
  "quotes": [
    {
      "quote": "Stay hungry, stay foolish.",
      "author": "Steve Jobs",
      "designation": "Co-founder, Chairman & CEO",
      "organization": "Apple Inc."
    },
    {
      "quote": "If you're changing the world, you're working on important things. You're excited to get up in the morning.",
      "author": "Larry Page",
      "designation": "Co-founder & CEO",
      "organization": "Google"
    },
    ...
  ]
}

 

For using Default Schema Validator, first we need to define the Expected Schema of the response as shown in the following image. We can use an external tool http://jsonschema.net to automatically generate the schema of our response.

Note:

Scenario 3: My API returns some dynamic properties like _id, createdOn etc. and I want to ignore them during response validation and also I want to validate some of the properties with regular expression.

Suppose, If your API which creates a resource on the server and returns some dynamic properties like _id, createdOn etc. And you want to ignore these dynamic properties during the response validation and also you want to check the email property with email regular expression. In such scenario, Default Validator can be used.

e.g. Let us suppose, the API returns the following response:

 

{
  "_id": "536493015f56452a03000010",
  "createdOn": "2014-05-03T06:56:01.134Z",
  "name": "John Doe",
  "email": "john.doe@example.com",
  "designation": "Chief Technical Officer",
  "organization": "Example.com",
  "country": "India",
  "aboutMe": "My name can be used as a placeholder name and I don't have any identity.",
  "twitterId": "fake.john.doe",
  "facebookId": "fake.john.doe",
  "githubId": "fake.john.doe"
}

 

Here in the above response, you want to ignore the dynamically generated _id and createdOn field.
For such scenarios,
  1. Simply use the special variable "{{*}}" for values, which you want to ignore.
  2. And use regular expression in format "{{/REG_EXP/}}" to match the value against a regular expression.

Now, the expected body should look like this:

Note: 

Scenario 4: My API returns a very large response and I am interested in validating only a small part of my API response.

If you want to validate only a small part of your API response and want to ignore rest of the properties then you can use special variable "{{*}}": "{{*}}".

Let us suppose, the API returns the following response:

{
  "key1": "value1",
  "key2": {
    "key2.1": "value2.1",
    "key2.2": "value2.2",
    "key2.3": "value2.3",
    "key2.4": "value2.4"
  },
  "key3": "value3",
  "key4": "value4"
}

And in the above response, you only want to validate key1, key2.2 and key2.3 values.

For such scenarios, simply use the special variable "{{*}}": "{{*}}" (key-value pair) to ignore rest of the keys and values. Now, the expected body should look like this:

Further, let us suppose, you want to validate only the existence of key "key1" in your response, not the value of "key1", then you can mix this scenario with scenario 4 and write your expected body like this:

Scenario 5: My API returns some response in which some part of the response can be obtained from the responses of previous test cases.

Let us take an example in which one test case creates a resource on server and second test case updates that newly created resource.

Scenario 6: My API returns dynamic response and none of the above fit to my needs.

In vREST, most of the responses can be validated with the help of built-in response validators "Default Validator" and "Default Schema Validator". But if that doesn't fit your needs, then you can define your own custom validator in vREST or you can even mix and match validators and assertions.

For this scenario,

  1. First define your custom validator in Project Configuration >> Response Validator Section.



  2. Now associate this custom validator with your test case like below:

That's it. Now your test case will be validated with your custom validator when you execute it.

 

If you think, your scenario is not covered here then you can discuss your scenario with us by sending an email to "support@vrest.io".

 

Note: