Write a local API smoke test with Hurl

Use a localhost JSON fixture to write a passing Hurl assertion, then verify that an incorrect expectation fails.

how-to5 steps

Updated

in short

Put the request and response assertions in a .hurl file. Run it with --test and check the exit status to distinguish success from failure.

Use the small fixture below to check the workflow before applying it to your own project.

Tools used

Before you start

  • Hurl and Python 3 installed
  • Two terminal windows on Linux or macOS
  • TCP port 18765 available on localhost

Steps

  1. Step 1: Create a local response

    Keep this terminal open. Python 3 provides the fixture server; port 18765 must be free.

    sh
    trmn_guide_dir="$(mktemp -d)"
    cd "$trmn_guide_dir"
    printf '%s\n' '{"status":"ok","items":3}' > health.json
    python3 -m http.server 18765 --bind 127.0.0.1
  2. Step 2: Write the smoke test

    In a second terminal, create this request file in another temporary directory.

    sh
    trmn_guide_dir="$(mktemp -d)"
    cd "$trmn_guide_dir"
    cat > health.hurl <<'EOF'
    GET http://127.0.0.1:18765/health.json
    HTTP 200
    [Asserts]
    jsonpath "$.status" == "ok"
    jsonpath "$.items" == 3
    EOF
  3. Step 3: Run the passing case

    Hurl prints a test report. The exit status below should be zero.

    sh
    hurl --test health.hurl
    printf "exit=%s\n" "$?"
  4. Step 4: Confirm that a bad assertion fails

    Change only the expected count in a second file. The exit status should be nonzero.

    sh
    sed 's/== 3/== 4/' health.hurl > failing.hurl
    hurl --test failing.hurl
    printf 'exit=%s\n' "$?"
  5. Step 5: Stop the fixture server

    Return to the first terminal and press Ctrl-C. The server is bound to localhost.

Further reading

[Hurl assertions](https://hurl.dev/docs/asserting-response.html)

FAQ

Why test a failure too?
It confirms that your assertion actually checks the value you care about.
Is this a production API example?
No. It tests a local static JSON response so you can learn the assertion workflow without credentials.

related