FLAT
CouperSevenval TechnologiesDocker ImageGithub
master
master
  • Changelog
  • FLAT
  • Administration
    • Configuration
    • Docker
    • Logging
  • Cookbook
    • Using the Built-in Mocking
    • Performing Additional Checks on JWT Access Tokens
    • Logging Custom Fields
    • Using Environment Variables
    • Handling Errors with an Error Flow
    • File Serving
    • Forwarding a Request to an Upstream API
    • Extracting Common Initialization Flow Tasks
    • Encoding and Decoding JWT
    • Passing Header Fields to the Client
    • How can I pass an arbitrary header field to an upstream system?
    • Performing Additional Checks on JWT Access Tokens
    • Proxying requests to Upstream APIs
    • Increasing the Request Timeout
    • How can I see what the client requested?
    • Using Swagger UI for API Documentation
    • Testing API Requests
    • Testing with Backend Requests
    • Testing Templates
    • Sending POST Requests
    • Processing Upstream Responses
    • Protecting Access using JWT Tokens
  • Reference
    • Configuration
    • Debugging
    • flat CLI
    • Flow
    • Variables
    • OpenAPI / Swagger Integration
    • OpenAPI
      • CORS - Cross-Origin Resource Sharing
    • OpenAPI
      • Differences from Swagger
    • OpenAPI
      • Mocking
    • OpenAPI
      • Routing
    • OpenAPI
      • Security
    • OpenAPI
      • Upstream APIs
    • OpenAPI
      • Validation
    • Flow Actions
      • assert Action
      • auth Action
      • backend-flow Action
      • copy Action
      • debug Action
      • dump Action
      • echo Action
      • error Action
      • eval Action
      • log Action
      • nameshave Action
      • pass-body Action
      • proxy-request Action
      • regex Action
      • request Action
      • requests Action
      • serve Action
      • set-config Action
      • set-env Action
      • set-response-headers Action
      • set-status Action
      • sub-flow Action
      • template Action
      • test-request Action
      • xslt Action
    • Functions
      • apply-codecs()
      • array-reverse()
      • array()
      • base64-decode()
      • base64-encode()
      • body()
      • calc-signature()
      • capitalize-first()
      • content()
      • decrypt-xml()
      • decrypt()
      • encrypt()
      • ends-with()
      • file-exists()
      • fit-document()
      • fit-log()
      • fit-serialize()
      • get-log()
      • has-class()
      • html-parse()
      • join()
      • json-doc()
      • json-parse()
      • json-stringify()
      • json-to-csv()
      • json-to-xml()
      • jwt-decode()
      • jwt-encode()
      • ldap-lookup()
      • ldap-query()
      • lookup()
      • matches()
      • md5()
      • replace()
      • sort()
      • split()
      • tolower()
      • toupper()
      • trim()
      • unixtime()
      • urldecode(), url-decode()
      • urlencode(), url-encode()
      • uuid3() and uuid4()
      • verify-signature()
      • verify-xmldsig()
      • xml-parse()
      • xml-to-json()
    • Templating
      • {{,}}
      • Comment {{// …}}
      • Dot {{.}}
      • Conditional `{{if <condition>}} … {{elseif <condition> }} … {{else}} … {{end}}
      • loop
      • ?? Operator
      • Object XML Notation (OXN)
      • Pair Producer {{: …}}
      • Placeholder
      • Template Variables
      • with
    • Testing
  • Tutorial
Powered by GitBook
On this page
  • Mocking the Backend
  • Asserting Upstream Requests
  • See also

Was this helpful?

  1. Cookbook

Testing with Backend Requests

PreviousTesting API RequestsNextTesting Templates

Last updated 5 years ago

Was this helpful?

Usually, our contain to one or more backends that we use to provide the client response. Sometimes upstream requests use results of other upstream requests. Testing such flows can be a challenge.

In this example flow, we use the helpful tool to reflect a parameter.

The Swagger snippet is

basePath: /api
paths:
  /dashboard:
    x-flat-flow: dashboard.xml
    get:
      parameters:
        - name: count
          in: query
          description: number of notifications
          type: number

and the flow in dashboard.xml:

<flow>
  <request content="notifications">
   {
    "url": "https://httpbin.org/anything",
    "headers": {
      "accept": "application/json"
    },
    "query": {
      "noti": {{ $request/get/count ?? 1 }}
    }
  }
  </request>
  <break if="$upstream/*/error" />

  <template>
    {{$rsp := content("notifications")/json }}
    {
      "sites": [],
      "notifications": {{ number($rsp/args/noti) ?? 2 }}
    }
  </template>
</flow>

We use the client's count= parameter (or 1 if missing) to send it as noti= to httpbin.org.

A response for that upstream request could look like this:

{
  "args": {
    "noti": "8"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.54.0"
  }, 
  "method": "GET", 
  "url": "https://httpbin.org/anything?noti=8"
}

The parameter is reflected back in the JSON responses args property. That is what we use in the response template, where it is finally called notifications.

Mocking the Backend

tests/test-notifications.xml:

<flat-test>
  <test-request>
  {
    "path": "/api/dashboard",
    "query": {
      "count": 5
    }
  }
  </test-request>

  <assert>
  [
    [ "json-parse($response)/notifications", 5 ]
  ]
  </assert>
</flat-test>

This actually works. However, our flow in fact sends the upstream request to httpbin.org. The test will fail if their server is down. And if we were mocking a single-sign-on service, we would need real credentials. That's not how we like our automated tests to work.

We extend our flat-test with this backend-flow call as its first action:

<flat-test>
  <backend-flow request="notifications">
    <template>
    {
      "args": {
        "noti": 5
      }
    }
    </template>
  </backend-flow>
  …

Note that the backend-flow action only registers that flow for subsequent request actions. That's why we have to put it first.

You can register multiple backend flows for your requests. Upstream requests and flows are matched using their request ID. In our case, notifications.

<backend-flow>
  <copy in="mock.json" />
</backend-flow>

Asserting Upstream Requests

Injecting a mocked response into your flow only allows to test one direction: downstream. How do we test that our flow has sent the right parameters to the upstream server?

You can use assert in backend-flow, too:

  <backend-flow request="notifications">
    <assert>
    [
      [ "$request/get/noti", 5 ]
    ]
    </assert>
    <template>
    {
      "args": {
        "noti": 5
      }
    }
    </template>
  </backend-flow>

This test will fail if we don't pass the count parameter as expected.

See also

A naïve approach to writing a would be a test-request with some assertions.

A allows us to create a fake response for the upstream requests. We can use regular to set headers, status code, response bodies and so on.

💡If your mock response is a bit longer than ours here, you can store it in a separate file and use instead:

For this assertion we have made use of the . You can use $body to inspect the upstream request body. These variables are overridden in backend flows and restored afterwards. All other global variables are shared between flow, backend flow and test flow! This allows for dynamic backend mocks and more assertion possibilities.

(cookbook)

(cookbook)

(reference)

(reference)

(reference)

(reference)

flows
upstream requests
httpbin.org
flat test
backend-flow test action
actions
copy
Testing API Requests
Testing Templates
Test Framework
assert
backend-flow
test-request
pre-defined variable $request