-
Notifications
You must be signed in to change notification settings - Fork 786
/
test.go
40 lines (30 loc) · 1.11 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package util
import (
"fmt"
"net/http"
)
func handleErr(request *http.Request, response http.ResponseWriter, err error) {
response.WriteHeader(http.StatusInternalServerError)
response.Write([]byte(err.Error())) //nolint:errcheck
}
func handleOk(response http.ResponseWriter, body []byte) {
response.WriteHeader(http.StatusOK)
response.Write(body) //nolint:errcheck
}
type MethodMap map[string]string
type Router map[string]MethodMap
// Are you a mod or a rocker? I'm a
type mocker func(http.ResponseWriter, *http.Request)
// @param dataDir Location of test data json file
// @param router Should map a URL path to a map that maps a method to a JSON response file name. Conceptually: (url, method) -> file
// See pkg/gits/bitbucket_cloud_test.go for an example.
func GetMockAPIResponseFromFile(dataDir string, route MethodMap) mocker {
return func(response http.ResponseWriter, request *http.Request) {
fileName := route[request.Method]
obj, err := LoadBytes(dataDir, fileName)
if err != nil {
handleErr(request, response, fmt.Errorf("request method: %s", request.Method))
}
handleOk(response, obj)
}
}