servefiles

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package servefiles provides a way to mock a HTTP server endpoint by providing response payloads from the disk

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MinimalTestingT

type MinimalTestingT interface {
	require.TestingT
	Logf(format string, args ...interface{})
}

MinimalTestingT defines the small subset of testing.T that we use having this be an interface makes plugging in other test runs and writing our own tests easier

type Server

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

Server implements a test server that response based on sets of pre-canned response contained in a directory supplied by calling SetBaseDir

func New

func New(t MinimalTestingT, baseDirs ...string) *Server

New creates a new instance of the test HTTP server. Once created you should call SetBaseDir to indicate where the fake responses are stored. You should call Close on the returned object when you're finished using it to shutdown the server if the supplied baseDirs is not empty, then SetBaseDirs will be called for you to configure the response mappings

func (*Server) Close

func (s *Server) Close()

Close shuts down the test server

func (*Server) LastBody

func (s *Server) LastBody(uri string) []byte

LastBody returns the most recently recevied POST/PUT request body for the indicated URI

func (*Server) LastReqHdr

func (s *Server) LastReqHdr(uri string) map[string][]string

LastReqHdr returns the headers of last request

func (*Server) RequestCount

func (s *Server) RequestCount(uri string) int

RequestCount returns the number of request processed for the indicated URI

func (*Server) RequestCounts

func (s *Server) RequestCounts() map[string]int

RequestCounts returns a map of the number of requests processed for each URI

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) SetBaseDirs

func (s *Server) SetBaseDirs(newBaseDirs ...string)

SetBaseDirs updates the baseDir to a new directory, if the directory is not valid, it'll fail the test. The directory is expected to contain a requests.json file that provides the mapping from request URI to file with the response data in it. e.g.

{
   "/v1/status" : "status",
   "get:/v1/status" : "getstatus"
}

the actual file used will be status.json, if this doesn't exist then it'll look for a file based on the number of requests to this URI e.g. status.1.json, status.2.json, etc. if there's no matching entry, or the specified file doesn't exist a 404 response is returned. the auth endpoint at /services/oauth2/token has special handling to update instance_url to the url of this test server [you still need to provide the base response file] requests.json must be in the first directory provided, named reponse payloaded will be used in the order of the supplied directoies, e.g. if requests.json says a response is describe.json, then it'll look in baseDirs[0] and if it can't find it there, look in baseDirs[1] and so on through the supplied list of baseDirs. This makes its easier for your tests to not have duplicated sets of response files by having fallback directories with common responses in. newBaseDirs must incldue at least one directory.

func (*Server) URL

func (s *Server) URL() string

URL returns the base URL to this server

Jump to

Keyboard shortcuts

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