From 1ca7d1bfe42ae4572fcff8ea49272de5d2a436bf Mon Sep 17 00:00:00 2001 From: Nick Kozlov <22479658+EnoRage@users.noreply.github.com> Date: Tue, 17 Nov 2020 15:26:41 +0300 Subject: [PATCH] Add mock package for tests with mocks --- go.mod | 1 + go.sum | 2 ++ mock/mock.go | 34 ++++++++++++++++++++++++++++++++++ mock/mock_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ mock/test.json | 3 +++ 5 files changed, 83 insertions(+) create mode 100644 mock/mock.go create mode 100644 mock/mock_test.go create mode 100644 mock/test.json diff --git a/go.mod b/go.mod index be1e052..20330de 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/imroc/req v0.3.0 github.com/kr/pretty v0.1.0 // indirect github.com/shopspring/decimal v1.2.0 github.com/stretchr/testify v1.6.1 diff --git a/go.sum b/go.sum index a41f7be..465958a 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ 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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U= +github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/mock/mock.go b/mock/mock.go new file mode 100644 index 0000000..8267e0f --- /dev/null +++ b/mock/mock.go @@ -0,0 +1,34 @@ +package mock + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "os" +) + +func ParseJsonFromFilePath(file string, intoStruct interface{}) error { + jsonFile, err := os.Open(file) + if err != nil { + return err + } + defer jsonFile.Close() + + byteValue, err := ioutil.ReadAll(jsonFile) + if err != nil { + return err + } + err = json.Unmarshal(byteValue, &intoStruct) + if err != nil { + return err + } + return nil +} + +func CreateMockedAPI(funcsMap map[string]func(http.ResponseWriter, *http.Request)) http.Handler { + r := http.NewServeMux() + for pattern, f := range funcsMap { + r.HandleFunc(pattern, f) + } + return r +} diff --git a/mock/mock_test.go b/mock/mock_test.go new file mode 100644 index 0000000..8408d60 --- /dev/null +++ b/mock/mock_test.go @@ -0,0 +1,43 @@ +package mock + +import ( + "fmt" + "github.com/imroc/req" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" +) + +func TestCreateMockedAPI(t *testing.T) { + data := make(map[string]func(http.ResponseWriter, *http.Request)) + data["/1"] = func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + if _, err := fmt.Fprint(w, `{"status": true}`); err != nil { + panic(err) + } + } + + server := httptest.NewServer(CreateMockedAPI(data)) + defer server.Close() + + resp, err := req.Get(server.URL + "/1") + assert.Nil(t, err) + type S struct { + Status bool + } + var s S + + assert.Nil(t, resp.ToJSON(&s)) + assert.True(t, s.Status) +} + +func TestParseJsonFromFilePath(t *testing.T) { + type S struct { + Status bool + } + var s S + err := ParseJsonFromFilePath("test.json", &s) + assert.Nil(t, err) + assert.True(t, s.Status) +} diff --git a/mock/test.json b/mock/test.json new file mode 100644 index 0000000..bc9697e --- /dev/null +++ b/mock/test.json @@ -0,0 +1,3 @@ +{ + "status": true +} \ No newline at end of file