Movatterモバイル変換


[0]ホーム

URL:


httpcheck

packagemodule
v1.12.5Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License:MITImports:17Imported by:8

Details

Repository

github.com/ikawaha/httpcheck

Links

README

GoGo Reference

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/httpcheck

API Documentation

Go Reference

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

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

funcNew

func New(hhttp.Handler, options ...Option) *Checker

New creates an HTTP Checker for testing with the given handler.

funcNewExternaladded inv1.10.2

func NewExternal(urlstring, options ...Option) *Checker

NewExternal creates an HTTP Checker for testing an external server specified by url.

func (*Checker)GetURL

func (c *Checker) GetURL()string

GetURL returns the server URL.

func (*Checker)PersistCookie

func (c *Checker) PersistCookie(cookiestring)

PersistCookie - enables a cookie to be preserved between requests

func (*Checker)Test

func (c *Checker) Test(tTestingT, method, pathstring) *Tester

Test - Prepare for testing some part of code which lives on provided path and method.

func (*Checker)TestRequest

func (c *Checker) TestRequest(tTestingT, request *http.Request) *Tester

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

func (c *Checker) UnpersistCookie(cookiestring)

UnpersistCookie - the specified cookie will not be preserved during requests anymore

typeFieldPartadded inv1.9.0

type FieldPart struct {FieldNamestringValuestring}

FieldPart represents a multipart part of the field type part.

func (FieldPart)Partadded inv1.9.0

func (pFieldPart) Part(mw *multipart.Writer)error

Part returns a multipart part.

typeFilePartadded inv1.9.0

type FilePart struct {FieldNamestringFileNamestring}

FilePart represents a multipart part of the file type part.

func (FilePart)Partadded inv1.9.0

func (pFilePart) Part(mw *multipart.Writer)error

Part returns a multipart part.

typeOption

type Option func(*Checker)

Option represents the option for the checker.

funcCheckRedirectadded inv1.4.0

func CheckRedirect(policy func(req *http.Request, via []*http.Request)error)Option

CheckRedirect sets the policy of redirection to the HTTP client.

funcClientTimeout

func ClientTimeout(dtime.Duration)Option

ClientTimeout sets the client timeout.

funcDebugadded inv1.12.1

func Debug()Option

funcNoRedirectadded 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.

typeParteradded inv1.9.0

type Parter interface {Part(mw *multipart.Writer)error}

Parter is the interface that create a multipart part.

typeTester

