stubble

package module
v0.0.0-...-0cf418b Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

Stubble

A flexible stub webservice.

License

Copyright 2023 Hayo van Loon

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

View Source
const Wildcard = ".*"

Variables

View Source
var NotFound = Rule{
	Name: "no_match",
	Response: Response{
		StatusCode: http.StatusNotImplemented,
		BodyString: "no response for request",
	},
}

NotFound is used when nothing matches. A 5xx is returned as a 404 would be easier to confuse with a normal prepared response.

Functions

This section is empty.

Types

type Handler

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

A Handler handles stubbed calls.

func FromFiles

func FromFiles(files ...string) (*Handler, error)

func New

func New(rules ...Rule) (*Handler, error)

func (*Handler) AddRule

func (h *Handler) AddRule(r *http.Request) error

func (*Handler) Close

func (h *Handler) Close() error

func (*Handler) GetRule

func (h *Handler) GetRule(r *http.Request, body []byte) (Rule, error)

GetRule retrieves the best matching response for the request.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Logger

type Logger interface {
	Debugf(format string, a ...any)
	Infof(format string, a ...any)
	Warnf(format string, a ...any)
	Errorf(format string, a ...any)
	Fatalf(format string, a ...any)
}

type Persistence

type Persistence interface {
	ListRules(context.Context) ([]Rule, error)
	SaveRule(context.Context, Rule) error
}

type Response

type Response struct {
	// Response status code
	StatusCode int `json:"statusCode"`
	// Response body string. Only one body field can be used.
	BodyString string `json:"bodyString"`
	// Response body JSON. Only one body field can be used
	BodyJSON any `json:"bodyJson"`
	// Response headers
	Headers map[string][]string `json:"headers"`
}

type Rule

type Rule struct {
	// The name of the rule.
	Name string `json:"name"`
	// A description, no functional impact.
	Description string `json:"description"`
	// HTTP method to match.
	Method string `json:"method"`
	// Path to match.
	Path string `json:"path"`
	// Path regex to match.
	PathRegex string `json:"pathRegex"`

	// Request query parameters. An entry with a zero-length value list will
	// only check for presence of the key.
	Params map[string][]string `json:"params"`
	// TODO(hvl): matching
	// Request headers. An entry with a zero-length value list will only check
	// for presence of the key.
	Headers map[string][]string `json:"headers"`
	// Request body to match as a string.
	BodyString string `json:"bodyString"`
	// Regex of body to match as a string.
	BodyStringRegex string `json:"bodyStringRegex"`
	// The JSON body to match. Field values can be a Wildcard.
	BodyJSON any `json:"body"`

	// The response to return.
	Response Response `json:"response"`
	// contains filtered or unexported fields
}

func InitRule

func InitRule(r Rule) (Rule, error)

func RuleFromRequest

func RuleFromRequest(r *http.Request) Rule

func (Rule) EqualMatch

func (ru Rule) EqualMatch(other Rule) bool

func (Rule) Match

func (ru Rule) Match(r *http.Request, body []byte) int

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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