Documentation ¶
Index ¶
- Variables
- func Name(testNum int) string
- func NewAuthServer() *httptest.Server
- func NilHandler(t *testing.T) http.Handler
- func OverrideLogWriters(t *testing.T)
- func WriteJSON(t *testing.T, wr io.Writer, object interface{})
- type Handlers
- type Mux
- type MuxHandlers
- type RequestTestFunc
- type RequestTestSpec
- type Servers
- type ServersFactory
- type TestLogWriter
Constants ¶
This section is empty.
Variables ¶
var FixtureUser = brain.User{ Username: "test-user", Email: "test@user.com", AuthorizedKeys: []brain.Key{ {Key: "ssh-rsa AAAAAAAAAAAAAAAAAIm a scary test user"}, {Key: "ssh-dsa AAAAAAAI even use DSA keys, that's how scary I am"}, }, }
FixtureUser is a simple User with a couple of keys. TODO(telyn): get rid of this in favour of making User objects in each test
Functions ¶
func NewAuthServer ¶
NewAuthServer creates a fake auth server that responds to any request with a session. just for convenience when writing tests that require auth.
func NilHandler ¶
NilHandler creates an http.Handler that fails and ends the test when called. It's the default for MuxHandlers and Handlers - so that you only need specify the endpoints you expect to talk to.
func OverrideLogWriters ¶
OverrideLogWriters overrides the bytemark-client/util/log package's writers with TestLogWriters
Types ¶
type Handlers ¶
type Handlers struct {
// contains filtered or unexported fields
}
Handlers is a struct which holds a http.Handler for each endpoint the client could possibly talk to. It gets converted to a Servers by MakeServers, which is the structure that's used in tests. Due to the existence of RequestTestSpec, most if not all the tests in lib and lib/requests should use RequestTestSpec instead of directly getting a Servers
type MuxHandlers ¶
MuxHandlers is the equivalent of Handlers, but for Mux objects instead of http.Handler.
func NewMuxHandlers ¶
func NewMuxHandlers(endpoint lib.Endpoint, url string, h http.HandlerFunc) (mh MuxHandlers, err error)
NewMuxHandlers creates a MuxHandler which will respond on the given endpoint URL with the handler provided, after which the request body will be automatically closed.
func (*MuxHandlers) AddMux ¶
func (mh *MuxHandlers) AddMux(ep lib.Endpoint, m Mux) (err error)
AddMux adds a Mux for the endpoint passed. Returns an error if it didn't recognise that endpoint
func (MuxHandlers) MakeServers ¶
func (mh MuxHandlers) MakeServers(t *testing.T) (s Servers)
MakeServers creates a Servers whose httptest.Server elements are handled by these Muxes
type RequestTestFunc ¶
RequestTestFunc is a function which will be run by RequestTestSpec.Run. This function should generally call some request method (such as those built-in to the lib.Client interface or external ones, usually in the lib/requests/* packages.
Any validation of the results of that request method should be done as part of the RequestTestFunc
type RequestTestSpec ¶
type RequestTestSpec struct { // MuxHandlers will be used if defined - this allows for the test to support // multiple endpoints, URLs, methods, etc. while still keeping as DRY as // possible. Otherwise, set the Method, Endpoint, URL, AssertRequest, // Response and StatusCode. MuxHandlers *MuxHandlers // Method is used to assert that the request was given the correct type // it is only used if MuxHandlers is nil Method string // Endpoint is used to build a MuxHandlers from when MuxHandlers is nil Endpoint lib.Endpoint // URL is used to build a MuxHandlers when MuxHandlers is nil URL string // Response will be JSON marshalled // to use a raw string (i.e. if you don't want to use JSON) cast it to // encoding/json.RawMessage - this will be reproduced verbatim Response interface{} // StatusCode is the status code that will be returned. If unset, will // default to whatever http.ResponseWriter.Write defaults to. // Only used if MuxHandlers is nil StatusCode int // AssertRequest is an optional func which will get called to check the // request object further - for example to check the URL has particular // query string keys AssertRequest func(t *testing.T, testName string, r *http.Request) // contains filtered or unexported fields }
RequestTestSpec is used to build up httptest servers for a test, then run a function and check it makes the correct request.
func (*RequestTestSpec) Run ¶
func (rts *RequestTestSpec) Run(t *testing.T, testName string, auth bool, fn RequestTestFunc)
Run sets up fake servers, creates a client that talks to them, then passes the client to fn. fn should run some request method using the client & test the results of that function.
type Servers ¶
type Servers struct {
// contains filtered or unexported fields
}
Servers holds an httptest.Server for each endpoint that the client could talk to
func NewClientAndServers ¶
NewClientAndServers constructs httptest Servers for a pretend auth and API endpoint, then constructs a Client that uses those servers. Used to test that the right URLs are being requested and such. because this is used in nearly all of the tests in lib, this also does some weird magic to set up a writer for log such that all the test output comes out attached to the test it's from
func (Servers) URLs ¶
func (s Servers) URLs() (urls lib.EndpointURLs)
URLs creates an EndpointURLs filled with all the URLs for the Servers
type ServersFactory ¶
ServersFactory is an interface used for convenience - so that Handlers or MuxHandlers can be passed to NewClientAndServers
type TestLogWriter ¶
type TestLogWriter struct {
// contains filtered or unexported fields
}
TestLogWriter is a writer that writes to the test log using testing.T.Log it's a bit naff since things like fmt.Printf and text/template.Execute call Write multiple times and each Write adds a new line to the log.