-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_driver.go
88 lines (64 loc) · 1.98 KB
/
mock_driver.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
package eygo
import (
"net/url"
)
type MockDriver struct {
requests map[string][]string
responses *responseCollection
}
func NewMockDriver() *MockDriver {
return &MockDriver{}
}
func (driver *MockDriver) Get(path string, params Params) Response {
return driver.handle("get", path+driver.processParams(params))
}
func (driver *MockDriver) Post(path string, params Params, data []byte) Response {
return driver.handle("post", path+driver.processParams(params))
}
func (driver *MockDriver) Put(path string, params Params, data []byte) Response {
return driver.handle("put", path+driver.processParams(params))
}
func (driver *MockDriver) Patch(path string, params Params, data []byte) Response {
return driver.handle("patch", path+driver.processParams(params))
}
func (driver *MockDriver) Delete(path string, params Params) Response {
return driver.handle("delete", path+driver.processParams(params))
}
func (driver *MockDriver) Reset() {
driver.requests = nil
driver.responses = nil
driver.setup()
}
func (driver *MockDriver) Requests(method string) []string {
var requests []string
driver.setup()
requests = append(requests, driver.requests[method]...)
return requests
}
func (driver *MockDriver) AddResponse(method string, path string, response Response) {
driver.setup()
driver.responses.add(method, path, response)
}
func (driver *MockDriver) RemoveResponse(method string, path string) {
driver.setup()
driver.responses.remove(method, path)
}
func (driver *MockDriver) handle(method string, path string) Response {
driver.setup()
driver.requests[method] = append(driver.requests[method], path)
return driver.responses.consume(method, path)
}
func (driver *MockDriver) processParams(params Params) string {
if len(params) > 0 {
return "?" + url.Values(params).Encode()
}
return ""
}
func (driver *MockDriver) setup() {
if driver.responses == nil {
driver.responses = &responseCollection{}
}
if driver.requests == nil {
driver.requests = make(map[string][]string)
}
}