type Tester struct {*Checker// contains filtered or unexported fields}

Tester represents the HTTP tester having TestingT.

func (*Tester)Cb

func (tt *Tester) Cb(callback func(*http.Response))

Cb - Will call provided callback function with current response

func (*Tester)Check

func (tt *Tester) Check() *Tester

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

func (tt *Tester) ContainsBody(segment []byte) *Tester

ContainsBody checks if the body contains provided [] byte data.

func (*Tester)ContainsString

func (tt *Tester) ContainsString(substrstring) *Tester

ContainsString converts the response to a string type and then checks it containing the given string.

func (*Tester)HasBody

func (tt *Tester) HasBody(expected []byte) *Tester

HasBody checks if the body is equal to provided []byte data.

func (*Tester)HasCookie

func (tt *Tester) HasCookie(key, expectedValuestring) *Tester

HasCookie checks if the response contains cookie with provided key and value.

func (*Tester)HasHeader

func (tt *Tester) HasHeader(key, expectedValuestring) *Tester

HasHeader checks if the response contains header on provided key with provided value.

func (*Tester)HasHeaders

func (tt *Tester) HasHeaders(headers map[string]string) *Tester

HasHeaders checks if the response contains a provided headers map.

func (*Tester)HasJSON

func (tt *Tester) HasJSON(expectedany) *Tester

HasJSON checks if the response body contains json with provided value.

func (*Tester)HasJsondeprecated

func (tt *Tester) HasJson(expectedany) *Tester

Deprecated: HasJson checks if the response body contains json with provided value.

func (*Tester)HasStatus

func (tt *Tester) HasStatus(expectedint) *Tester

HasStatus checks if the response status is equal to that provided.

func (*Tester)HasString

func (tt *Tester) HasString(expectedstring) *Tester

HasString converts the response to a string type and then compares it with the given string.

func (*Tester)HasXML

func (tt *Tester) HasXML(expectedany) *Tester

HasXML checks if body contains xml with provided value.

func (*Tester)HasXmldeprecated

func (tt *Tester) HasXml(expectedany) *Tester

Deprecated: HasXml checks if body contains xml with provided value.

func (*Tester)MatchesJSONQueryadded inv1.12.0

func (tt *Tester) MatchesJSONQuery(qstring) *Tester

func (*Tester)MustContainsBodyadded inv1.7.0

func (tt *Tester) MustContainsBody(segment []byte) *Tester

MustContainsBody checks if the body contains provided [] byte data.

func (*Tester)MustContainsStringadded inv1.7.0

func (tt *Tester) MustContainsString(substrstring) *Tester

MustContainsString converts the response to a string type and then checks it containing the given string.

func (*Tester)MustHasBodyadded inv1.7.0

func (tt *Tester) MustHasBody(expected []byte) *Tester

MustHasBody checks if the body is equal to provided []byte data.

func (*Tester)MustHasCookieadded inv1.7.0

func (tt *Tester) MustHasCookie(key, expectedValuestring) *Tester

MustHasCookie checks if the response contains cookie with provided key and value.

func (*Tester)MustHasHeaderadded inv1.7.0

func (tt *Tester) MustHasHeader(key, expectedValuestring) *Tester

MustHasHeader checks if the response contains header on provided key with provided value.

func (*Tester)MustHasHeadersadded inv1.7.0

func (tt *Tester) MustHasHeaders(headers map[string]string) *Tester

MustHasHeaders checks if the response contains a provided headers map.

func (*Tester)MustHasJSONadded inv1.7.0

func (tt *Tester) MustHasJSON(expectedany) *Tester

MustHasJSON checks if the response body contains json with provided value.

func (*Tester)MustHasStatusadded inv1.7.0

func (tt *Tester) MustHasStatus(expectedint) *Tester

MustHasStatus checks if the response status is equal to that provided.

func (*Tester)MustHasStringadded inv1.7.0

func (tt *Tester) MustHasString(expectedstring) *Tester

MustHasString converts the response to a string type and then compares it with the given string.

func (*Tester)MustHasXMLadded inv1.7.0

func (tt *Tester) MustHasXML(expectedany) *Tester

MustHasXML checks if body contains xml with provided value.

func (*Tester)MustNotContainsBodyadded inv1.7.0

func (tt *Tester) MustNotContainsBody(segment []byte) *Tester

MustNotContainsBody checks if the body does not contain provided [] byte data.

func (*Tester)MustNotContainsStringadded inv1.7.0

func (tt *Tester) MustNotContainsString(substrstring) *Tester

MustNotContainsString converts the response to a string type and then checks if it does notcontain the given string.

func (*Tester)NotContainsBody

func (tt *Tester) NotContainsBody(segment []byte) *Tester

NotContainsBody checks if the body does not contain provided [] byte data.

func (*Tester)NotContainsString

func (tt *Tester) NotContainsString(substrstring) *Tester

NotContainsString converts the response to a string type and then checks if it does notcontain the given string.

func (*Tester)NotMatchesJSONQueryadded inv1.12.0

func (tt *Tester) NotMatchesJSONQuery(qstring) *Tester

func (*Tester)Tadded inv1.11.0

func (tt *Tester) T()TestingT

T returns the TestingT.

func (*Tester)WithBasicAuth

func (tt *Tester) WithBasicAuth(user, passstring) *Tester

WithBasicAuth is an alias to set basic auth in the request header.

func (*Tester)WithBearerAuthadded inv1.5.0

func (tt *Tester) WithBearerAuth(tokenstring) *Tester

WithBearerAuth is an alias to set bearer auth in the request header.

func (*Tester)WithBody

func (tt *Tester) WithBody(body []byte) *Tester

WithBody adds the []byte data to the body.

func (*Tester)WithCookie

func (tt *Tester) WithCookie(key, valuestring) *Tester

WithCookie puts cookie on the request.

func (*Tester)WithHeader

func (tt *Tester) WithHeader(key, valuestring) *Tester

WithHeader puts header on the request.

func (*Tester)WithHeaders

func (tt *Tester) WithHeaders(headers map[string]string) *Tester

WithHeaders puts a map of headers on the request.

func (*Tester)WithHostHeaderadded inv1.6.0

func (tt *Tester) WithHostHeader(valuestring) *Tester

WithHostHeader puts "Host" header on the request.

func (*Tester)WithJSON

func (tt *Tester) WithJSON(expectedany) *Tester

WithJSON adds a json encoded struct to the body.

func (*Tester)WithJsondeprecated

func (tt *Tester) WithJson(expectedany) *Tester

Deprecated: WithJson adds a json encoded struct to the body.

func (*Tester)WithMultipartadded inv1.9.0

func (tt *Tester) WithMultipart(partParter, parts ...Parter) *Tester

WithMultipart add a multipart data to the body.

func (*Tester)WithString

func (tt *Tester) WithString(bodystring) *Tester

WithString adds the string to the body.

func (*Tester)WithXML

func (tt *Tester) WithXML(bodyany) *Tester

WithXML adds a xml encoded body to the request.

func (*Tester)WithXmldeprecated

func (tt *Tester) WithXml(bodyany) *Tester

Deprecated: WithXml adds a xml encoded body to the request.

typeTestingTadded inv1.8.0

type TestingT interface {Errorf(formatstring, args ...any)FailNow()}

TestingT is an interface wrapper around *testing.T.

Source Files

View all Source files

Directories

PathSynopsis
plugin
goamodule

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp