Skip to content

Commit

Permalink
Add Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed May 19, 2016
1 parent bd0d1cb commit 916e4b9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
40 changes: 40 additions & 0 deletions binder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package httpexpect

import (
"net/http"
"net/http/httptest"
)

// Binder implements networkless Client attached directly to http.Handler.
//
// Binder emulates network communication by using given http.Handler directly.
// It passes httptest.ResponseRecorder as http.ResponseWriter to the handler,
// and then construct http.Response from recorded data.
type Binder struct {
handler http.Handler
}

// NewBinder returns a new Binder given http.Handler.
func NewBinder(handler http.Handler) *Binder {
return &Binder{handler}
}

// Do implements Client.Do.
func (binder *Binder) Do(req *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()

binder.handler.ServeHTTP(recorder, req)

resp := http.Response{
Request: req,
StatusCode: recorder.Code,
Status: http.StatusText(recorder.Code),
Header: recorder.HeaderMap,
}

if recorder.Body != nil {
resp.Body = readCloserAdapter{recorder.Body}
}

return &resp, nil
}
56 changes: 56 additions & 0 deletions binder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package httpexpect

import (
"bytes"
"io/ioutil"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

type mockHandler struct {
t *testing.T
}

func (c mockHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
assert.True(c.t, err == nil)

assert.Equal(c.t, "GET", req.Method)
assert.Equal(c.t, "http://example.com", req.URL.String())
assert.Equal(c.t, "body", string(body))

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`{"hello":"world"}`))

assert.True(c.t, err == nil)
}

func TestBinder(t *testing.T) {
binder := NewBinder(mockHandler{t})

req, err := http.NewRequest(
"GET", "http://example.com", bytes.NewReader([]byte("body")))

if err != nil {
t.Fatal(err)
}

resp, err := binder.Do(req)
if err != nil {
t.Fatal(err)
}

header := http.Header{
"Content-Type": {"application/json"},
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, header, resp.Header)
assert.Equal(t, `{"hello":"world"}`, string(b))
}
2 changes: 1 addition & 1 deletion expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type Config struct {
}

// Client is used to send http.Request and receive http.Response.
// Note that http.Client implements this interface.
// http.Client, Binder, and fasthttpexpect.Binder implement this interface.
type Client interface {
// Do sends request and returns response.
Do(*http.Request) (*http.Response, error)
Expand Down
29 changes: 27 additions & 2 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,18 @@ func TestExpectLiveFast(t *testing.T) {
}))
}

func BenchmarkExpectStandard(b *testing.B) {
func TestExpectBinderStandard(t *testing.T) {
handler := createHandler()

testHandler(WithConfig(Config{
BaseURL: "http://example.com",
Client: NewBinder(handler),
Reporter: NewAssertReporter(t),
Printer: NewDebugPrinter(t, true),
}))
}

func BenchmarkExpectLiveStandard(b *testing.B) {
handler := createHandler()

server := httptest.NewServer(handler)
Expand All @@ -254,7 +265,7 @@ func BenchmarkExpectStandard(b *testing.B) {
}
}

func BenchmarkExpectFast(b *testing.B) {
func BenchmarkExpectLiveFast(b *testing.B) {
handler := createHandler()

server := httptest.NewServer(handler)
Expand All @@ -270,3 +281,17 @@ func BenchmarkExpectFast(b *testing.B) {
testHandler(e)
}
}

func BenchmarkExpectBinderStandard(b *testing.B) {
handler := createHandler()

e := WithConfig(Config{
BaseURL: "http://example.com",
Client: NewBinder(handler),
Reporter: NewRequireReporter(b),
})

for i := 0; i < b.N; i++ {
testHandler(e)
}
}

0 comments on commit 916e4b9

Please sign in to comment.