killgrave

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2019 License: MIT Imports: 12 Imported by: 0

README

CircleCI codecov Go Report Card GoDoc FriendsOfGo

Golang Killgrave

Killgrave

Killgrave is a simulator for HTTP-based APIs, in simple words a Mock Server, very easy to use made in Go.

Getting started

Install killgrave using go:

$ GO111MODULE=off go get -u github.com/friendsofgo/killgrave/cmd/killgrave

Install killgrave using homebrew:

$ brew install friendsofgo/tap/killgrave

Use killgrave with default flags:

$ killgrave
2019/04/14 23:53:26 The fake server is on tap now: http://localhost:3000

Or custome your server with this flags:

 -host string
        if you run your server on a different host (default "localhost")
 -imposters string
        directory where your imposter are saved (default "imposters")
 -port int
        por to run the server (default 3000)
 -version
        show the version of the application

How to use

Create an imposter

You must be create an imposter to start to use the application

imposters/create_gopher.json

{
    "request": {
        "method": "POST",
        "endpoint": "/gophers",
        "schema_file": "schemas/create_gopher_request.json",
        "headers": {
            "Content-Type": [
                "application/json"
            ]
        }
    },
    "response": {
        "status": 200,
        "content_type": "application/json",
        "bodyFile": "responses/create_gopher_response.json"
    }
}

And its related files

schemas/create_gopher_request.json
{
    "type": "object",
    "properties": {
        "data": {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "enum": [
                        "gophers"
                    ]
                },
                "attributes": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "color": {
                            "type": "string"
                        },
                        "age": {
                            "type": "integer"
                        }
                    },
                    "required": [
                        "name",
                        "color",
                        "age"
                    ]
                }
            },
            "required": [
                "type",
                "attributes"
            ]
        }
    },
    "required": [
        "data"
    ]
}
responses/create_gopher_response.json
{
    "data": {
        "type": "gophers",
        "id": "01D8EMQ185CA8PRGE20DKZTGSR",
        "attributes": {
            "name": "Zebediah",
            "color": "Purple",
            "age": 55
        }
    }
}

And then with the server on tap you can execute your request:

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{
            "data": {
                "type": "gophers",
                "attributes": {
                "name": "Zebediah",
                "color": "Purple",
                "age": 55
                }
            }
    }' \
  http://localhost:3000/gophers

Features

  • Imposters created in json
  • Validate json schemas on requests
  • Validate requests headers
  • Check response status
  • All content-type bodies
  • Write body files (XML, JSON, HTML...)
  • Write bodies in line
  • Regex for using on endpoint urls

Next Features

  • Validate headers on body
  • Proxy server
  • Dynamic responses and error responses
  • Record proxy server
  • Better documentation with examples of each feature

Contributing

Contributions are more than welcome, if you are interested please fork this repo and send your Pull Request.

License

MIT License, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ImposterHandler

func ImposterHandler(imposter Imposter) http.HandlerFunc

ImposterHandler create specific handler for the received imposter

Types

type Imposter

type Imposter struct {
	Request  Request  `json:"request"`
	Response Response `json:"response"`
}

Imposter define an imposter structure

type Request

type Request struct {
	Method     string       `json:"method"`
	Endpoint   string       `json:"endpoint"`
	SchemaFile *string      `json:"schema_file"`
	Headers    *http.Header `json:"headers"`
}

Request represent the structure of real request

type Response

type Response struct {
	Status      int     `json:"status"`
	Body        string  `json:"body"`
	BodyFile    *string `json:"bodyFile"`
	ContentType string  `json:"content_type"`
}

Response represent the structure of real response

type Server

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

Server definition of mock server

func NewServer

func NewServer(p string, r *mux.Router) *Server

NewServer initialize the mock server

func (*Server) Run

func (s *Server) Run() error

Run read all the files on the impostersPath and creates different handlers for each imposter

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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