mockServerClient

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

README

MockServer Client for golang

Why I created this?

I couldn't find one already that supported the latest version of MockServer. There are some implementations out there but do not have all the capabilities of the MockServer.

Installation

go get github.com/zdenkoC/go-mock-server-client

Usage

import {
    m "github.com/zdenkoC/mockServerClient"
}

func main() {
    container = startMockServer() //starts mockServer as testcontainer. Not shown here
    mockServerUrl, _ := container.URL(context.Background())
    client := mockServerClient.New(mockServerUrl))

    client.RegisterExpectation(m.Expectation{
        Request:  m.Request{Path: "/", Headers: []m.HTTPHeader{{Name: "X-Session-Id", Values: []string{ctx.HttpSessionId}}}},
        Response: m.JsonResponse(map[string]any{"result": "alive"}),
        Times:    m.Once(),
        ID:       mockId(),
    })
    ...
    client.Verify(m.Verify{
        Request: m.Request{
			Path: "/", 
			Headers: []m.HTTPHeader{{Name: "X-Session-Id", Values: []string{ctx.HttpSessionId}}}, 
		},
        Times:   m.VerifyTimes{AtLeast: 1, AtMost: 1},
    })
    ...
    client.ClearById(id)
}

License

Refer to the Golang license. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	Base64Bytes string   `json:"base64Bytes,omitempty"`
	Not         bool     `json:"not,omitempty"`
	Type        BodyType `json:"type,omitempty"`
	ContentType string   `json:"contentType,omitempty"`
}

func JsonBody

func JsonBody(value string) Body

func StringBody

func StringBody(value string) Body

type BodyType

type BodyType string
const (
	BINARY     BodyType = "BINARY"
	JSON       BodyType = "JSON"
	JSONSchema BodyType = "JSON_SCHEMA"
	JSONPath   BodyType = "JSON_PATH"
	Parameters BodyType = "PARAMETERS"
	Regex      BodyType = "REGEX"
	String     BodyType = "STRING"
	XML        BodyType = "XML"
	XMLSchema  BodyType = "XML_SCHEMA"
	XPath      BodyType = "XPATH"
	LogEvent   BodyType = "LOG_EVENT"
)

type ConnectionOptions

type ConnectionOptions struct {
	SuppressContentLengthHeader bool     `json:"suppressContentLengthHeader,omitempty"`
	ContentLengthHeaderOverride int      `json:"contentLengthHeaderOverride,omitempty"`
	SuppressConnectionHeader    bool     `json:"suppressConnectionHeader,omitempty"`
	ChunkSize                   int      `json:"chunkSize,omitempty"`
	KeepAliveOverride           bool     `json:"keepAliveOverride,omitempty"`
	CloseSocket                 bool     `json:"closeSocket,omitempty"`
	CloseSocketDelay            TimeUnit `json:"closeSocketDelay,omitempty"`
}

type Expectation

type Expectation struct {
	ID                           string                       `json:"id,omitempty"`
	Priority                     int                          `json:"priority,omitempty"`
	Request                      Request                      `json:"httpRequest,omitempty"`
	Response                     Response                     `json:"httpResponse,omitempty"`
	HTTPResponseTemplate         HTTPResponseTemplate         `json:"httpResponseTemplate,omitempty"`
	HTTPResponseClassCallback    HTTPResponseClassCallback    `json:"httpResponseClassCallback,omitempty"`
	HTTPResponseObjectCallback   HTTPResponseObjectCallback   `json:"httpResponseObjectCallback,omitempty"`
	HTTPForward                  HTTPForward                  `json:"httpForward,omitempty"`
	HTTPForwardTemplate          HTTPForwardTemplate          `json:"httpForwardTemplate,omitempty"`
	HTTPForwardClassCallback     HTTPForwardClassCallback     `json:"httpForwardClassCallback,omitempty"`
	HTTPForwardObjectCallback    HTTPForwardObjectCallback    `json:"httpForwardObjectCallback,omitempty"`
	HTTPOverrideForwardedRequest HTTPOverrideForwardedRequest `json:"httpOverrideForwardedRequest,omitempty"`
	HTTPError                    HTTPError                    `json:"httpError,omitempty"`
	Times                        Times                        `json:"times,omitempty"`
	TimeToLive                   TimeToLive                   `json:"timeToLive,omitempty"`
}

type ExpectationId

