bogus

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2016 License: MIT Imports: 6 Imported by: 0

README

Bogus

Build Status Coverage Go Reportcard GoDoc

Bogus simplifies the creation of a mocked http server using the net/http/httptest package. It allows the creation of one to many endpoints with unique responses. The interactions of each endpoint are recorded for assertions.

Usage

Setting a payload and status against a root path

import "github.com/gomicro/bogus"

...

	g.Describe("Tests needing a test server", func(){
		var server *bogus.Bogus

		g.BeforeEach(func(){
			server = bogus.New()
			server.SetPayload([]byte("some return payload"))
			server.SetStatus(200)
			server.Start()
		})

		g.It("should connect to a test server", func(){
			host, port := server.HostPort()

			...

			Expect(server.Hits()).To(Equal(1))
		})
	})

Setting a payload and status against a specific path

import "github.com/gomicro/bogus"

...

	g.Describe("Tests needing a test server", func(){
		var server *bogus.Bogus

		g.BeforeEach(func(){
			server = bogus.New()
			server.Start()
		})

		g.It("should connect to a test server", func(){
			server.AddPath("/foo/bar").
				SetPayload([]byte("some return payload")).
				SetStatus(200)
			host, port := server.HostPort()

			...

			Expect(server.Hits()).To(Equal(1))
		})
	})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bogus

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

Bogus represents a test server

func New

func New() *Bogus

New returns a newly intitated bogus server

func (*Bogus) AddPath

func (b *Bogus) AddPath(path string) *Path

AddPath adds a new path to the bogus server handler and returns the new path for further configuration

func (*Bogus) Close

func (b *Bogus) Close()

Close calls the close method for the underlying httptest server

func (*Bogus) HandlePaths

func (b *Bogus) HandlePaths(w http.ResponseWriter, r *http.Request)

HandlePaths implements the http handler interface and decides how to respond based on the paths configured

func (*Bogus) HitRecords

func (b *Bogus) HitRecords() []HitRecord

HitRecords returns a slice of the hit records recorded for inspection

func (*Bogus) Hits

func (b *Bogus) Hits() int

Hits returns the total number of hits seen against the bogus server

func (*Bogus) HostPort

func (b *Bogus) HostPort() (string, string)

HostPort returns the host and port number of the bogus server

func (*Bogus) SetPayload

func (b *Bogus) SetPayload(p []byte)

SetPayload is a convenience function allowing shorthand configuration of the payload for the default path

func (*Bogus) SetStatus

func (b *Bogus) SetStatus(s int)

SetStatus is a convenience function allowing shorthand configuration of the status for the default path

func (*Bogus) Start

func (b *Bogus) Start()

Start initializes the bogus server and sets it to handle the configured paths

type HitRecord

type HitRecord struct {
	Verb  string
	Path  string
	Query url.Values
	Body  []byte
}

HitRecord represents a recording of information from a single hit againstr the bogus server

type Path

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

Path represents an endpoint added to a bogus server and how it should respond

func (*Path) Hits

func (p *Path) Hits() int

Hits returns the number of hits recorded for the path

func (*Path) SetMethods

func (p *Path) SetMethods(methods ...string) *Path

SetMethods accepts a list of methods the path should respond to

func (*Path) SetPayload

func (p *Path) SetPayload(payload []byte) *Path

SetPayload sets the response payload for the path and returns the path for additional configuration

func (*Path) SetStatus

func (p *Path) SetStatus(status int) *Path

SetStatus sets the http status for the path and returns the path for additional configuration

Jump to

Keyboard shortcuts

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