echowbt

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 10 Imported by: 0

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONEncode

func JSONEncode(s interface{}) []byte

JSONEncode transforms interface into a []byte

Types

type Client

type Client struct {
	E *echo.Echo
	H DictString
}

Client represents the client instance

func New

func New() (c Client)

New returns a client instance

func (Client) Delete

func (c Client) Delete(url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Delete represents a Delete Request

func (Client) Get

func (c Client) Get(url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Get represents a Get Request

func (Client) Patch

func (c Client) Patch(url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Patch represents a Patch Request

func (Client) Post

func (c Client) Post(url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Post represents a Post Request

func (Client) Put

func (c Client) Put(url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Put represents a Put Request

func (Client) Request

func (c Client) Request(method string, url URL, handler echo.HandlerFunc, data []byte, headers DictString) *httptest.ResponseRecorder

Request is the method performing the request

func (*Client) SetHeaders

func (c *Client) SetHeaders(headers DictString)

SetHeaders allow you define some headers

type Dict

type Dict map[string]interface{}

Dict represents a dict object

func JSONDecode

func JSONDecode(b *bytes.Buffer) Dict

JSONDecode returns an interface from a json formatted http.ResponseRecorder.Body

type DictString

type DictString map[string]string

DictString represents a dict of string key and values

type MultiPartForm

type MultiPartForm struct {
	Data        []byte
	ContentType string
}

MultiPartForm is the struct returned by FormData func

func FormData

func FormData(fields DictString, files DictString) (MultiPartForm, error)

FormData helps create a form data payload

type URL

type URL struct {
	Path        string
	Params      []string
	Values      []string
	QueryString map[string]string
}

URL is a type representing an URL with : * Path * Params * Values

func NewURL

func NewURL(p string, uv DictString, qs DictString) (u URL)

NewURL is a function returning an URL struct p as Path uv as Url Values ( e.g /:username/ ) qs as Url Query String ( e.g /?debug=true)

func (URL) String

func (u URL) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL