diff --git a/httput/README.md b/httput/README.md new file mode 100644 index 0000000..45077df --- /dev/null +++ b/httput/README.md @@ -0,0 +1,56 @@ +# Httput + +[![Source](https://img.shields.io/badge/git-source-orange.svg?style=flat-square)](https://github.com/ymohl-cl/gopkg/tree/httput-release/httput) +[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/ymohl-cl/gopkg/httput) +[![Build Status](https://travis-ci.org/ymohl-cl/gopkg.svg?branch=httput-release&style=flat-square)](https://travis-ci.org/ymohl-cl/gopkg) +[![codecov](https://codecov.io/gh/ymohl-cl/gopkg/branch/httput-release/graph/badge.svg?style=flat-square)](https://codecov.io/gh/ymohl-cl/gopkg) +[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/ymohl-cl/gopkg/httput-release/LICENSE) + +httput package provide a tool suite to make an unitaries tests arround http and echo framework. + +## Requirements + +Golang 1.12.4 or higher + +download the package + +``` bash +go get -u github.com/ymohl-cl/gopkg/httput +``` + +## Usage + +``` Golang +import "github.com/ymohl-cl/gopkg/httput" + +func main() { + // build a request + request := httptest.NewRequest(...) + + // build a fake echo context + c := httput.NewContext(request) + + // the call your handlers to test them + _ = MyHandler(c) + + // finally print the response content + fmt.Printf("status code: %d", c.Rec.Code) + fmt.Printf("content body: %s", c.Rec.Body.String()) +} +``` + +Output: + +``` bash +> "status code: 200" +> "content body: {\"status\":\"200\"}" +``` + +## Changelog + +### v0.0.1 + +Initial commit + +- fake echo context to tests handlers +- tests and documentation \ No newline at end of file diff --git a/httput/echo.go b/httput/echo.go new file mode 100644 index 0000000..ac5d3a4 --- /dev/null +++ b/httput/echo.go @@ -0,0 +1,25 @@ +package httput + +import ( + "net/http" + "net/http/httptest" + + "github.com/labstack/echo" +) + +// Context take an echo context and a httptest.Record to handlers tests +type Context struct { + Input echo.Context + Rec *httptest.ResponseRecorder +} + +// NewContext return a context to handlers tests +func NewContext(req *http.Request) Context { + var c Context + var e *echo.Echo + + e = echo.New() + c.Rec = httptest.NewRecorder() + c.Input = e.NewContext(req, c.Rec) + return c +} diff --git a/httput/echo_test.go b/httput/echo_test.go new file mode 100644 index 0000000..9575ac8 --- /dev/null +++ b/httput/echo_test.go @@ -0,0 +1,27 @@ +package httput + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewContext(t *testing.T) { + // Default: should be ok + func() { + var c Context + var request *http.Request + var err error + payload := map[string]interface{}{ + "Name": "Toto", + } + + if request, err = RequestJSON(http.MethodPost, "/path", &payload); err != nil { + t.Error(err) + } + c = NewContext(request) + assert.NotNil(t, c.Input) + assert.NotNil(t, c.Rec) + }() +} diff --git a/httput/go.mod b/httput/go.mod new file mode 100644 index 0000000..a5bbd5a --- /dev/null +++ b/httput/go.mod @@ -0,0 +1,12 @@ +module github.com/ymohl-cl/gopkg/httput + +require ( + github.com/labstack/echo v3.3.5+incompatible + github.com/labstack/gommon v0.2.8 // indirect + github.com/mattn/go-colorable v0.0.9 // indirect + github.com/mattn/go-isatty v0.0.4 // indirect + github.com/stretchr/testify v1.3.0 + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 // indirect + golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 // indirect +) diff --git a/httput/go.sum b/httput/go.sum new file mode 100644 index 0000000..ccbb776 --- /dev/null +++ b/httput/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/labstack/echo v3.3.5+incompatible h1:9PfxPUmasKzeJor9uQTaXLT6WUG/r+vSTmvXxvv3JO4= +github.com/labstack/echo v3.3.5+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= +github.com/labstack/gommon v0.2.8 h1:JvRqmeZcfrHC5u6uVleB4NxxNbzx6gpbJiQknDbKQu0= +github.com/labstack/gommon v0.2.8/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFYA+wQNJ4= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4 h1:gKMu1Bf6QINDnvyZuTaACm9ofY+PRh+5vFz4oxBZeF8= +github.com/valyala/fasttemplate v0.0.0-20170224212429-dcecefd839c4/go.mod h1:50wTf68f99/Zt14pr046Tgt3Lp2vLyFZKzbFXTOabXw= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0= +golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/httput/request.go b/httput/request.go new file mode 100644 index 0000000..7ff5cdd --- /dev/null +++ b/httput/request.go @@ -0,0 +1,23 @@ +package httput + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + + "github.com/labstack/echo" +) + +// RequestJSON build a httptest request with the specific payload +func RequestJSON(method, path string, payload interface{}) (*http.Request, error) { + var b []byte + var err error + + if b, err = json.Marshal(payload); err != nil { + return nil, err + } + req := httptest.NewRequest(method, path, bytes.NewBuffer(b)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + return req, nil +} diff --git a/httput/request_test.go b/httput/request_test.go new file mode 100644 index 0000000..f7f7566 --- /dev/null +++ b/httput/request_test.go @@ -0,0 +1,42 @@ +package httput + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + path = "/path_to_" +) + +func TestRequestJSON(t *testing.T) { + // Default: should be ok + func() { + var request *http.Request + var err error + p := map[string]interface{}{ + "Name": "Toto", + } + + request, err = RequestJSON(http.MethodPost, "/path", &p) + if assert.NoError(t, err) { + assert.NotNil(t, request) + } + }() + + // Should return an error because payload isn't marshalable + func() { + var request *http.Request + var err error + p := map[string]interface{}{ + "Name": make(chan int), + } + + request, err = RequestJSON(http.MethodPost, "/path", p) + if assert.Error(t, err) { + assert.Nil(t, request) + } + }() +}