Documentation ¶
Overview ¶
Package servefiles provides a way to mock a HTTP server endpoint by providing response payloads from the disk
Index ¶
- type MinimalTestingT
- type Server
- func (s *Server) Close()
- func (s *Server) LastBody(uri string) []byte
- func (s *Server) LastReqHdr(uri string) map[string][]string
- func (s *Server) RequestCount(uri string) int
- func (s *Server) RequestCounts() map[string]int
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetBaseDirs(newBaseDirs ...string)
- func (s *Server) URL() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MinimalTestingT ¶
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) LastBody ¶
LastBody returns the most recently recevied POST/PUT request body for the indicated URI
func (*Server) LastReqHdr ¶
LastReqHdr returns the headers of last request
func (*Server) RequestCount ¶
RequestCount returns the number of request processed for the indicated URI
func (*Server) RequestCounts ¶
RequestCounts returns a map of the number of requests processed for each URI
func (*Server) SetBaseDirs ¶
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.