htest

package module
v0.2.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 {
	// contains filtered or unexported fields
}

func NewClient

func NewClient() *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
}

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) *Response

func (*Response) BadRequest

func (r *Response) BadRequest() *Response

func (*Response) Bind

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

func (*Response) Bytes

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

func (*Response) JSON

func (r *Response) JSON() *JSON

func (*Response) OK

func (r *Response) OK() *Response

func (*Response) String

func (r *Response) String() string

func (*Response) With

func (r *Response) With(t *testing.T) *Response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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