forked from plouc/go-gitlab-client
/
server.go
129 lines (105 loc) · 3.06 KB
/
server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
type Mapping struct {
Request struct {
Method string `json:"method"`
Path string `json:"path"`
Query map[string]string `json:"query"`
} `json:"request"`
Response struct {
Status int `json:"status"`
BodyFileName string `json:"bodyFileName"`
BodyContent string `json:"bodyContent"`
Headers map[string]string `json:"headers"`
} `json:"response"`
}
func (m *Mapping) Match(r *http.Request) bool {
if m.Request.Method != r.Method {
return false
}
if m.Request.Path != r.URL.Path {
return false
}
for k, v := range m.Request.Query {
if r.URL.Query().Get(k) != v {
return false
}
}
return true
}
func getMocksDirs(t *testing.T) (string, string) {
d := baseDir(t)
mocksDir := filepath.Join(filepath.Join(d, ".."), "mocks")
return filepath.Join(mocksDir, "mappings"), filepath.Join(mocksDir, "files")
}
func serverFromMappings(t *testing.T, mappings []*Mapping) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, m := range mappings {
if m.Match(r) {
for k, v := range m.Response.Headers {
w.Header().Set(k, v)
}
w.WriteHeader(m.Response.Status)
w.Write([]byte(m.Response.BodyContent))
return
}
}
t.Errorf("no request found matching: %s %s\nevery request has to be mocked!", r.Method, r.URL.Path)
}))
return ts
}
func CreateMockServerFromDir(t *testing.T, d string) *httptest.Server {
mappingsDir, _ := getMocksDirs(t)
var m []string
walkErr := filepath.Walk(filepath.Join(mappingsDir, d), func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && filepath.Ext(f.Name()) == ".json" {
f := filepath.Base(path)
f = f[:len(f)-5]
m = append(m, filepath.Join(d, f))
}
return nil
})
if walkErr != nil {
t.Fatal(fmt.Sprintf("an error occurred while traversing mappings dir %s:\n%v", d, walkErr))
return nil
}
return CreateMockServer(t, m)
}
func CreateMockServer(t *testing.T, mappingPaths []string) *httptest.Server {
mappingsDir, filesDir := getMocksDirs(t)
var mappings []*Mapping
for _, p := range mappingPaths {
f := filepath.Join(mappingsDir, fmt.Sprintf("%s.json", p))
contents, err := ioutil.ReadFile(f)
if err != nil {
t.Fatal(fmt.Sprintf("an error occurred while loading mapping file %s\n%v", f, err))
return nil
}
mapping := Mapping{}
err = json.Unmarshal(contents, &mapping)
if err != nil {
t.Fatal(fmt.Sprintf("an error occurred while unmarshalling mapping %s\n%v", f, err))
return nil
}
if mapping.Response.BodyFileName != "" {
b := filepath.Join(filesDir, mapping.Response.BodyFileName)
body, err := ioutil.ReadFile(b)
if err != nil {
t.Fatal(fmt.Sprintf("an error occurred while loading mapping response body: %s\n%v", b, err))
return nil
}
mapping.Response.BodyContent = string(body)
}
mappings = append(mappings, &mapping)
}
return serverFromMappings(t, mappings)
}