dolus

package module
v0.0.0-...-00d9010 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 15 Imported by: 0

README

Dolus Mock Server

Overview

Dolus is a mock server designed to simulate HTTP-based APIs. It's useful in testing scenarios where you need to mimic the behavior of external services. Dolus allows you to define expectations for incoming requests and specify the responses that should be returned.

Key Features

  • Expectation Engine: Dolus uses an expectation engine to match incoming requests against a set of predefined expectations. Each expectation can specify the HTTP method, path, headers, and query parameters of the request.
  • Dynamic Response Generation: Dolus can generate dynamic responses based on the incoming request and the matched expectation. This includes generating random data for certain fields in the response body.
  • Schema Generation: Dolus includes functionality for generating schemas from different types of input, including OpenAPI 3 examples and arbitrary data structures.
  • Logging: Dolus includes a logging package for detailed logging of the server's operations.

How to Use

  1. Define your expectations in a .cue file. Each expectation should be an object that includes the following fields:

    • priority: The priority of the expectation. If multiple expectations match a request, the one with the highest priority is used.
    • request: An object that describes the request. This includes the following fields:
      • path: The path of the request.
      • method: The HTTP method of the request.
      • headers: An object that maps header names to expected values. The values can be either strings or arrays of strings.
      • params: An object that includes query and path fields. Both of these are objects that map parameter names to expected values.
    • response: An object that describes the response. This includes the following fields:
      • body: An object that describes the body of the response. This can include fields that are generated dynamically, such as id: task.#GenInt32 & {min: 80, max: 100} which generates a random int32 number between 80 and 100 for the id field.
      • status: The HTTP status code of the response.

    Here's an example:

    expectation.#Expectation & {
        priority: 1
        request: expectation.#Request & {
            path:   "/store/order/{orderId}"
            method: "GET"
            headers: {
                "Content-Type": ["application/json"]
            }
            params: {
                query: {
                    value: [3,7]
                }
                path: {
                    orderId: 3
                }
            }
        }
        response: expectation.#Response & {
            body: {
                petId: {
                    id: task.#GenInt32 & {min: 80, max: 100}
                }
                complete: true
                status:   "good day"
            }
            status: 200
        }
    }
    
  2. Pass your .cue file into the Dolus server. The server will parse the .cue file and load the expectations.

  3. Start the Dolus server. The server will listen for incoming requests and match them against the predefined expectations. If a match is found, the server will return the corresponding response.


Documentation

Index

Constants

View Source
const (
	Version = "0.0.1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client to the Dolus server

func Connect

func Connect(ctx context.Context, address string) (*Client, error)

Connect to the Dolus server

func (*Client) AddExpectation

func (c *Client) AddExpectation(expectation expectation.Expectation) error

AddExpectation to the Dolus server

type Server

type Server struct {
	OpenAPIspec      string
	HideBanner       bool
	HidePort         bool
	EchoServer       *echo.Echo
	GenerationConfig generator.GenerationConfig
	// contains filtered or unexported fields
}

func New

func New() *Server

func (*Server) AddCueExpectationsFromFolder

func (d *Server) AddCueExpectationsFromFolder()

func (*Server) AddExpectations

func (d *Server) AddExpectations(files ...string)

func (*Server) Start

func (d *Server) Start(address string) error

Directories

Path Synopsis
cmd
internal
api
Package api provides primitives to interact with the openapi HTTP API.
Package api provides primitives to interact with the openapi HTTP API.
pkg

Jump to

Keyboard shortcuts

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