httpcheck
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
httpcheck
supertest inspired library for testing HTTP servers.
A Fork fromivpusic/httpcheck with following changes:
- Change to set testing.T when generating the request instead of the constructor,
- Fix to prevent incorrect method chain,
- Add to the timeout option of the client to the checker.
How to install?
go get github.com/ikawaha/httpcheckAPI Documentation
How to use?
Basic example
package mainimport ("github.com/ikawaha/httpcheck")func TestExample(t *testing.T) {// testHandler should be instance of http.Handlerchecker := httpcheck.New(&testHandler{})checker.Test(t, "GET", "/some/url").WithHeader("key", "value").WithCookie("key", "value").Check().HasStatus(200).HasCookie("key", "expectedValue").HasHeader("key", "expectedValue").HasJSON(&someType{})}Include body
String
package mainimport ("github.com/ivpusic/httpcheck")func TestExample(t *testing.T) {checker := httpcheck.New(&testHandler{})checker.Test(t, "GET", "/some/url").WithString("Hello!")Check().HasStatus(200)}JSON
package mainimport ("github.com/ivpusic/httpcheck")func TestExample(t *testing.T) {checker := httpcheck.New(&testHandler{})data := &someStruct{field1: "hi",}checker.Test(t, "GET", "/some/url").WithJSON(data)Check().HasStatus(200)}XML
package mainimport ("github.com/ivpusic/httpcheck")func TestExample(t *testing.T) {checker := httpcheck.New(&testHandler{})data := &someStruct{field1: "hi",}checker.Test(t, "GET", "/some/url").WithXML(data)Check().HasStatus(200)}Provide*http.Request instance
package mainimport ("net/http""github.com/ivpusic/httpcheck")func TestExample(t *testing.T) {checker := httpcheck.New(&testHandler{})checker.TestRequest(t, &http.Request{ /* fields */ }).Check().HasStatus(200)}Define callback
package mainimport ("net/http""github.com/ikawaha/httpcheck")func TestExample(t *testing.T) {checker := httpcheck.New(&testHandler{})checker.Test(t, "GET", "/some/url").Check().HasStatus(200).HasBody([]byte("some body")).Cb(func(response *http.Response) { /* do something */ })}License MIT
Documentation¶
Index¶
- Constants
- type Checker
- type FieldPart
- type FilePart
- type Option
- type Parter
- type Tester
- func (tt *Tester) Cb(callback func(*http.Response))
- func (tt *Tester) Check() *Tester
- func (tt *Tester) ContainsBody(segment []byte) *Tester
- func (tt *Tester) ContainsString(substr string) *Tester
- func (tt *Tester) HasBody(expected []byte) *Tester
- func (tt *Tester) HasCookie(key, expectedValue string) *Tester
- func (tt *Tester) HasHeader(key, expectedValue string) *Tester
- func (tt *Tester) HasHeaders(headers map[string]string) *Tester
- func (tt *Tester) HasJSON(expected any) *Tester
- func (tt *Tester) HasJson(expected any) *Testerdeprecated
- func (tt *Tester) HasStatus(expected int) *Tester
- func (tt *Tester) HasString(expected string) *Tester
- func (tt *Tester) HasXML(expected any) *Tester
- func (tt *Tester) HasXml(expected any) *Testerdeprecated
- func (tt *Tester) MatchesJSONQuery(q string) *Tester
- func (tt *Tester) MustContainsBody(segment []byte) *Tester
- func (tt *Tester) MustContainsString(substr string) *Tester
- func (tt *Tester) MustHasBody(expected []byte) *Tester
- func (tt *Tester) MustHasCookie(key, expectedValue string) *Tester
- func (tt *Tester) MustHasHeader(key, expectedValue string) *Tester
- func (tt *Tester) MustHasHeaders(headers map[string]string) *Tester
- func (tt *Tester) MustHasJSON(expected any) *Tester
- func (tt *Tester) MustHasStatus(expected int) *Tester
- func (tt *Tester) MustHasString(expected string) *Tester
- func (tt *Tester) MustHasXML(expected any) *Tester
- func (tt *Tester) MustNotContainsBody(segment []byte) *Tester
- func (tt *Tester) MustNotContainsString(substr string) *Tester
- func (tt *Tester) NotContainsBody(segment []byte) *Tester
- func (tt *Tester) NotContainsString(substr string) *Tester
- func (tt *Tester) NotMatchesJSONQuery(q string) *Tester
- func (tt *Tester) T() TestingT
- func (tt *Tester) WithBasicAuth(user, pass string) *Tester
- func (tt *Tester) WithBearerAuth(token string) *Tester
- func (tt *Tester) WithBody(body []byte) *Tester
- func (tt *Tester) WithCookie(key, value string) *Tester
- func (tt *Tester) WithHeader(key, value string) *Tester
- func (tt *Tester) WithHeaders(headers map[string]string) *Tester
- func (tt *Tester) WithHostHeader(value string) *Tester
- func (tt *Tester) WithJSON(expected any) *Tester
- func (tt *Tester) WithJson(expected any) *Testerdeprecated
- func (tt *Tester) WithMultipart(part Parter, parts ...Parter) *Tester
- func (tt *Tester) WithString(body string) *Tester
- func (tt *Tester) WithXML(body any) *Tester
- func (tt *Tester) WithXml(body any) *Testerdeprecated
- type TestingT
Constants¶
const (// DefaultClientTimeout is the default timeout for requests made by checker.DefaultClientTimeout = 5 *time.Second)
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeChecker¶
type Checker struct {// contains filtered or unexported fields}Checker represents the HTTP checker without TestingT.
funcNewExternal¶added inv1.10.2
NewExternal creates an HTTP Checker for testing an external server specified by url.
func (*Checker)PersistCookie¶
PersistCookie - enables a cookie to be preserved between requests
func (*Checker)Test¶
Test - Prepare for testing some part of code which lives on provided path and method.
func (*Checker)TestRequest¶
TestRequest - If you want to provide you custom http.Request instance, you can do it using this methodIn this case internal http.Request instance won't be created, and passed instance will be usedfor making request
func (*Checker)UnpersistCookie¶
UnpersistCookie - the specified cookie will not be preserved during requests anymore
typeOption¶
type Option func(*Checker)
Option represents the option for the checker.
funcCheckRedirect¶added inv1.4.0
CheckRedirect sets the policy of redirection to the HTTP client.
funcNoRedirect¶added inv1.4.0
func NoRedirect()Option
NoRedirect is the alias of the following:
CheckRedirect(func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse})Client returns ErrUseLastResponse, the next request is not sent and the most recentresponse is returned with its body unclosed.
typeTester¶
type Tester struct {*Checker// contains filtered or unexported fields}Tester represents the HTTP tester having TestingT.
func (*Tester)Check¶
Check - Will make request to built request object.After request is made, it will save response object for future assertionsResponsibility of this method is also to start and stop HTTP server
func (*Tester)ContainsBody¶
ContainsBody checks if the body contains provided [] byte data.
func (*Tester)ContainsString¶
ContainsString converts the response to a string type and then checks it containing the given string.
func (*Tester)HasCookie¶
HasCookie checks if the response contains cookie with provided key and value.
func (*Tester)HasHeader¶
HasHeader checks if the response contains header on provided key with provided value.
func (*Tester)HasHeaders¶
HasHeaders checks if the response contains a provided headers map.
func (*Tester)HasString¶
HasString converts the response to a string type and then compares it with the given string.
func (*Tester)MatchesJSONQuery¶added inv1.12.0
func (*Tester)MustContainsBody¶added inv1.7.0
MustContainsBody checks if the body contains provided [] byte data.
func (*Tester)MustContainsString¶added inv1.7.0
MustContainsString converts the response to a string type and then checks it containing the given string.
func (*Tester)MustHasBody¶added inv1.7.0
MustHasBody checks if the body is equal to provided []byte data.
func (*Tester)MustHasCookie¶added inv1.7.0
MustHasCookie checks if the response contains cookie with provided key and value.
func (*Tester)MustHasHeader¶added inv1.7.0
MustHasHeader checks if the response contains header on provided key with provided value.
func (*Tester)MustHasHeaders¶added inv1.7.0
MustHasHeaders checks if the response contains a provided headers map.
func (*Tester)MustHasJSON¶added inv1.7.0
MustHasJSON checks if the response body contains json with provided value.
func (*Tester)MustHasStatus¶added inv1.7.0
MustHasStatus checks if the response status is equal to that provided.
func (*Tester)MustHasString¶added inv1.7.0
MustHasString converts the response to a string type and then compares it with the given string.
func (*Tester)MustHasXML¶added inv1.7.0
MustHasXML checks if body contains xml with provided value.
func (*Tester)MustNotContainsBody¶added inv1.7.0
MustNotContainsBody checks if the body does not contain provided [] byte data.
func (*Tester)MustNotContainsString¶added inv1.7.0
MustNotContainsString converts the response to a string type and then checks if it does notcontain the given string.
func (*Tester)NotContainsBody¶
NotContainsBody checks if the body does not contain provided [] byte data.
func (*Tester)NotContainsString¶
NotContainsString converts the response to a string type and then checks if it does notcontain the given string.
func (*Tester)NotMatchesJSONQuery¶added inv1.12.0
func (*Tester)WithBasicAuth¶
WithBasicAuth is an alias to set basic auth in the request header.
func (*Tester)WithBearerAuth¶added inv1.5.0
WithBearerAuth is an alias to set bearer auth in the request header.
func (*Tester)WithCookie¶
WithCookie puts cookie on the request.
func (*Tester)WithHeader¶
WithHeader puts header on the request.
func (*Tester)WithHeaders¶
WithHeaders puts a map of headers on the request.
func (*Tester)WithHostHeader¶added inv1.6.0
WithHostHeader puts "Host" header on the request.
func (*Tester)WithMultipart¶added inv1.9.0
WithMultipart add a multipart data to the body.
func (*Tester)WithString¶
WithString adds the string to the body.