htest

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2018 License: MIT Imports: 8 Imported by: 0

README

htest is a http-test package

Coverage Status Go Report Card Build Status Documentation

Usage

Test HandlerFunc
// handler.go
package myapp

import (
	"io"
	"net/http"
)

func NameHandler(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, `{"name": "hexi"}`)
}
// handler_test.go
package myapp

import (
	"testing"
	"github.com/Hexilee/htest"
)

func TestNameHandlerFunc(t *testing.T) {
	client := htest.NewClient().ToFunc(NameHandler)
	body := client.Get("").Send().OK().JSON()
	body.String("name", "hexi")
}

You can also test handler (*http.ServeMux, *echo.Echo .etc.)

To ServeMux
// handler.go
package myapp

import (
	"io"
	"net/http"
)

var (
	Mux *http.ServeMux
)

func init() {
	Mux = http.NewServeMux()
	Mux.HandleFunc("/name", NameHandler)
}

func NameHandler(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, `{"name": "hexi"}`)
}
// handler_test.go
package myapp

import (
	"testing"
	"github.com/Hexilee/htest"
)

func TestNameHandler(t *testing.T) {
	client := htest.NewClient().To(Mux)
	body := client.Get("/name").Send().OK().JSON()
	body.String("name", "hexi")
}
To Echo
// handler.go
package myapp

import (
	"io"
	"github.com/labstack/echo"
)

var (
	server *echo.Echo
)

func init() {
	server = echo.New()
	server.GET("/name", NameHandlerEcho)
}

func NameHandlerEcho(c echo.Context) error {
	return c.String(http.StatusOK, `{"name": "hexi"}`)
}
// handler_test.go
package myapp

import (
	"testing"
	"github.com/Hexilee/htest"
)

func TestNameHandlerEcho(t *testing.T) {
	client := htest.NewClient().To(server)
	body := client.Get("/name").Send().OK().JSON()
	body.String("name", "hexi")
}

Documentation

Overview

htest is a http test package

import "github.com/Hexilee/htest"

Index

Constants

View Source
const (
	CONNECT = "CONNECT"
	DELETE  = "DELETE"
	GET     = "GET"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	PATCH   = "PATCH"
	POST    = "POST"
	PUT     = "PUT"
	TRACE   = "TRACE"
)

HTTP methods

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + charsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMETextXML                          = "text/xml"
	MIMETextXMLCharsetUTF8               = MIMETextXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + charsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + charsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEOctetStream                      = "application/octet-stream"
)

MIME types

View Source
const (
	HeaderAccept              = "Accept"
	HeaderAcceptEncoding      = "Accept-Encoding"
	HeaderAllow               = "Allow"
	HeaderAuthorization       = "Authorization"
	HeaderContentDisposition  = "Content-Disposition"
	HeaderContentEncoding     = "Content-Encoding"
	HeaderContentLength       = "Content-Length"
	HeaderContentType         = "Content-Type"
	HeaderCookie              = "Cookie"
	HeaderSetCookie           = "Set-Cookie"
	HeaderIfModifiedSince     = "If-Modified-Since"
	HeaderLastModified        = "Last-Modified"
	HeaderLocation            = "Location"
	HeaderUpgrade             = "Upgrade"
	HeaderVary                = "Vary"
	HeaderWWWAuthenticate     = "WWW-Authenticate"
	HeaderXForwardedFor       = "X-Forwarded-For"
	HeaderXForwardedProto     = "X-Forwarded-Proto"
	HeaderXForwardedProtocol  = "X-Forwarded-Protocol"
	HeaderXForwardedSsl       = "X-Forwarded-Ssl"
	HeaderXUrlScheme          = "X-Url-Scheme"
	HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
	HeaderXRealIP             = "X-Real-IP"
	HeaderXRequestID          = "X-Request-ID"
	HeaderServer              = "Server"
	HeaderOrigin              = "Origin"

	// Access control
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// Security
	HeaderStrictTransportSecurity = "Strict-Transport-Security"
	HeaderXContentTypeOptions     = "X-Content-Type-Options"
	HeaderXXSSProtection          = "X-XSS-Protection"
	HeaderXFrameOptions           = "X-Frame-Options"
	HeaderContentSecurityPolicy   = "Content-Security-Policy"
	HeaderXCSRFToken              = "X-CSRF-Token"
)

Headers

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*testing.T
	// contains filtered or unexported fields
}

func NewClient

func NewClient(t *testing.T) *Client

func (Client) Connect added in v0.2.0

func (c Client) Connect(path string) *Request

func (Client) Delete added in v0.2.0

