EchoWBT is a simple wrapper of httptest allowing you to simply test your Echo app With EchoWBT handles for you :
- the instanciation of httptest.NewRequest and httptest.NewRecorder
- the binding of the two above with to an echo.Context
- the setting of request headers
- the setting of Path, ParamNames and ParamsValues for your context
go get github.com/josuebrunel/echowbt
import (
"github.com/josuebrunel/echowbt"
"github.com/project/app"
"testing"
"github.com/stretchr/testify/assert"
"net/http"
)
func TestPingHandler(t *testing.T)
client := echowbt.New()
rec := client.Get(echowbt.URL{"/ping"}, app.PingHandler(), nil, echowbt.DictString{"Authorization": "X-Auth xyw:uiyu"})
assert.Equal(t, http.StatusOK, rec.Code)
data := echowbt.JSONDecode(rec.Body)
assert.Equal(t, int64(1), data["count"])
assert.Equal(t, "ping", data["data"])
The default Content-Type is application/json. To change it use SetHeaders method
client.SetHeaders(echow.DictString{"Content-Type": "text/html"})
// simple url
url := echowbt.NewURL("/", nil, nil)
rec := client.Get(url, MyHanlder(), nil, nil)
// url with values e.g /:username/infos/
url = echowbt.NewURL("/:username/infos", map[string]string{"username": "loking"}, nil)
rec := client.Get(url, MyHanlder(), nil, nil)
// url with query string
url = echowbt.NewURL("/events/", nil, echowbt.DictString{"event_id": "23"})
rec := client.Get(url, MyHanlder(), nil, nil)
You can pass headers to your request by using echowbt.Headers type
headers := echowbt.DictString{"Content-Type": "application/x-www-form-urlencoded", "Authorization": "Token <mytoken>"}
rec := client.Post(url, MyHanlder(), []byte{"username=josh&password=joshpwd"}, headers)
You can send a JSON Payload by using echowbt.JSONEncode func
u := User{Username: "lokinghd"}
rec := client.Post(url, MyHanlder(), echowbt.JSONEncode(u), headers)
You can send a MultipartForm Data by using echowbt.FormData func
formFields := echowbt.DictString{"firstname": "Josué", "lastname": "Kouka", "City": "Pointe-Noire"}
fileFields := echowbt.DictString{"avatar": "/tmp/jk.png"}
formData, _ := echowbt.FormData(formFields, fileFields)
headers := echowbt.DictString{"Content-Type": formData.ContentType} // IMPORTANT FOR PART BOUNDARY
rec := client.Post(url, MyHanlder(), FormData.Data, headers)
You can decode your JSON Response by using echowbt.JSONDecode func
rec := client.Get(url, MyHanlder(), JSONEncode(payload), headers)
data = echowbt.JSONDecode(rec.Body)
assert.Equal(t, int64(1), data["count"])
assert.Equal(t, "uuid", data["data"]["uuid"])
For in depth examples check the main_test.go file
Voila ;) !