testserver

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	Path     string
	Status   int
	Response []byte
	Headers  map[string]string
}

Hander defines the endpoint and response from your http test server

Example (Serve)
testServer := Handler{Response: testhelpers.LoadFile(&testing.T{}, "testdata/identity.json")}.Serve()
defer testServer.Close()

resp, err := http.Get(testServer.URL)
if err != nil {
	fmt.Println(err)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
	fmt.Println(err)
}
fmt.Println(string(b))
Output:

{
    "id": "1a",
    "type": "hero"
}

func (Handler) Request added in v0.4.0

func (h Handler) Request(r *http.Request) (*http.Response, error)

Request is an alias for RoundTrip

func (Handler) RoundTrip added in v0.4.0

func (h Handler) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip mocks a HTTP round trip for the provided response values

func (Handler) Serve

func (h Handler) Serve() *Server

Serve will create and serve this test handler from a new http test server

type Handlers added in v0.4.0

type Handlers struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewHandlers added in v0.4.0

func NewHandlers(handle ...Handler) *Handlers

NewHandlers initializes a new set of handlers

func (*Handlers) Get added in v0.4.0

func (h *Handlers) Get(r *http.Request) (Handler, error)

func (*Handlers) Request added in v0.4.0

func (h *Handlers) Request(r *http.Request) (*http.Response, error)

Request is an alias for RoundTrip

func (*Handlers) RoundTrip added in v0.4.0

func (h *Handlers) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip mocks a HTTP round trip for the provided handlers

type Server

type Server struct {
	*httptest.Server
	*Handlers
}

Server wraps a httptest server and will loop over its handlers to return a response from the test server

Example
testServer := New(Handler{Status: http.StatusCreated, Response: []byte("success")},
	Handler{Status: http.StatusRequestTimeout, Response: []byte("timeout")})
defer testServer.Close()

resp, _ := http.Get(testServer.URL)
b, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("%d: %s\n", resp.StatusCode, string(b))
resp.Body.Close()

resp, _ = http.Get(testServer.URL)
b, _ = ioutil.ReadAll(resp.Body)
fmt.Printf("%d: %s\n", resp.StatusCode, string(b))
resp.Body.Close()

resp, _ = http.Get(testServer.URL)
b, _ = ioutil.ReadAll(resp.Body)
fmt.Printf("%d: %s\n", resp.StatusCode, string(b))
resp.Body.Close()
Output:

201: success
408: timeout
201: success

func New

func New(handle ...Handler) *Server

Creates a new server that will loop over the specified response handlers when serving a response

Jump to

Keyboard shortcuts

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