type ExpectationId struct {
	ID string `json:"id,omitempty"`
}

type HTTPCookie

type HTTPCookie struct {
	Name  string `json:"name,omitempty"`
	Value string `json:"value,omitempty"`
}

type HTTPCookies

type HTTPCookies struct {
	Add     []HTTPCookie `json:"add,omitempty"`
	Replace []HTTPCookie `json:"replace,omitempty"`
	Remove  []string     `json:"remove,omitempty"`
}

type HTTPError

type HTTPError struct {
	Delay          TimeUnit `json:"delay,omitempty"`
	DropConnection bool     `json:"dropConnection,omitempty"`
	ResponseBytes  string   `json:"responseBytes,omitempty"`
}

type HTTPForward

type HTTPForward struct {
	Delay  TimeUnit `json:"delay,omitempty"`
	Host   string   `json:"host,omitempty"`
	Port   int      `json:"port,omitempty"`
	Scheme string   `json:"scheme,omitempty"`
}

type HTTPForwardClassCallback

type HTTPForwardClassCallback struct {
	Delay         TimeUnit `json:"delay,omitempty"`
	CallbackClass string   `json:"callbackClass,omitempty"`
}

type HTTPForwardObjectCallback

type HTTPForwardObjectCallback struct {
	Delay            TimeUnit `json:"delay,omitempty"`
	ClientID         string   `json:"clientId,omitempty"`
	ResponseCallback bool     `json:"responseCallback,omitempty"`
}

type HTTPForwardTemplate

type HTTPForwardTemplate struct {
	Delay        TimeUnit `json:"delay,omitempty"`
	TemplateType string   `json:"templateType,omitempty"`
	Template     string   `json:"template,omitempty"`
}

type HTTPHeader

type HTTPHeader struct {
	Name   string   `json:"name,omitempty"`
	Values []string `json:"values,omitempty"`
}

type HTTPHeaders

type HTTPHeaders struct {
	Add     []HTTPHeader `json:"add,omitempty"`
	Replace []HTTPHeader `json:"replace,omitempty"`
	Remove  []string     `json:"remove,omitempty"`
}

type HTTPOverrideForwardedRequest

type HTTPOverrideForwardedRequest struct {
	Delay            TimeUnit         `json:"delay,omitempty"`
	RequestOverride  Request          `json:"requestOverride,omitempty"`
	RequestModifier  RequestModifier  `json:"requestModifier,omitempty"`
	ResponseOverride Response         `json:"responseOverride,omitempty"`
	ResponseModifier ResponseModifier `json:"responseModifier,omitempty"`
}

type HTTPResponseClassCallback

type HTTPResponseClassCallback struct {
	Delay         TimeUnit `json:"delay,omitempty"`
	CallbackClass string   `json:"callbackClass,omitempty"`
}

type HTTPResponseObjectCallback

type HTTPResponseObjectCallback struct {
	Delay            TimeUnit `json:"delay,omitempty"`
	ClientID         string   `json:"clientId,omitempty"`
	ResponseCallback bool     `json:"responseCallback,omitempty"`
}

type HTTPResponseTemplate

type HTTPResponseTemplate struct {
	Delay        TimeUnit `json:"delay,omitempty"`
	TemplateType string   `json:"templateType,omitempty"`
	Template     string   `json:"template,omitempty"`
}

type MockServerClient

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

func New

func New(uri string) *MockServerClient

func (*MockServerClient) Clear

func (msc *MockServerClient) Clear(request Request)

func (*MockServerClient) ClearAll

func (msc *MockServerClient) ClearAll()

func (*MockServerClient) ClearById

func (msc *MockServerClient) ClearById(expectationId string)

func (*MockServerClient) RegisterExpectation

func (msc *MockServerClient) RegisterExpectation(expectation Expectation)

func (*MockServerClient) Verify

func (msc *MockServerClient) Verify(verify Verify)

func (*MockServerClient) VerifySequence

func (msc *MockServerClient) VerifySequence(sequence VerifySequence)

type PathModifier

type PathModifier struct {
	Regex        string `json:"regex,omitempty"`
	Substitution string `json:"substitution,omitempty"`
}

type PathParameter

type PathParameter struct {
	Name   string   `json:"name,omitempty"`
	Values []string `json:"values,omitempty"`
}

type QueryParameter

type QueryParameter struct {
	Name   string   `json:"name,omitempty"`
	Values []string `json:"values,omitempty"`
}

