josuebrunel / echowbt

A wrapper around httptest to test echo apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EchoWBT

https://github.com/josuebrunel/echowbt/workflows/test/badge.svg?branch=master https://coveralls.io/repos/github/josuebrunel/echowbt/badge.svg?branch=master https://goreportcard.com/badge/github.com/josuebrunel/echowbt

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

Installation

go get github.com/josuebrunel/echowbt

Quickstart

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

client.SetHeaders(echow.DictString{"Content-Type": "text/html"})

URL Construction

// 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

headers := echowbt.DictString{"Content-Type": "application/x-www-form-urlencoded", "Authorization": "Token <mytoken>"}
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

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

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

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 ;) !

About

A wrapper around httptest to test echo apps

License:MIT License


Languages

Language:Go 98.7%Language:Makefile 1.3%