Skip to content

Commit

Permalink
Add new function for mock package
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoRage committed Nov 17, 2020
1 parent 1ca7d1b commit 3d9a5ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ func ParseJsonFromFilePath(file string, intoStruct interface{}) error {
return nil
}

func JsonFromFilePathToString(file string) (string, error) {
jsonFile, err := os.Open(file)
if err != nil {
return "", err
}
defer jsonFile.Close()

byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return "", err
}

return string(byteValue), nil
}

func CreateMockedAPI(funcsMap map[string]func(http.ResponseWriter, *http.Request)) http.Handler {
r := http.NewServeMux()
for pattern, f := range funcsMap {
Expand Down
8 changes: 8 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ func TestParseJsonFromFilePath(t *testing.T) {
assert.Nil(t, err)
assert.True(t, s.Status)
}

func TestJsonFromFilePathToString(t *testing.T) {
data, err := JsonFromFilePathToString("test.json")
assert.Nil(t, err)
assert.Equal(t, `{
"status": true
}`, data)
}

0 comments on commit 3d9a5ce

Please sign in to comment.