-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
87 lines (71 loc) · 1.69 KB
/
mock.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"sync"
)
// Mock contains mock info
type Mock struct {
Name string `json:"name"`
Request Request `json:"request"`
Response Response `json:"response"`
Proxy string `json:"proxy"`
}
// MockCollection for work with mocks
type MockCollection struct {
mutex sync.Mutex
mocks []*Mock
}
// Lookup mock that looks like http request
func (c *MockCollection) Lookup(r *http.Request) *Mock {
for _, mock := range collection.mocks {
if mock.Request.LooksLike(r) {
return mock
}
}
return nil
}
func (c *MockCollection) Rebuild(files []string) error {
c.mutex.Lock()
defer c.mutex.Unlock()
c.mocks = []*Mock{}
for _, f := range files {
temp, err := os.ReadFile(f)
if err != nil {
return fmt.Errorf("Unable to read %s file", f)
}
var mocks []*Mock
if err := json.Unmarshal(temp, &mocks); err != nil {
return fmt.Errorf("Unable to parse %s file", f)
}
for _, m := range mocks {
if m.Name == "" {
m.Name = fmt.Sprintf("%s: %s %s", f, m.Request.Method, m.Request.URL)
}
}
for _, m := range mocks {
if m.Response.File != nil {
mockDir := filepath.Dir(f)
absPath, err := filepath.Abs(fmt.Sprintf("%s/%s", mockDir, *m.Response.File))
if err != nil {
return err
}
if _, err := os.Stat(absPath); os.IsNotExist(err) {
return fmt.Errorf("[%s] Data file %s not exists", m.Name, absPath)
}
m.Response.File = &absPath
}
}
c.mocks = append(c.mocks, mocks...)
}
sort.Slice(c.mocks, func(i, j int) bool {
return c.mocks[i].Request.Priority > c.mocks[j].Request.Priority
})
log.Println("Mocks found:", len(c.mocks))
return nil
}