Examples

Explore practical examples of using HttpProbe for different API testing scenarios.

Table of contents

  1. Basic API Tests
    1. Simple GET Request
    2. Simple POST Request
  2. Authentication Tests
    1. JWT Authentication
    2. OAuth2 Authentication
  3. CRUD API Tests
    1. Complete CRUD Example
  4. Advanced Features
    1. Environment-Specific Tests
    2. Dynamic Data Generation
    3. Using Test Hooks
    4. Schema Validation
  5. Integration Tests
    1. Microservices Integration

Basic API Tests

Simple GET Request

name: "Simple API Test"
description: "Basic GET request test"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
suites:
  - name: "Basic Tests"
    cases:
      - title: "Get API Status"
        request:
          method: GET
          url: "${base_url}/status"
          headers:
            - key: Accept
              value: application/json
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              $.status: "ok"

Simple POST Request

name: "User Creation Test"
description: "Test user creation API"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
suites:
  - name: "User Management"
    cases:
      - title: "Create New User"
        request:
          method: POST
          url: "${base_url}/users"
          headers:
            - key: Content-Type
              value: application/json
            - key: Accept
              value: application/json
          body:
            type: json
            data: |
              {
                "name": "John Doe",
                "email": "john@example.com",
                "password": "securePassword123"
              }
          assertions:
            status: 201
            body:
              "$.success": true
              "$.user.name": "John Doe"
              "$.user.email": "john@example.com"

Authentication Tests

JWT Authentication

name: "Authentication API Tests"
description: "Test authentication endpoints"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
  username:
    type: string
    value: "testuser"
  password:
    type: string
    value: "${env:TEST_PASSWORD}"
suites:
  - name: "Authentication"
    cases:
      - title: "Login and Get Token"
        request:
          method: POST
          url: "${base_url}/auth/login"
          headers:
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "username": "${username}",
                "password": "${password}"
              }
          assertions:
            status: 200
            body:
              $.success: true
              $.token: "contains eyJ"  # JWT tokens start with eyJ
              $.expiresIn: "> 0"
          export:
            body:
              - path: $.token
                as: jwt_token

OAuth2 Authentication

name: "OAuth2 Authentication Tests"
description: "Test OAuth2 flow"
variables:
  auth_url:
    type: string
    value: "https://auth.example.com"
  client_id:
    type: string
    value: "${env:OAUTH_CLIENT_ID}"
  client_secret:
    type: string
    value: "${env:OAUTH_CLIENT_SECRET}"
suites:
  - name: "OAuth Flow"
    cases:
      - title: "Get Access Token"
        request:
          method: POST
          url: "${auth_url}/oauth/token"
          headers:
            - key: Content-Type
              value: application/x-www-form-urlencoded
          body:
            type: form
            data: |
              grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}
          assertions:
            status: 200
            body:
              $.access_token: "length > 0"
              $.token_type: "Bearer"
              $.expires_in: "> 0"
        export:
          body:
            - path: $.access_token
              as: oauth_token

CRUD API Tests

Complete CRUD Example

name: "Product API CRUD Tests"
description: "Test Create, Read, Update, Delete operations for products"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
  auth_token:
    type: string
    value: "${env:AUTH_TOKEN}"
suites:
  - name: "Product CRUD"
    cases:
      - title: "Create Product"
        request:
          method: POST
          url: "${base_url}/products"
          headers:
            - key: Authorization
              value: Bearer ${auth_token}
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "name": "Test Product",
                "price": 99.99,
                "category": "Electronics",
                "inStock": true
              }
          assertions:
            status: 201
            body:
              $.success: true
              $.product.id: "length > 0"  # Check that ID was generated
              $.product.name: "Test Product"
        export:
          body:
            - path: $.product.id
              as: product_id
              
      - title: "Get Product"
        request:
          method: GET
          url: "${base_url}/products/${product_id}"  # Using the exported product_id
          headers:
            - key: Authorization
              value: Bearer ${auth_token}
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              $.name: "Test Product"
              $.price: 99.99
              $.category: "Electronics"
              $.inStock: true
              
      - title: "Update Product"
        request:
          method: PUT
          url: "${base_url}/products/${product_id}" 
          headers:
            - key: Authorization
              value: Bearer ${auth_token}
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "name": "Updated Product",
                "price": 149.99
              }
          assertions:
            status: 200
            body:
              $.success: true
              $.product.name: "Updated Product"
              $.product.price: 149.99
              $.product.category: "Electronics"  # Unchanged field
              
      - title: "Delete Product"
        request:
          method: DELETE
          url: "${base_url}/products/${product_id}"
          headers:
            - key: Authorization
              value: Bearer ${auth_token}
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              $.success: true
              $.message: "contains deleted"

Advanced Features

Environment-Specific Tests

name: "Environment-Specific Tests"
description: "Tests that adapt to different environments"
variables:
  env_name:
    type: string
    value: "${env:TARGET_ENV}"  # dev, staging, prod
  base_url:
    type: string
    value: "${env:${env_name}_API_URL}"  # Uses dynamic environment variables