func (c Client) Delete(path string) *Request

func (Client) Get

func (c Client) Get(path string) *Request

func (Client) Head added in v0.2.0

func (c Client) Head(path string) *Request

func (Client) NewRequest

func (c Client) NewRequest(req *http.Request) *Request

func (Client) Options added in v0.2.0

func (c Client) Options(path string) *Request

func (Client) Patch added in v0.2.0

func (c Client) Patch(path string, body io.Reader) *Request

func (Client) Post added in v0.2.0

func (c Client) Post(path string, body io.Reader) *Request

func (Client) Put added in v0.2.0

func (c Client) Put(path string, body io.Reader) *Request

func (Client) To

func (c Client) To(handler http.Handler) *Client

func (Client) ToFunc

func (c Client) ToFunc(handlerFunc http.HandlerFunc) *Client

func (Client) Trace added in v0.2.0

func (c Client) Trace(path string) *Request

type JSON

type JSON struct {
	*testing.T
	// contains filtered or unexported fields
}

func NewJSON

func NewJSON(body []byte, t *testing.T) *JSON

func (*JSON) Bind

func (j *JSON) Bind(obj interface{}) error

func (*JSON) Exist

func (j *JSON) Exist(key string) *JSON

func (*JSON) GetJSON

func (j *JSON) GetJSON(key string) (result gjson.Result, exist bool)

func (*JSON) NotExist

func (j *JSON) NotExist(key string) *JSON

func (*JSON) String

func (j *JSON) String(key, expect string) *JSON

type Request

type Request struct {
	*http.Request
	Handler http.Handler
	*testing.T
}

func (*Request) Send

func (r *Request) Send() *Response

func (*Request) SetHeader

func (r *Request) SetHeader(key, value string) *Request

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

type Response

type Response struct {
	*http.Response
	*testing.T
}

func NewResponse

func NewResponse(response *http.Response, t *testing.T) *Response

func (*Response) Bind

func (r *Response) Bind(obj interface{}) error

func (*Response) Bytes

func (r *Response) Bytes() []byte

func (*Response) Code added in v0.4.0

func (r *Response) Code(statusCode int) *Response

func (*Response) JSON

func (r *Response) JSON() *JSON

func (*Response) StatusAccepted added in v0.4.0

func (r *Response) StatusAccepted() *Response

func (*Response) StatusAlreadyReported added in v0.4.0

func (r *Response) StatusAlreadyReported() *Response

func (*Response) StatusBadGateway added in v0.4.0

func (r *Response) StatusBadGateway() *Response

func (*Response) StatusBadRequest added in v0.4.0

func (r *Response) StatusBadRequest() *Response

func (*Response) StatusConflict added in v0.4.0

func (r *Response) StatusConflict() *Response

func (*Response) StatusContinue added in v0.4.0

func (r *Response) StatusContinue() *Response

func (*Response) StatusCreated added in v0.4.0

func (r *Response) StatusCreated() *Response

func (*Response) StatusExpectationFailed added in v0.4.0

func (r *Response) StatusExpectationFailed() *Response

func (*Response) StatusFailedDependency added in v0.4.0

func (r *Response) StatusFailedDependency() *Response

func (*Response) StatusForbidden added in v0.4.0

func (r *Response) StatusForbidden() *Response

func (*Response) StatusFound added in v0.4.0

func (r *Response) StatusFound() *Response

func (*Response) StatusGatewayTimeout added in v0.4.0

func (r *Response) StatusGatewayTimeout() *Response

func (*Response) StatusGone added in v0.4.0

func (r *Response) StatusGone() *Response

func (*Response) StatusHTTPVersionNotSupported added in v0.4.0

func (r *Response) StatusHTTPVersionNotSupported() *Response

func (*Response) StatusIMUsed added in v0.4.0

func (r *Response) StatusIMUsed() *Response

func (*Response) StatusInsufficientStorage added in v0.4.0

func (r *Response) StatusInsufficientStorage() *Response

func (*Response) StatusInternalServerError added in v0.4.0

func (r *Response) StatusInternalServerError() *Response

func (*Response) StatusLengthRequired added in v0.4.0

func (r *Response) StatusLengthRequired() *Response

func (*Response) StatusLocked added in v0.4.0

func (r *Response) StatusLocked() *Response

func (*Response) StatusLoopDetected added in v0.4.0

func (r *Response) StatusLoopDetected() *Response

func (*Response) StatusMethodNotAllowed added in v0.4.0

func (r *Response) StatusMethodNotAllowed() *Response

func (*Response) StatusMovedPermanently added in v0.4.0

