diff --git a/README.md b/README.md index e5653ff85..a13c0de6f 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,21 @@ Documentation is available on [GoDoc](https://godoc.org/github.com/gavv/httpexpe $ go get github.com/gavv/httpexpect ``` -## Example +## Examples -See [`example`](example) directory for complete sources of fruits server and test. +See [`example`](example) directory for various usage examples. + +* [`fruits_test.go`](example/fruits_test.go) + + Using httpexpect with default and custom config. Communicating with server via HTTP client or invoking `http.Handler` directly. + +* [`echo_test.go`](example/echo_test.go) + + Using httpexpect with two http handlers obtained with [`echo`](https://github.com/labstack/echo/) framework: `http.Handler` and `fasthttp.RequestHandler`. + +## Quick start + +Here is a complete example of end-to-end test for [`FruitServer`](example/fruits.go). ```go import ( diff --git a/example/echo.go b/example/echo.go new file mode 100644 index 000000000..e222afbe9 --- /dev/null +++ b/example/echo.go @@ -0,0 +1,39 @@ +package example + +import ( + "github.com/labstack/echo" + echofast "github.com/labstack/echo/engine/fasthttp" + echostandard "github.com/labstack/echo/engine/standard" + "github.com/valyala/fasthttp" + "net/http" +) + +// EchoServer creates HTTP server using echo framework. +// +// Implemented API: +// GET /hello print "hello, world" +func EchoServer() *echo.Echo { + ec := echo.New() + + ec.GET("/hello", func(ctx echo.Context) error { + return ctx.String(http.StatusOK, "hello, world!") + }) + + return ec +} + +// EchoHandlerStandard creates http.Handler for EchoServer(). +func EchoHandlerStandard() http.Handler { + server := echostandard.New("") + server.SetHandler(EchoServer()) + return http.Handler(server) +} + +// EchoHandlerFast creates fasthttp.RequestHandler for EchoServer(). +func EchoHandlerFast() fasthttp.RequestHandler { + server := echofast.New("") + server.SetHandler(EchoServer()) + return func(ctx *fasthttp.RequestCtx) { + server.ServeHTTP(ctx) + } +} diff --git a/example/echo_test.go b/example/echo_test.go new file mode 100644 index 000000000..224897d50 --- /dev/null +++ b/example/echo_test.go @@ -0,0 +1,40 @@ +package example + +import ( + "github.com/gavv/httpexpect" + "github.com/gavv/httpexpect/fasthttpexpect" + "net/http" + "testing" +) + +func TestEcho_Standard(t *testing.T) { + // create http.Handler + handler := EchoHandlerStandard() + + // create httpexpect instance that will call htpp.Handler directly + e := httpexpect.WithConfig(httpexpect.Config{ + Reporter: httpexpect.NewAssertReporter(t), + Client: httpexpect.NewBinder(handler), + }) + + // run tests + e.GET("/hello"). + Expect(). + Status(http.StatusOK).Body().Equal("hello, world!") +} + +func TestEcho_Fast(t *testing.T) { + // create fasthttp.RequestHandler + handler := EchoHandlerFast() + + // create httpexpect instance that will call fasthtpp.RequestHandler directly + e := httpexpect.WithConfig(httpexpect.Config{ + Reporter: httpexpect.NewAssertReporter(t), + Client: fasthttpexpect.NewBinder(handler), + }) + + // run tests + e.GET("/hello"). + Expect(). + Status(http.StatusOK).Body().Equal("hello, world!") +} diff --git a/example/example.go b/example/example.go new file mode 100644 index 000000000..cf91d3a35 --- /dev/null +++ b/example/example.go @@ -0,0 +1,2 @@ +// Package example is usage example for httpexpect. +package example diff --git a/example/fruits.go b/example/fruits.go index 4dab8e0f4..71f5029f8 100644 --- a/example/fruits.go +++ b/example/fruits.go @@ -1,5 +1,4 @@ -// Package fruits is usage example for httpexpect. -package fruits +package example import ( "encoding/json" @@ -7,8 +6,8 @@ import ( "path" ) -var ( - fruits = make(map[string]interface{}) +type ( + fruitmap map[string]interface{} ) // FruitServer creates http.Handler for fruits server. @@ -18,15 +17,22 @@ var ( // GET /fruits/{name} get fruit // PUT /fruits/{name} add or update fruit func FruitServer() http.Handler { + fruits := fruitmap{} + mux := http.NewServeMux() - mux.HandleFunc("/fruits", handleFruitList) - mux.HandleFunc("/fruits/", handleFruit) + mux.HandleFunc("/fruits", func(w http.ResponseWriter, r *http.Request) { + handleFruitList(fruits, w, r) + }) + + mux.HandleFunc("/fruits/", func(w http.ResponseWriter, r *http.Request) { + handleFruit(fruits, w, r) + }) return mux } -func handleFruitList(w http.ResponseWriter, r *http.Request) { +func handleFruitList(fruits fruitmap, w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": ret := []string{} @@ -47,7 +53,7 @@ func handleFruitList(w http.ResponseWriter, r *http.Request) { } } -func handleFruit(w http.ResponseWriter, r *http.Request) { +func handleFruit(fruits fruitmap, w http.ResponseWriter, r *http.Request) { _, name := path.Split(r.URL.Path) switch r.Method { diff --git a/example/fruits_test.go b/example/fruits_test.go index 043e6e33f..55e0aee4d 100644 --- a/example/fruits_test.go +++ b/example/fruits_test.go @@ -1,18 +1,14 @@ -package fruits +package example import ( "github.com/gavv/httpexpect" "net/http" "net/http/httptest" "testing" + "time" ) -func TestFruits(t *testing.T) { - server := httptest.NewServer(FruitServer()) - defer server.Close() - - e := httpexpect.New(t, server.URL) - +func runFruitsTests(e *httpexpect.Expect) { e.GET("/fruits"). Expect(). Status(http.StatusOK).JSON().Array().Empty() @@ -63,3 +59,54 @@ func TestFruits(t *testing.T) { Expect(). Status(http.StatusNotFound) } + +func TestFruits_DefaultClient(t *testing.T) { + // create http.Handler + handler := FruitServer() + + // start server using httptest + server := httptest.NewServer(handler) + defer server.Close() + + // create httpexpect instance using http.DefaultClient + e := httpexpect.New(t, server.URL) + + // run tests + runFruitsTests(e) +} + +func TestFruits_CustomClientAndConfig(t *testing.T) { + // create http.Handler + handler := FruitServer() + + // start server using httptest + server := httptest.NewServer(handler) + defer server.Close() + + // create httpexpect instance using custom config + e := httpexpect.WithConfig(httpexpect.Config{ + BaseURL: server.URL, + Printer: httpexpect.NewDebugPrinter(t, true), + Reporter: httpexpect.NewAssertReporter(t), + Client: &http.Client{ + Timeout: time.Second * 30, + }, + }) + + // run tests + runFruitsTests(e) +} + +func TestFruits_UseHandlerDirectly(t *testing.T) { + // create http.Handler + handler := FruitServer() + + // create httpexpect instance that will call htpp.Handler directly + e := httpexpect.WithConfig(httpexpect.Config{ + Reporter: httpexpect.NewAssertReporter(t), + Client: httpexpect.NewBinder(handler), + }) + + // run tests + runFruitsTests(e) +} diff --git a/wercker.yml b/wercker.yml index ef716d0bd..5f8685e91 100644 --- a/wercker.yml +++ b/wercker.yml @@ -14,7 +14,7 @@ build: name: go get (repo) code: | go version - go get -v -t + go get -v -t ./... - script: name: go test