| name | api-testing |
| description | Test APIs with Newman/Postman collections, contract testing with Pact, mock servers, and GraphQL testing. |
| metadata | {"thinkfleetbot":{"emoji":"🧪","requires":{"bins":["curl","jq"]}}} |
API Testing
Comprehensive API testing: functional, contract, mock, and GraphQL.
Functional API Testing with curl
REST endpoints
response=$(curl -s -w "\n%{http_code}" https://api.example.com/users/1)
body=$(echo "$response" | head -n -1)
status=$(echo "$response" | tail -1)
echo "Status: $status"
echo "$body" | jq .
curl -s -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d '{"name": "Test User", "email": "test@example.com"}' | jq .
curl -s -X PUT https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d '{"name": "Updated User"}' | jq .
curl -s -X DELETE https://api.example.com/users/1 \
-H "Authorization: Bearer $API_TOKEN" -w "\nHTTP %{http_code}\n"
curl -s -X PATCH https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-d '{"status": "active"}' | jq .
Validate response schema
curl -s https://api.example.com/users/1 | jq 'has("id", "name", "email")'
curl -s https://api.example.com/users | jq 'length'
curl -s https://api.example.com/users/1 | jq '{id_is_number: (.id | type == "number"), name_is_string: (.name | type == "string")}'
Newman (Postman CLI)
newman run collection.json
newman run collection.json -e environment.json
newman run collection.json --reporters cli,htmlextra --reporter-htmlextra-export report.html
newman run collection.json --folder "User CRUD"
newman run collection.json -d test-data.csv -n 10
newman run collection.json --bail
Contract Testing (Pact)
Consumer side (JavaScript example)
npm install --save-dev @pact-foundation/pact
npx jest --testPathPattern=pact
ls pacts/
Provider verification
npx jest --testPathPattern=pact-verification
pact-provider-verifier --provider-base-url http://localhost:3000 \
--pact-url pacts/consumer-provider.json
Mock Servers
Prism (OpenAPI mock)
npx @stoplight/prism-cli mock openapi.yaml
npx @stoplight/prism-cli mock openapi.yaml --dynamic
npx @stoplight/prism-cli proxy openapi.yaml http://localhost:3000
Simple mock with Node
npx json-server --watch db.json --port 3001
GraphQL Testing
curl -s -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ users { id name email } }"}' | jq .
curl -s -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"query": "mutation { createUser(name: \"Test\", email: \"t@t.com\") { id } }"}' | jq .
curl -s -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ __schema { types { name } } }"}' | jq '.data.__schema.types | length'
curl -s -X POST https://api.example.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "query GetUser($id: ID!) { user(id: $id) { name } }", "variables": {"id": "1"}}' | jq .
Notes
- Test error responses too — send bad data and verify proper 400/422 responses.
- Contract tests prevent integration breakage between services. Run them in CI.
- Mock servers unblock frontend development when backend isn't ready.
- GraphQL introspection should be disabled in production — test for it.
- Always test with authentication. Unauthenticated endpoints are a security risk.