func (r *Response) StatusMovedPermanently() *Response

func (*Response) StatusMultiStatus added in v0.4.0

func (r *Response) StatusMultiStatus() *Response

func (*Response) StatusMultipleChoices added in v0.4.0

func (r *Response) StatusMultipleChoices() *Response

func (*Response) StatusNetworkAuthenticationRequired added in v0.4.0

func (r *Response) StatusNetworkAuthenticationRequired() *Response

func (*Response) StatusNoContent added in v0.4.0

func (r *Response) StatusNoContent() *Response

func (*Response) StatusNonAuthoritativeInfo added in v0.4.0

func (r *Response) StatusNonAuthoritativeInfo() *Response

func (*Response) StatusNotAcceptable added in v0.4.0

func (r *Response) StatusNotAcceptable() *Response

func (*Response) StatusNotExtended added in v0.4.0

func (r *Response) StatusNotExtended() *Response

func (*Response) StatusNotFound added in v0.4.0

func (r *Response) StatusNotFound() *Response

func (*Response) StatusNotImplemented added in v0.4.0

func (r *Response) StatusNotImplemented() *Response

func (*Response) StatusNotModified added in v0.4.0

func (r *Response) StatusNotModified() *Response

func (*Response) StatusOK added in v0.4.0

func (r *Response) StatusOK() *Response

func (*Response) StatusPartialContent added in v0.4.0

func (r *Response) StatusPartialContent() *Response

func (*Response) StatusPaymentRequired added in v0.4.0

func (r *Response) StatusPaymentRequired() *Response

func (*Response) StatusPermanentRedirect added in v0.4.0

func (r *Response) StatusPermanentRedirect() *Response

func (*Response) StatusPreconditionFailed added in v0.4.0

func (r *Response) StatusPreconditionFailed() *Response

func (*Response) StatusPreconditionRequired added in v0.4.0

func (r *Response) StatusPreconditionRequired() *Response

func (*Response) StatusProcessing added in v0.4.0

func (r *Response) StatusProcessing() *Response

func (*Response) StatusProxyAuthRequired added in v0.4.0

func (r *Response) StatusProxyAuthRequired() *Response

func (*Response) StatusRequestEntityTooLarge added in v0.4.0

func (r *Response) StatusRequestEntityTooLarge() *Response

func (*Response) StatusRequestHeaderFieldsTooLarge added in v0.4.0

func (r *Response) StatusRequestHeaderFieldsTooLarge() *Response

func (*Response) StatusRequestTimeout added in v0.4.0

func (r *Response) StatusRequestTimeout() *Response

func (*Response) StatusRequestURITooLong added in v0.4.0

func (r *Response) StatusRequestURITooLong() *Response

func (*Response) StatusRequestedRangeNotSatisfiable added in v0.4.0

func (r *Response) StatusRequestedRangeNotSatisfiable() *Response

func (*Response) StatusResetContent added in v0.4.0

func (r *Response) StatusResetContent() *Response

func (*Response) StatusSeeOther added in v0.4.0

func (r *Response) StatusSeeOther() *Response

func (*Response) StatusServiceUnavailable added in v0.4.0

func (r *Response) StatusServiceUnavailable() *Response

func (*Response) StatusSwitchingProtocols added in v0.4.0

func (r *Response) StatusSwitchingProtocols() *Response

func (*Response) StatusTeapot added in v0.4.0

func (r *Response) StatusTeapot() *Response

func (*Response) StatusTemporaryRedirect added in v0.4.0

func (r *Response) StatusTemporaryRedirect() *Response

func (*Response) StatusTooManyRequests added in v0.4.0

func (r *Response) StatusTooManyRequests() *Response

func (*Response) StatusUnauthorized added in v0.4.0

func (r *Response) StatusUnauthorized() *Response

func (*Response) StatusUnavailableForLegalReasons added in v0.4.0

func (r *Response) StatusUnavailableForLegalReasons() *Response

func (*Response) StatusUnprocessableEntity added in v0.4.0

func (r *Response) StatusUnprocessableEntity() *Response

func (*Response) StatusUnsupportedMediaType added in v0.4.0

func (r *Response) StatusUnsupportedMediaType() *Response

func (*Response) StatusUpgradeRequired added in v0.4.0

func (r *Response) StatusUpgradeRequired() *Response

func (*Response) StatusUseProxy added in v0.4.0

func (r *Response) StatusUseProxy() *Response

func (*Response) StatusVariantAlsoNegotiates added in v0.4.0

func (r *Response) StatusVariantAlsoNegotiates() *Response

func (*Response) String

func (r *Response) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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