api-stubs

command module
v0.0.0-...-ffade1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 16 Imported by: 0

README

Go Report Card Coverage Status CI build License

Go Mock Server

A flexible and powerful mock server implementation in Go that allows you to define mock endpoints with advanced request matching capabilities and templated responses.

Features

  • Flexible Request Matching:

    • URL path matching with templates (e.g., /users/{id})
    • Regular expression pattern matching
    • Query parameter validation
    • Request body validation
    • Multiple matching patterns: equalTo, matches, doesNotMatch, contains, doesNotContain
  • Powerful Response Handling:

    • Template-based response bodies with access to request parameters
    • File-based response bodies
    • Custom HTTP status codes
    • Custom response headers

Installation

go get github.com/dev-shimada/go-mock-server

Usage

  1. Create a configuration file in /configs/config.json:
[
  {
    "request": {
      "urlPathTemplate": "/users/{id}",
      "method": "GET",
      "pathParameters": {
        "id": {
          "matches": "^[0-9]+$"
        }
      }
    },
    "response": {
      "status": 200,
      "body": "{\"id\": \"{{.Path.id}}\", \"name\": \"User {{.Path.id}}\"}"
    }
  }
]
  1. Run the server:
go run main.go

The server will start on port 8080 by default.

Configuration Format

Request Matching
{
  "request": {
    "urlPathTemplate": "/example/{param}",  // URL template with path parameters
    "method": "GET",                        // HTTP method
    "pathParameters": {                     // Path parameter validation rules
      "param": {
        "equalTo": "value",                 // Exact match
        "matches": "^[0-9]+$",             // Regex pattern match
        "doesNotMatch": "[a-z]+",          // Negative regex pattern match
        "contains": "substring",            // String contains
        "doesNotContain": "substring"       // String does not contain
      }
    },
    "queryParameters": {                    // Query parameter validation
      "param": {
        // Same matching rules as pathParameters
      }
    },
    "body": {                              // Request body validation
      // Same matching rules as parameters
    }
  }
}
Response Configuration
{
  "response": {
    "status": 200,                         // HTTP status code
    "body": "Response content",            // Direct response content
    "bodyFileName": "response.json",       // OR file-based response
    "headers": {                           // Custom response headers
      "Content-Type": "application/json"
    }
  }
}
Template Variables

In response bodies, you can use the following template variables:

  • Path parameters: {{.Path.paramName}}
  • Query parameters: {{.Query.paramName}}

Example Configurations

  1. Basic endpoint with path parameter:
{
  "request": {
    "urlPathTemplate": "/users/{id}",
    "method": "GET",
    "pathParameters": {
      "id": {
        "matches": "^[0-9]+$"
      }
    }
  },
  "response": {
    "status": 200,
    "body": "{\"id\": \"{{.Path.id}}\", \"name\": \"User {{.Path.id}}\"}"
  }
}
  1. Endpoint with file-based response:
{
  "request": {
    "urlPathTemplate": "/data/{type}",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "bodyFileName": "responses/data.json"
  }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Read this in other languages: 日本語

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL