Documentation
¶
Index ¶
- Constants
- func JSON(v interface{}) []byte
- func MessageMimeType(key string) string
- func ResourceMIMEType(class string) string
- func RunIntegrationTests(t *testing.T, def Definition, cases TestCases, c *Client) error
- func SetMux(mux http.Handler)
- func TogglePrintAsTestsRun()
- func ToggleVerbose()
- func UsingCredentials(d *Definition)
- func UsingNoAuth(d *Definition)
- type BodyDefinition
- type Client
- type Definition
- type Integration
- type Runner
- type TestCase
- type TestCases
- type Unit
Constants ¶
const ( // POST HTTP POST Request Method POST = "POST" // GET HTTP GET Request Method GET = "GET" // HEAD HTTP HEAD Request Method HEAD = "HEAD" // PUT HTTP PUT Request Method PUT = "PUT" // PATCH HTTP PATCH Request Method PATCH = "PATCH" // DELETE HTTP DELETE Request Method DELETE = "DELETE" // CONNECT HTTP CONNECT Request Method CONNECT = "CONNECT" // OPTIONS HTTP OPTIONS Request Method OPTIONS = "OPTIONS" // TRACE HTTP TRACE Request Method TRACE = "TRACE" // AuthorizationCredentials marks a resource as requiring credentials // be supplied. AuthorizationCredentials = "credentials" // AuthenticationChecksum marks a resource as requiring a checksum // be supplied. AuthenticationChecksum = "checksum" // AuthorizationOpenID marks a resource as requiring OpenID credentials. AuthorizationOpenID = "openID" // AuthorizationNone marks a resource as not requiring credentials. AuthorizationNone = "insecure" )
const ( MIMETypeJSON = "application/json" MIMETypeXML = "application/xml" MIMETypeGOB = "application/gob" )
Variables ¶
This section is empty.
Functions ¶
func JSON ¶
func JSON(v interface{}) []byte
JSON is a simple convenience function to serialize a result into JSON which helps test authors create JSON values without checking for errors.
A common use-case is to set an 'ExpectBody' value into a test case:
testcase := truth.TestCase { Name: "Expect Error", Status: 400, Payload: RegisterPayload{ Name: "John Conner", Email: "jconner@cyberdyne-systems.com", Password: "Th3Futur3IsN0tS3T", }, ExpectBody: truth.JSON(map[string]string{ "error": "Unknown username or password", }, }
func MessageMimeType ¶
MessageMimeType returns a traditional mimetype to communicate the encoding of a message. Example:
application/json
Preference is to pass one of the known mimetype constants. Example:
json
If the provided key is unknown it will simply be returned prefixed as follows:
application/{key}
func ResourceMIMEType ¶
ResourceMIMEType builds a custom mimetype such as
application/vnd.{your-namespace}.user
using the provided class. The formula is:
application/vnd.{your-namespace}.{class}.
If an endpoint is working with messages and not domain specific resources use the `MessageMimeType` which focuses only on the encoding.
func RunIntegrationTests ¶
RunIntegrationTests runs integration or full-stack tests using the provided metadata and test cases. Provide a client to perform full-stack. If nil is provided the server's Mux will be called directly.
func TogglePrintAsTestsRun ¶
func TogglePrintAsTestsRun()
func ToggleVerbose ¶
func ToggleVerbose()
func UsingCredentials ¶
func UsingCredentials(d *Definition)
UsingCredentials specifies that the provided definition requires authentication to be accessed.
func UsingNoAuth ¶
func UsingNoAuth(d *Definition)
UsingNoAuth specifies that the provided definition does not require authentication to be accessed.
Types ¶
type BodyDefinition ¶
type BodyDefinition struct { // PackageName holds a representation of the package holding the response. // Typically if the response is not a custom type, this will be blank. PackageName string // Data holds a reference to the data structure that defines the body. Data interface{} }
type Client ¶
type Client struct {
Hostname string
}
func (Client) BuildRequest ¶
NewRequest builds an HTTP Request for tests using the provided metadata. If non-empty string is provided for the path it will be used instead of the metadata's path which is useful for route params embedded within the path such as:
/users/:name
Optionally, a payload and headers may be provided.
func (Client) MakeRequest ¶
func (c Client) MakeRequest(md Definition, tc TestCase, result interface{}) (*http.Response, []byte, error)
MakeRequest dispatches an HTTP request. If a payload is provided the response body will be read. If the returned status code is 2XX the response will be unmarshalled into the result. Provide a `nil` result to manage unmarshalling manually.
type Definition ¶
type Definition struct { // Properties that drive behavior. Method string Path string MIMETypeRequest string MIMETypeResponse string RequestHeaders map[string]string ResponseHeaders map[string]string // InputParams URL Path variables /users/{ID} InputParams interface{} // QueryParams Query string parameters QueryParams interface{} RequestBody BodyDefinition ResponseBody BodyDefinition Authenticated bool Authentication string Package string // Unique name or path to the package. Useful for generated documentation. Description string // Description of the endpoint to be used in generated documentation. Name string // Name of the endpoint to be used in generated documentation. StatsKey string // Key for instrumentation metrics. // contains filtered or unexported fields }
Definition defines an API endpoint.
func Configure ¶
func Configure(d Definition, options ...func(*Definition)) Definition
Configure returns a new Metadata struct initialized to default values unless customized by passing optional functions.
func (*Definition) Init ¶
func (def *Definition) Init() error
Init bootstraps a new Definition unless the given Definition is already marked as initialized.
type Integration ¶
type TestCase ¶
type TestCase struct { Name string Path string Headers map[string]string Payload interface{} Status int ExpectBody []byte Contains []string Result interface{} Verbose bool Integration func(Integration) Unit func(Unit) // contains filtered or unexported fields }
TestCase structures a specific test which can be applied unit, integration, full-stack, or load testing.
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
advanced
Package main is a demonstration application placed under test.
|
Package main is a demonstration application placed under test. |
basic
Package main is a basic example of how to perform integration testing in-process using the Truth package.
|
Package main is a basic example of how to perform integration testing in-process using the Truth package. |