Guides

Test through an API gateway

Run Portman contract tests through an API gateway such as Kong, Tyk, AWS API Gateway, Azure API Management or Apigee to catch missing auth, broken request validation and response rewrites.

Portman works with any API gateway. It sends requests to the base URL you give it and checks the responses against your OpenAPI spec, so it doesn't matter what sits in between. Point it at the gateway and you test what your consumers actually get: your service plus every gateway policy in front of it.

Most gateways can import an OpenAPI document to configure their routes (Tyk OAS, Kong with decK, AWS API Gateway, Azure API Management, Apigee, Zuplo). The same spec then configures the gateway and generates the tests that check the gateway really behaves that way.

Test through the gateway

Pass the gateway URL as the base URL. Any path prefix the gateway adds goes in the base URL too:

bash
portman -l ./openapi.yaml -b https://gateway.example.com/api/v1 --runNewman

-b replaces the servers in the spec, so a spec written for the upstream service works unchanged. Vendor extensions such as x-kong-*, x-amazon-apigateway-* or x-tyk-* are ignored.

A test run through the gateway catches gateway misconfigurations that tests against the service alone can't:

Gateway featureWhat breaksHow Portman catches it
Auth plugin or authorizerA new route goes live without the plugin attachedA variation without credentials expects 401 or 403 on every operation
Request validationOut-of-spec input reaches the upstreamFuzzing sends missing required fields and out-of-range values and expects a 4xx
Response transformationA rewrite drops or renames fieldsThe JSON schema contract test fails
CachingA stale cached response has an old shapeThe JSON schema contract test fails
Headers set at the gatewayHSTS, nosniff or CORS missing on some routesA header check in extendTests

The security example bundles the auth, input and header checks into one config. See also Security tests.

Compare with the upstream

Run the same spec once through the gateway and once against the upstream service directly:

  • Fails in both: the service is wrong.
  • Passes upstream, fails through the gateway: the gateway configuration is wrong.

With the Portman GitHub Action, a matrix runs both:

yaml
jobs:  contract:    runs-on: ubuntu-latest    strategy:      fail-fast: false      matrix:        target:          - { name: gateway, url: 'https://gateway.staging.example.com/api/v1' }          - { name: upstream, url: 'http://orders.internal:8080' }    steps:      - uses: actions/checkout@v4      - uses: apideck-libraries/portman-action@v1        with:          spec: openapi.yaml          base-url: ${{ matrix.target.url }}          artifact-name: portman-${{ matrix.target.name }}        env:          PORTMAN_API_KEY: ${{ secrets.STAGING_GATEWAY_KEY }}

Gateway credentials

Pass credentials as PORTMAN_ environment variables so they never live in the config: PORTMAN_API_KEY becomes {{apiKey}}.

If the spec already describes the gateway's security scheme, Portman fills it in. If the gateway adds auth that the spec doesn't describe, set it for the whole collection with securityOverwrites in globals:

json
{  "globals": {    "securityOverwrites": {      "apiKey": {        "key": "x-api-key",        "in": "header",        "value": "{{apiKey}}"      }    }  }}

For headers the gateway requires on some routes only, such as a tenant or version header, use overwriteRequestHeaders in overwrites.

Rate limits

A full run sends one request per operation, plus one per variation and fuzzed value. If the gateway rate-limits the test client, space the requests out with Newman's delayRequest (milliseconds) in a Newman options file:

json
{  "delayRequest": 250}

Or give the test client its own plan or key with a higher limit, and test heavy operations separately with a filter.

Who does this

Tyk, the API gateway vendor, contract-tests its own gateway API with Portman in CI. Its tests check requests, responses, content types and headers against the gateway's OpenAPI spec (TykTechnologies/tyk, ci/tests/specs).

At runtime

A gateway passes CI once and then keeps changing. Someone edits a route in the console, a plugin is upgraded, a certificate or header policy is replaced, or the upstream deploys a change that skipped CI. None of that goes through your pull requests, so tests that run only in CI miss it.

Run the same tests on a schedule against production, through the gateway. Portman then works as a synthetic monitor for your contract: every few minutes or hours, it checks that each route still requires auth, still validates input and still returns what the spec promises.

yaml
name: Production contract monitoron:  schedule:    - cron: '*/30 * * * *'   # every 30 minutes  workflow_dispatch: jobs:  monitor:    runs-on: ubuntu-latest    steps:      - uses: actions/checkout@v4      - id: portman        uses: apideck-libraries/portman-action@v1        with:          spec: openapi.yaml          base-url: https://api.example.com/v1          preset: security          filter: read-only.json   # no writes against production        env:          PORTMAN_API_KEY: ${{ secrets.PROD_READONLY_KEY }}      - if: failure()        run: echo "::error::${{ steps.portman.outputs.failed }} contract checks failed in production"        # replace with your alerting: Slack, PagerDuty, an issue

read-only.json is a filter that removes the write operations, so the monitor never creates, changes or deletes production data, even if auth turns out to be broken:

json
{  "methods": ["post", "put", "patch", "delete"]}

Give the monitor its own read-only key. Then a failing auth check means the gateway let a request through that it should have blocked, not that the key lacked access.

What the schedule catches:

Change after releaseCheck that fails
A route loses its auth plugin or authorizerRequests without or with forged credentials get a 2xx instead of 401 or 403
Request validation is switched offOut-of-range query parameters reach the upstream and return a 2xx or 5xx
An upstream deploy changes a responseJSON schema contract test, or undeclared fields in strict mode
A header policy is replacedHSTS, nosniff or CORS checks
A route is removed or misroutedStatus code and content type checks

Portman Monitor runs exactly this daily against public API specs, and shows what the results look like.

Validate live traffic in the gateway

A schedule checks your API with test requests. To check every real request, turn on the gateway's own OpenAPI validation and give it the same spec. Most gateways validate requests; few validate responses:

GatewayRequestsResponsesEnabled perNon-blocking mode
Tyk OASBody, headers, path and query parameters (422 on failure)NoEndpointNo
Kong (OAS Validation plugin, Enterprise)Body (JSON), path, query, header and cookie parametersJSON bodyService or routeResponse failures can notify only
AWS API Gateway (REST APIs)Body and parametersNoAPI or methodNo
Azure API ManagementBody, parametersBody, headers, status codesPolicy scopedetect logs without blocking
Apigee (OASValidation policy)Path, verb, parameters, optional bodyBody present, headersProxy flowcontinueOnError
ZuploBody, headers, path and query parametersNoRouteLog-only per part

Two things follow from the table:

  • Validation is opt-in, route by route. A new endpoint, an imported spec without the validation flag, or a validator left in a log-only mode lets out-of-spec requests through without an error anywhere. Portman's fuzzed requests fail on exactly those routes, because they expect a 4xx and get a 2xx or 5xx.
  • Responses are mostly unchecked. Only Kong (Enterprise) and Azure validate response bodies. Everywhere else, a response that drifts from the spec reaches your consumers. Portman's contract tests check every response, in CI and on a schedule.

The two work together. The gateway rejects out-of-spec requests as they arrive. Portman proves that this validation is on for every route, and checks the responses the gateway doesn't.

What stays with the gateway

Portman doesn't sit in the request path and can't block a live request. Rate limiting, bot detection, threat protection and traffic analytics stay with the gateway. Portman checks that the gateway is configured the way your spec says, before release and continuously after it.