concept predicate in category microservice

appears as: predicates, predicate, predicates, The predicate, predicate, The predicates
Testing Microservices with Mountebank

This is an excerpt from Manning's book Testing Microservices with Mountebank.

Figure 1.15. Response generation in mountebank using predicates and responses in stubs

Network protocols are complicated beasts. The first job of the imposter is to simplify a protocol-specific request into a JSON structure so that you can match the request against a set of predicates. Each protocol gets its own request JSON structure; we will look at HTTP’s structure in the next chapter.

You configure each imposter with a list of stubs. A stub is nothing more than a collection of one or more responses and, optionally, a list of predicates. Predicates are defined in terms of request fields, and each one says something like “Request field X must equal 123.” No self-respecting mocking tool would leave users with a simple equals as the only comparison operator, and mountebank ups the game with special predicate extensions to make working with XML and JSON easier. Chapter 4 explores predicates in detail.

Listing 2.3. An imposter with predicates
curl -X POST http://localhost:2525/imposters --data '{
  "port": 3000,
  "protocol": "http",
  "stubs": [                                                 #1
    {
      "predicates": [{
        "equals": {                                          #2
          "query": { "page": "2" }                           #2
        }                                                    #2
      }],
      "responses": [{
        "is": {                                              #3
          "statusCode": 200,                                 #3
          "headers": {"Content-Type": "application/json"},   #3
          "body": { "products": [] }                         #3
        }                                                    #3
      }]
    },
    {
      "responses": [{
        "is": {                                              #4
          "statusCode": 200,                                 #4
          "headers": { "Content-Type": "application/json" }, #4
          "body": {                                          #4
            "products": [                                    #4
              {                                              #4
                "id": "2599b7f4",                            #4
                "name": "The Midas Dogbowl",                 #4
                "description": "Pure gold"                   #4
              },                                             #4
              {                                              #4
                "id": "e1977c9e",                            #4
                "name": "Fishtank Amore",                    #4
                "description": "Show your fish some love"    #4
              }                                              #4
            ],                                               #4
            "_links": {                                      #4
              "next": "/products?page=2&itemsPerPage=2"      #4
            }                                                #4
          }                                                  #4
        }                                                    #4
      }]
    }
  ]
}'

Although mountebank cannot guarantee you Abagnale’s indefatigable confidence, it does give you the ability to mimic one of the other key factors of his success: tailoring your response to your audience. Had Abagnale acted like a pilot when facing a room full of medical interns, he would have had a much shorter medical career. Mountebank uses predicates to determine which response to use based on the incoming request, giving your imposter the ability pretend to be a virtual pilot for one request and a virtual doctor for the next one.

  • Predicates are case-insensitive by default. You can change that by setting the caseSensitive predicate parameter to true.