Skip to main content

E-commerce API Example

This example demonstrates how to test a typical e-commerce API using Dochia, covering scenarios like products, inventory, cart, and checkout flows.

Overview

  • Test product catalog and inventory endpoints
  • Validate cart and checkout workflows
  • Handle authentication and session management
  • Test payment and order processing

Prerequisites

  • OpenAPI spec for the e-commerce API (-c, --contract)
  • Test environment URL, e.g., https://api.store.example.com (-s, --server)
  • Test payment gateway credentials (if applicable)

Quick Start

# Basic test run
dochia test -c store-api.yml -s https://api.store.example.com

# Dry run to preview tests
dochia test -c store-api.yml -s https://api.store.example.com --dry-run

Common Scenarios

Product Catalog

# Test only product endpoints
dochia test -c store-api.yml -s https://api.store.example.com \
--path "/products" --path "/products/{id}" --path "/categories"

# Test product search with query params
dochia test -c store-api.yml -s https://api.store.example.com \
--query-params "q=phone&category=electronics&limit=10"

Shopping Cart

# Test cart operations with session token
dochia test -c store-api.yml -s https://api.store.example.com \
--path "/cart" --path "/cart/items" \
--header "Authorization=Bearer $SESSION_TOKEN"

# Or use reference data for required fields
dochia test -c store-api.yml -s https://api.store.example.com \
--reference-data "userId=test123,currency=USD,storeId=main"

Checkout Flow

# Test checkout with test payment method
dochia test -c store-api.yml -s https://api.store.example.com \
--path "/checkout/shipping" --path "/checkout/payment" --path "/orders" \
--header "Authorization=Bearer $SESSION_TOKEN" \
--reference-data "paymentMethodId=test_pm_123"

Inventory Management

# Test inventory updates (admin)
dochia test -c store-api.yml -s https://api.store.example.com \
--path "/admin/inventory" --path "/admin/products" \
--header "X-Admin-Key=$ADMIN_API_KEY"

Authentication & Sessions

Customer Login

# auth-script.sh
#!/bin/bash
# Get session token from login endpoint
echo "Bearer $(curl -s -X POST https://api.store.example.com/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"$TEST_PASSWORD"}' \
| jq -r '.token')"
# Run with token refresh
dochia test -c store-api.yml -s https://api.store.example.com \
--headers headers.yml \
--auth-refresh-script ./auth-script.sh \
--auth-refresh 300

Test Data Management

Reference Data

# reference.yml
all:
currency: "USD"
country: "US"

/products:
category: "electronics"

/checkout:
shippingMethod: "standard"
paymentMethod: "credit_card"

Security & Compliance

# Mask sensitive data in reports
dochia test -c store-api.yml -s https://api.store.example.com \
--mask-headers "Authorization,X-API-Key,Set-Cookie" \
-R "creditCardNumber=4111111111111111"

# Test with different user roles
dochia test -c store-api.yml -s https://api.store.example.com \
--tags "customer"

dochia test -c store-api.yml -s https://api.store.example.com \
--tags "admin" -H "X-Admin-Key=$ADMIN_API_KEY"

Performance Testing

# Test with rate limiting
dochia test -c store-api.yml -s https://api.store.example.com \
--max-requests-per-minute 200 \
--paths "/products,/categories"

# Monitor response times
dochia test -c store-api.yml -s https://api.store.example.com \
--max-response-time 2000 \
--execution-stats

Troubleshooting

  • List available endpoints:
    dochia list --paths -c store-api.yml
  • Debug authentication issues:
    dochia test -c store-api.yml -s https://api.store.example.com -vvv
  • Check for slow endpoints:
    dochia test -c store-api.yml -s https://api.store.example.com --execution-stats

See Also