suites:
  - name: "Core API"
    cases:
      - title: "Check Environment"
        request:
          method: GET
          url: "${base_url}/info"
          headers:
            - key: Accept
              value: application/json
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              "$.environment": "${env_name}"

Dynamic Data Generation

name: "Dynamic Data Tests"
description: "Tests using dynamically generated data"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
  request_id:
    type: string
    value: "${uuid()}"  # Generate a UUID
  timestamp:
    type: string
    value: "${now()}"  # Current timestamp in milliseconds
suites:
  - name: "Logging API"
    cases:
      - title: "Log Event"
        request:
          method: POST
          url: "${base_url}/events"
          headers:
            - key: Content-Type
              value: application/json
            - key: X-Request-ID
              value: ${request_id}
          body:
            type: json
            data: |
              {
                "event": "TEST_EVENT",
                "timestamp": "${timestamp}",
                "data": {
                  "requestId": "${request_id}",
                  "source": "httpprobe_test"
                }
              }
          assertions:
            status: 201
            body:
              "$.success": true

Using Test Hooks

name: "Test With Hooks Example"
description: "Demonstrates how to use hooks for setup and data preparation"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
# Reference another test file that will be executed before this one
# Typically used for authentication or setup
before_all:
  - auth/login.test.yaml
suites:
  - name: "Protected API"
    cases:
      - title: "Access Protected Resource"
        request:
          method: GET
          url: "${base_url}/protected-resource"
          headers:
            - key: Authorization
              value: Bearer ${access_token}  # Token from login.test.yaml
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              $.success: true
              $.data: "length > 0"

Schema Validation

name: "Schema Validation Tests"
description: "Tests that validate response schema"
variables:
  base_url:
    type: string
    value: "https://api.example.com"
suites:
  - name: "User API"
    cases:
      - title: "Get User with Schema Validation"
        request:
          method: GET
          url: "${base_url}/users/123"
          headers:
            - key: Accept
              value: application/json
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              schema: |
                {
                  "type": "object",
                  "required": ["id", "name", "email", "createdAt"],
                  "properties": {
                    "id": { 
                      "type": "integer",
                      "minimum": 1
                    },
                    "name": { 
                      "type": "string",
                      "minLength": 1
                    },
                    "email": { 
                      "type": "string",
                      "format": "email"
                    },
                    "createdAt": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "role": {
                      "type": "string",
                      "enum": ["admin", "user", "guest"]
                    },
                    "settings": {
                      "type": "object",
                      "properties": {
                        "notifications": { "type": "boolean" },
                        "theme": { "type": "string" }
                      }
                    }
                  }
                }

Integration Tests

Microservices Integration

name: "Order Processing Flow"
description: "Test the complete order flow across multiple services"
variables:
  auth_url:
    type: string
    value: "https://auth.example.com"
  order_url:
    type: string
    value: "https://orders.example.com"
  payment_url:
    type: string
    value: "https://payments.example.com"
  username:
    type: string
    value: "${env:TEST_USERNAME}"
  password:
    type: string
    value: "${env:TEST_PASSWORD}"
suites:
  - name: "E2E Order Flow"
    cases:
      - title: "1. Login"
        request:
          method: POST
          url: "${auth_url}/login"
          headers:
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "username": "${username}",
                "password": "${password}"
              }
          assertions:
            status: 200
            body:
              "$.token": "length > 0"
          export:
            body:
              - path: $.token
                as: access_token
              
      - title: "2. Create Order"
        request:
          method: POST
          url: "${order_url}/orders"
          headers:
            - key: Authorization
              value: Bearer ${access_token}  # From previous test
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "items": [
                  {"productId": "prod-123", "quantity": 2},
                  {"productId": "prod-456", "quantity": 1}
                ],
                "shippingAddress": {
                  "street": "123 Main St",
                  "city": "Anytown",
                  "zipCode": "12345"
                }
              }
          assertions:
            status: 201
            body:
              "$.orderId": "length > 0"
              "$.total": "> 0"
          export:
            body:
              - path: $.orderId
                as: order_id
              
      - title: "3. Process Payment"
        request:
          method: POST
          url: "${payment_url}/process"
          headers:
            - key: Authorization
              value: Bearer ${access_token}
            - key: Content-Type
              value: application/json
          body:
            type: json
            data: |
              {
                "orderId": "${order_id}",
                "paymentMethod": "credit_card",
                "cardDetails": {
                  "number": "4111111111111111",
                  "expiry": "12/25",
                  "cvv": "123"
                }
              }
          assertions:
            status: 200
            body:
              "$.success": true
              "$.transactionId": "length > 0"
              
      - title: "4. Check Order Status"
        request:
          method: GET
          url: "${order_url}/orders/${order_id}"
          headers:
            - key: Authorization
              value: Bearer ${access_token}
          body:
            type: json
            data: null
          assertions:
            status: 200
            body:
              $.status: "paid"
              $.paymentDetails.transactionId: "length > 0"

These examples cover a wide range of API testing scenarios and showcase HttpProbe’s features for creating comprehensive and maintainable test suites.