testutil

package
v3.0.0-...-c77a060 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Name

func Name(testNum int) string

Name returns a good name for the current test.

func NewAuthServer

func NewAuthServer() *httptest.Server

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

func NilHandler(t *testing.T) http.Handler

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

func OverrideLogWriters(t *testing.T)

OverrideLogWriters overrides the bytemark-client/util/log package's writers with TestLogWriters

func WriteJSON

func WriteJSON(t *testing.T, wr io.Writer, object interface{})

WriteJSON marshals the object as JSON and writes it to the writer

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

func (*Handlers) Fill

func (h *Handlers) Fill(t *testing.T)

Fill ensures each endpoint has an http.Handler - except auth. auth is allowed to be nil.

func (Handlers) MakeServers

func (h Handlers) MakeServers(t *testing.T) (s Servers)

MakeServers creates a Servers object from this Handlers. if auth is nil, creates the default auth server with NewAuthServer.

type Mux

type Mux map[string]http.HandlerFunc

Mux is a map of URL paths to http.HandlerFuncs

func (Mux) ToHandler

func (m Mux) ToHandler() (serveMux *http.ServeMux)

ToHandler turns the Mux into an http.ServeMux

type MuxHandlers

type MuxHandlers struct {
	Auth    Mux
	Brain   Mux
	Billing Mux
	SPP     Mux
	API     Mux
}

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

type RequestTestFunc func(lib.Client)

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

func NewClientAndServers(t *testing.T, factory ServersFactory) (c lib.Client, s Servers, err error)

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) Client

func (s Servers) Client() (c lib.Client, err error)

Client makes a bytemarkClient for these Servers

func (*Servers) Close

func (s *Servers) Close()

Close ensures all the servers have been closed.

func (Servers) URLs

func (s Servers) URLs() (urls lib.EndpointURLs)

URLs creates an EndpointURLs filled with all the URLs for the Servers

type ServersFactory

type ServersFactory interface {
	MakeServers(t *testing.T) (s Servers)
}

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.

func (TestLogWriter) Write

func (tlw TestLogWriter) Write(p []byte) (n int, err error)

Write writes the bytes as a string to t.Log

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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