bogus

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 7 Imported by: 0

README

Bogus

Build Status Go Reportcard GoDoc License Release

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.

Requirements

Golang version 1.6 or higher

Installation

go get github.com/gomicro/bogus

Usage

See the examples within the docs for ways to use the library.

Versioning

The library will be versioned in accordance with Semver 2.0.0. See the releases section for the latest version. Until version 1.0.0 the libary is considered to be unstable.

It is always highly recommended to vendor the version you are using.

License

See LICENSE.md for more information.

Documentation

Overview

Package bogus provides a minimal set of helpers on top of the net/http/httptest package

Index

Examples

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

Example
// This would normally be provided by a normal testing function setup
var t *testing.T

server := bogus.New()
server.AddPath("/foo/bar").
	SetMethods("GET").
	SetPayload([]byte("some return payload")).
	SetStatus(http.StatusOK)
host, port := server.HostPort()

resp, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
if err != nil {
	t.Errorf("expected nil error, got %v", err.Error())
}
defer resp.Body.Close()

if server.Hits() != 1 {
	t.Errorf("expected server to be hit once, got %v", server.Hits())
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
	t.Errorf("expected nil error, got: %v", err.Error())
}

if string(bodyBytes) != "some return payload" {
	t.Errorf("Expected a different payload, got %v", string(bodyBytes))
}

if resp.StatusCode != http.StatusOK {
	t.Errorf("expected status 200, got %v", resp.StatusCode)
}
Output:

Example (GoblinGomega)
// This would normally be provided by a normal testing function setup
var t *testing.T

g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })

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

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

	g.It("should connect to a test server", func() {
		server.AddPath("/").
			SetMethods("GET")

		host, port := server.HostPort()

		_, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
		Expect(err).To(BeNil())
		Expect(server.Hits()).To(Equal(1))
	})
})

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

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

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

		resp, err := http.Get(fmt.Sprintf("https://%v:%v", host, port))
		Expect(err).To(BeNil())
		defer resp.Body.Close()

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

		bodyBytes, err := ioutil.ReadAll(resp.Body)
		Expect(err).To(BeNil())
		Expect(string(bodyBytes)).To(Equal("some return payload"))
		Expect(resp.StatusCode).To(Equal(http.StatusOK))
	})
})
Output:

func New

func New() *Bogus

New returns a newly intitated bogus server

func (*Bogus) AddPath

func (b *Bogus) AddPath(path string) *paths.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

type HitRecord

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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