func WithQueryParameter

func WithQueryParameter(name string, values ...string) []QueryParameter

type QueryStringParameters

type QueryStringParameters struct {
	Add     []QueryParameter `json:"add,omitempty"`
	Replace []QueryParameter `json:"replace,omitempty"`
	Remove  []string         `json:"remove,omitempty"`
}

type Request

type Request struct {
	Secure                bool             `json:"secure,omitempty"`
	KeepAlive             bool             `json:"keepAlive,omitempty"`
	Method                string           `json:"method,omitempty"`
	Path                  string           `json:"path,omitempty"`
	PathParameters        []PathParameter  `json:"pathParameters,omitempty"`
	QueryStringParameters []QueryParameter `json:"queryStringParameters,omitempty"`
	Body                  Body             `json:"body,omitempty"`
	Headers               []HTTPHeader     `json:"headers,omitempty"`
	Cookies               []HTTPCookie     `json:"cookies,omitempty"`
	SocketAddress         SocketAddress    `json:"socketAddress,omitempty"`
	Protocol              string           `json:"protocol,omitempty"`
}

func (*Request) WithPath

func (r *Request) WithPath(path string) *Request

type RequestModifier

type RequestModifier struct {
	Path                  PathModifier          `json:"path,omitempty"`
	QueryStringParameters QueryStringParameters `json:"queryStringParameters,omitempty"`
	Headers               HTTPHeaders           `json:"headers,omitempty"`
	Cookies               HTTPCookies           `json:"cookies,omitempty"`
}

type Response

type Response struct {
	Delay             TimeUnit          `json:"delay,omitempty"`
	Body              Body              `json:"body,omitempty"`
	Cookies           []HTTPCookie      `json:"cookies,omitempty"`
	ConnectionOptions ConnectionOptions `json:"connectionOptions,omitempty"`
	Headers           []HTTPHeader      `json:"headers,omitempty"`
	StatusCode        int               `json:"statusCode,omitempty"`
	ReasonPhrase      string            `json:"reasonPhrase,omitempty"`
}

func JsonResponse

func JsonResponse(val any) Response

func StringResponse

func StringResponse(val string) Response

type ResponseModifier

type ResponseModifier struct {
	Headers HTTPHeaders `json:"headers,omitempty"`
	Cookies HTTPCookies `json:"cookies,omitempty"`
}

type SocketAddress

type SocketAddress struct {
	Host   string `json:"host,omitempty"`
	Port   int    `json:"port,omitempty"`
	Scheme string `json:"scheme,omitempty"`
}

type TimeToLive

type TimeToLive struct {
	TimeUnit   string `json:"timeUnit,omitempty"`
	TimeToLive int    `json:"timeToLive,omitempty"`
	Unlimited  bool   `json:"unlimited,omitempty"`
}

type TimeUnit

type TimeUnit struct {
	TimeUnit string `json:"timeUnit,omitempty"`
	Value    int    `json:"value,omitempty"`
}

type Times

type Times struct {
	RemainingTimes int  `json:"remainingTimes,omitempty"`
	Unlimited      bool `json:"unlimited,omitempty"`
}

func Once

func Once() Times

type Verify

type Verify struct {
	ExpectationID                                       ExpectationId `json:"expectationId,omitempty"`
	Request                                             Request       `json:"httpRequest,omitempty"`
	Times                                               VerifyTimes   `json:"times"`
	MaximumNumberOfRequestToReturnInVerificationFailure int           `json:"maximumNumberOfRequestToReturnInVerificationFailure,omitempty"`
}

type VerifySequence

type VerifySequence struct {
	ExpectationIds                                      []ExpectationId `json:"expectationIds,omitempty"`
	Requests                                            []Request       `json:"httpRequests,omitempty"`
	MaximumNumberOfRequestToReturnInVerificationFailure int             `json:"maximumNumberOfRequestToReturnInVerificationFailure,omitempty"`
}

func Sequence

func Sequence() *VerifySequence

func (*VerifySequence) WithIds

func (vs *VerifySequence) WithIds(ids []string) *VerifySequence

type VerifyTimes

type VerifyTimes struct {
	AtLeast int `json:"atLeast"`
	AtMost  int `json:"atMost"`
}

func NeverCalled

func NeverCalled() VerifyTimes

Jump to

Keyboard shortcuts

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