Documentation ¶
Overview ¶
Package fclient provides a testing client for http handlers.
To use it, you should write a testable app. You need to pass an object which implements http.Handler (has a ServeHTTP method) to the client
package app_test import ( "fmt" "net/http" "testing" "github.com/alexbyk/ftest/fclient" ) type MyApp struct{} func (app MyApp) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`{"foo": "bar"}`)) } func Test_hello(t *testing.T) { app := MyApp{} cl := fclient.New(t, app) cl.Get("/hello").CodeEq(200). BodyContains("bar"). JSONEq(`{"foo":"bar"}`) // using response directly res := cl.Get("/hello") fmt.Println(res.Body) }
You can also transform a simple function into http.Handler interface using http.HandlerFunc adapter
fn := func(w http.ResponseWriter, r *http.Request) {} cl := fclient.New(t, http.HandlerFunc(fn)) cl.Get("/") // ...
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { // handler is an instance of http.Handler Handler http.Handler // DefaultHeaders is a map for default headers DefaultHeaders map[string]string // Jar holds cookies. Can be set to nil to turn cookies off Jar http.CookieJar // contains filtered or unexported fields }
Client is a struct which holds current test, LastResponse and LastRequest instances
func (*Client) Do ¶
Do invokes the handler, passing a given argument as a request, and stores cookies from the response in Jar. A ".Domain" field of every cookie will be cleared
func (*Client) NewRequest ¶
NewRequest creates a new request (r) by httptest.NewRequest, then fills r.Headers from cl.DefaultHeaders, then fills cookies from cl.Jar and returns r
type Response ¶
type Response struct { *httptest.ResponseRecorder // contains filtered or unexported fields }
A Response represents the response from an HTTP request. It also inherits all methods from *httptest.ResponseRecorder
func NewResponse ¶
func NewResponse(t test) *Response
NewResponse returns a Response object with default ResponseRecorder
func (*Response) BodyContains ¶
BodyContains checks if a response body contains a given string
func (*Response) BodyEq ¶
BodyEq checks if a response body is equal to the given argument. Accepts []byte or string