mockcond

package
v0.0.0-...-d65dd4f Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 7 Imported by: 0

README

Description

This package is used by mockserver it provides common HTTP request checks.

Key Features

  • Validate HTTP request properties (method, headers, query parameters, body, etc.).
  • Combine simple conditions into complex, nested conditional expressions.
  • Extendable by implementing custom conditions.

Usage

Simple Conditions

Simple conditions check if an HTTP request meets specific criteria. You can create your own custom conditions by implementing the mockcond.Condition interface.

Examples

Match a specific URL path suffix:

If:   mockcond.PathSuffix("/v2/tasks"),

Ensure the request method is POST:

If:    mockcond.MethodPOST(),

Check for a query parameter with a specific value:

If:    mockcond.QueryParam("startAt", "17"),

Ensure a query parameter is missing:

If:    mockcond.QueryParamsMissing("updated_at[gte]"),

Check for a specific header:

If:    mockcond.Header(testApiVersionHeader),

Match the exact request body content:

If:    mockcond.Body(bodyRequest),

Complex Conditions

You can combine conditions using logical operators like And & Or. These operators allow for nested conditions, enabling you to create complex validation expressions that resemble logical conditionals in code.

Examples

Check for a header and ensure the method is DELETE:

If: mockcond.And{
	mockcond.MethodDELETE(),
	mockcond.Header(testApiVersionHeader),
},

Match a specific URL path suffix for either GET or POST methods:

If: mockcond.And{
    mockcond.PathSuffix("/api/resource"),
    mockcond.Or{
        mockcond.MethodGET(),
        mockcond.MethodPOST(),
    },
},

Custom Conditions

You can create custom conditions by implementing the mockcond.Condition interface to suit your specific requirements. Here is a basic example of how to create a custom condition:

type CustomCondition struct {}

func (c CustomCondition) EvaluateCondition(w http.ResponseWriter, r *http.Request) bool {
	// Implementation goes here. 
	return true
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type And

type And []Condition

And is a composite Condition which evaluates to true if all conditions are met. Empty list returns false.

func (And) EvaluateCondition

func (a And) EvaluateCondition(w http.ResponseWriter, r *http.Request) bool

type Check

type Check func(w http.ResponseWriter, r *http.Request) bool

Check is the most basic Condition. It is a function definition that allows custom implementation. There are some out of the box functions in this package that have this signature.

func Body

func Body(expected string) Check

Body returns a check expecting body to match template text.

func BodyBytes

func BodyBytes(expected []byte) Check

BodyBytes returns a check expecting body to match template bytes.

func Header(header http.Header) Check

func Method

func Method(methodName string) Check

Method returns a check expecting HTTP method name to match the template.

func MethodDELETE

func MethodDELETE() Check

func MethodPATCH

func MethodPATCH() Check

func MethodPOST

func MethodPOST() Check

func MethodPUT

func MethodPUT() Check

func PathSuffix

func PathSuffix(expectedSuffix string) Check

PathSuffix returns a check expecting request URL path to match the template.

func QueryParam

func QueryParam(key string, value ...string) Check

func QueryParamsMissing

func QueryParamsMissing(keys ...string) Check

func (Check) EvaluateCondition

func (c Check) EvaluateCondition(w http.ResponseWriter, r *http.Request) bool

type Condition

type Condition interface {
	EvaluateCondition(w http.ResponseWriter, r *http.Request) bool
}

Condition computes whether http.Request meets some rule.

type Or

type Or []Condition

Or is a composite Condition which evaluates to true if at least one condition is met. Empty list returns false.

func (Or) EvaluateCondition

func (o Or) EvaluateCondition(w http.ResponseWriter, r *http.Request) bool

Jump to

Keyboard shortcuts

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