-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mock package for tests with mocks
- Loading branch information
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"status": true | ||
} |