https://github.com/josuebrunel/echowbt
A wrapper around httptest to test echo apps
https://github.com/josuebrunel/echowbt
echo echo-framework http-client test web webtest
Last synced: 5 months ago
JSON representation
A wrapper around httptest to test echo apps
- Host: GitHub
- URL: https://github.com/josuebrunel/echowbt
- Owner: josuebrunel
- License: mit
- Created: 2020-12-12T03:30:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-14T00:17:39.000Z (about 3 years ago)
- Last Synced: 2025-11-22T15:17:43.711Z (7 months ago)
- Topics: echo, echo-framework, http-client, test, web, webtest
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
EchoWBT
=======
.. image:: https://github.com/josuebrunel/echowbt/workflows/test/badge.svg?branch=master
:target: https://github.com/josuebrunel/echowbt/actions?query=workflow%3Atest
.. image:: https://coveralls.io/repos/github/josuebrunel/echowbt/badge.svg?branch=master
:target: https://coveralls.io/github/josuebrunel/echowbt?branch=master
.. image:: https://pkg.go.dev/badge/github.com/josuebrunel/echowbt.svg
:target: https://pkg.go.dev/github.com/josuebrunel/echowbt
.. image:: https://goreportcard.com/badge/github.com/josuebrunel/echowbt
:target: https://goreportcard.com/report/github.com/josuebrunel/echowbt
.. image:: https://img.shields.io/badge/License-MIT-blue.svg
:target: https://github.com/josuebrunel/echowbt/blob/master/LICENSE
**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
.. _Echo: https://github.com/labstack/echo
Installation
------------
.. code:: go
go get github.com/josuebrunel/echowbt
Quickstart
----------
.. code:: go
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"])
Set a default content type
^^^^^^^^^^^^^^^^^^^^^^^^^^
The default *Content-Type* is *application/json*. To change it use *SetHeaders* method
.. code:: go
client.SetHeaders(echow.DictString{"Content-Type": "text/html"})
URL Construction
^^^^^^^^^^^^^^^^
.. code:: go
// 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)
Headers
^^^^^^^
You can pass *headers* to your request by using *echowbt.Headers* type
.. code:: go
headers := echowbt.DictString{"Content-Type": "application/x-www-form-urlencoded", "Authorization": "Token "}
rec := client.Post(url, MyHanlder(), []byte{"username=josh&password=joshpwd"}, headers)
Send JSON Data
^^^^^^^^^^^^^^
You can send a *JSON Payload* by using *echowbt.JSONEncode* func
.. code:: go
u := User{Username: "lokinghd"}
rec := client.Post(url, MyHanlder(), echowbt.JSONEncode(u), headers)
Send MultipartForm Data
^^^^^^^^^^^^^^^^^^^^^^^
You can send a *MultipartForm Data* by using *echowbt.FormData* func
.. code:: go
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)
Decoding JSON Response
^^^^^^^^^^^^^^^^^^^^^^
You can decode your JSON Response by using *echowbt.JSONDecode* func
.. Code:: go
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 ;) !