Thursday, May 19, 2016

Parameterising contracts for interaction driven development



This is a follow on post about Contract, if you've not read any of the previous posts, this should serve as a good introduction. 

Having to specify single contract per file is a little painful, especially if you have a few scenarios to be tested. Lets look at a simple example, imagine I want a contract to verify that an endpoint exists and the correct status code will be returned. 





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "info" : {
    "details": "The server should return the correct status for an endpoint"
  },
  "request" : {
    "method" : "GET",
    "path" : "/service/car"
  },
  "response" : {
    "status" : 200
  }
}

If we wanted to expand on this to include multiple endpoints, we would need to duplicate a lot of configuration. With parameters, we can reuse the same contract multiple times.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
  "info" : {
    "details": "The server should return the correct status for an endpoint"
  },
  "parameters" : [
    {
      "endpointName" : "car",
      "expectedStatus": 200
    },
    {
      "endpointName" : "jeep",
      "expectedStatus": 200
    },
    {
      "endpointName" : "bicycle",
      "expectedStatus": 404
    },
    {
      "endpointName" : "pedestrian",
      "expectedStatus": 404
    }
  ],
  "request" : {
    "method" : "GET",
    "path" : "/service/${contract.parameters.endpointName}"
  },
  "response" : {
    "status" : "${contract.parameters.expectedStatus}"
  }
}


A client, fed this contract will execute 4 requests, one for each parameter group. A server, fed this contract, will expand it into 4 contracts.

Links:

No comments: