Skip to content

Commit

Permalink
Update examples and README (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed May 20, 2016
1 parent 4efa97e commit 0db99fa
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 19 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,29 @@

## Documentation

Documentation is available on [GoDoc](https://godoc.org/github.com/gavv/httpexpect).
Documentation is available on [GoDoc](https://godoc.org/github.com/gavv/httpexpect). It contains an overview and reference.

## Installation

```
$ 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 (
Expand Down
39 changes: 39 additions & 0 deletions example/echo.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
40 changes: 40 additions & 0 deletions example/echo_test.go
Original file line number Diff line number Diff line change
@@ -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!")
}
2 changes: 2 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package example is usage example for httpexpect.
package example
22 changes: 14 additions & 8 deletions example/fruits.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Package fruits is usage example for httpexpect.
package fruits
package example

import (
"encoding/json"
"net/http"
"path"
)

var (
fruits = make(map[string]interface{})
type (
fruitmap map[string]interface{}
)

// FruitServer creates http.Handler for fruits server.
Expand All @@ -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{}
Expand All @@ -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 {
Expand Down
61 changes: 54 additions & 7 deletions example/fruits_test.go
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build:
name: go get (repo)
code: |
go version
go get -v -t
go get -v -t ./...
- script:
name: go test
Expand Down

0 comments on commit 0db99fa

Please sign in to comment.