mimic

package
Version: v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2017 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mimic contains a mockup tool with middleware.

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(h http.Handler) http.Handler

Handler middleware.

func Load

func Load(filename string) error

Load loads yml file with parameters for mocking.

func SetCatcher

func SetCatcher(c Catcher) error

SetCatcher sets the catcher implementation for middleware interference. You can implement your custom Catcher, by default JSONCatcher is set. In order to set your own catcher this function must be called at init time.

Types

type Cache

type Cache struct {
	Config *Config
	List   map[string]*Endpoint
	sync.RWMutex
}

Cache contains mock data.

func (*Cache) Get

func (c *Cache) Get(s string) (*Endpoint, error)

Get returns cached mockup data.

func (*Cache) Prepare

func (c *Cache) Prepare() error

Prepare will load the keys that default catcher returns from config file and store as cache map.

type Catcher

type Catcher interface {
	// Key method returns the key for the cache list.
	Key(interface{}) (string, error)

	// Catch method call Key and if exists returns Endpoint data.
	Catch(*http.Request) (interface{}, bool)

	// Render method allow custom http response when catch handles something.
	Render(http.ResponseWriter, interface{}) error
}

Catcher interface allows custom request handling. View JSONCatcher and XMLCatcher for a sample implementation.

type Config

type Config struct {
	Endpoints []*Endpoint `yaml:"endpoints"`
}

Config yaml.

type Endpoint

type Endpoint struct {
	Method   string            `yaml:"method"`
	Headers  map[string]string `yaml:"headers"`
	URI      string            `yaml:"uri"`
	Params   string            `yaml:"params"`
	Response *Response         `yaml:"response"`
}

Endpoint config.

func Get

func Get(s string) (*Endpoint, error)

Get return Endpoint data from cache if exists.

type JSONCatcher

type JSONCatcher struct{}

JSONCatcher implements Catcher.

func (*JSONCatcher) Catch

func (j *JSONCatcher) Catch(r *http.Request) (interface{}, bool)

Catch implements Catcher.

func (*JSONCatcher) Key

func (j *JSONCatcher) Key(v interface{}) (string, error)

Key implements Catcher.

func (*JSONCatcher) Render

func (j *JSONCatcher) Render(w http.ResponseWriter, v interface{}) error

Render implements Catcher.

type Response

type Response struct {
	Status  int               `yaml:"status"`
	Headers map[string]string `yaml:"headers"`
	Body    string            `yaml:"body"`
}

Response mock struct.

type XMLCatcher

type XMLCatcher struct{}

XMLCatcher implements Catcher. TODO;

func (*XMLCatcher) Catch

func (j *XMLCatcher) Catch(r *http.Request) (interface{}, bool)

Catch implements Catcher.

func (*XMLCatcher) Render

func (j *XMLCatcher) Render(w http.ResponseWriter, v interface{}) error

Render implements Catcher.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL