Package Usage: go: github.com/cespare/webtest
Package webtest implements script-based testing for web servers.
The scripts, described below, are run against http.Handler implementations.
The test function TestHandler takes a *testing.T and a glob pattern,
which must match at least one file.
It creates a subtest of the top-level test for each script.
Within each per-script subtest, it creates a per-case subtest
for each case in the script, making it easy to run selected cases.
The function CheckHandler is similar but does not require a *testing.T,
making it suitable for use in other contexts.
It runs the entire script and returns a multiline error summarizing
any problems.
A script is a text file containing a sequence of cases, separated by blank lines.
Lines beginning with # characters are ignored as comments.
A case is a sequence of lines describing a request, along with checks to be
applied to the response. For example, here is a trivial script:
This script has a single case. The first line describes the request.
The second line describes a single check to be applied to the response.
In this case, the request is a GET of the URL /, and the response body
must contain the text “Go is an open source programming language”.
Each case begins with a line starting with GET, HEAD, POST, PATCH, PUT, or
DELETE. The argument (the remainder of the line) is the URL to be used in
the request. Following this line, the request can be further customized
using lines of the form
where the verb and key are single space-separated words and the text is
arbitrary text to the end of the line, or multiline text (described below).
Whether there is a key depends on the <verb>.
The possible values for <verb> are as follows.
The verb “hint” specifies text to be printed if the test case fails, as a
hint about what might be wrong.
The verb “reqheader” takes the header name as the <key> and sets a header
value for the request:
The verb “reqcookie” takes the cookie name as the <key> and sets a cookie
value for the request:
The verbs “postbody”, “postquery”, and “posttype” customize a POST, PATCH,
PUT, or DELETE request.
For example:
This describes a POST request with a posted Content-Type of “application/json”
and a body “{"go": true}”.
The “postquery” verb specifies a post body in the form of a sequence of
key-value pairs, query-encoded and concatenated automatically as a
convenience. Using “postquery” also sets the default posted Content-Type
to “application/x-www-form-urlencoded”.
For example:
This stanza sends a request with post body “x=hello+world&y=Go+%26+You”.
(The multiline syntax is described in detail below.)
By default, a stanza like the ones above checks only that the request
succeeds in returning a response with HTTP status code 200 (OK).
Additional checks are specified by more lines of the form
In the example above, <value> is “body”, there is no <key>,
<op> is “contains”, and <text> is “Go is an open source programming language”.
Whether there is a <key> depends on the <value>; “body” does not have one.
The possible values for <value> are:
If a case contains no check of “code”, then it defaults to checking that
the HTTP status code is 200, as described above, with one exception:
if the case contains a check of “redirect”, then the code is required to
be a 30x code.
The “trimbody” value is the body with all runs of spaces and tabs
reduced to single spaces, leading and trailing spaces removed on
each line, and blank lines removed.
The "rawcookie" value is the full serialized form of the cookie as seen in a
Set-Cookie header. For example, the value could be
The possible operators for <op> are:
For example:
The <text> in a request or check line can take a multiline form,
by omitting it from the original line and then specifying the text
as one or more following lines, each indented by a single tab.
The text is taken to be the sequence of indented lines, including
the final newline, but with the leading tab removed from each.
The “postquery” example above showed the multiline syntax.
Another common use is for multiline “body” checks. For example:
4 versions
Latest release: over 1 year ago
1 dependent package
View more package details: https://packages.ecosyste.ms/registries/proxy.golang.org/packages/github.com/cespare/webtest
View more repository details: http://repos.ecosyste.ms/hosts/GitHub/repositories/cespare%2Fwebtest
Dependent Repos 1
cespare/assetserver
Go file server for web assetsSize: 58.6 KB - Last synced: 19 days ago - Pushed: 7 months ago
