-
-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters