-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_collection.go
63 lines (45 loc) · 1.32 KB
/
response_collection.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
package eygo
import (
"errors"
)
type responseCollection struct {
responses map[string][]Response
}
func (rc *responseCollection) add(method string, path string, response Response) {
identifier := rc.identify(method, path)
rc.setup(identifier)
rc.responses[identifier] = append(rc.responses[identifier], response)
}
func (rc *responseCollection) remove(method string, path string) {
identifier := rc.identify(method, path)
rc.responses[identifier] = nil
rc.setup(identifier)
}
func (rc *responseCollection) consume(method string, path string) Response {
rc.setup("")
identifier := rc.identify(method, path)
if len(rc.responses[identifier]) == 0 {
return Response{Error: errors.New("No response")}
}
response := rc.responses[identifier][0]
rc.trim(identifier)
return response
}
func (rc *responseCollection) trim(identifier string) {
if len(rc.responses[identifier]) == 1 {
rc.responses[identifier] = nil
} else {
rc.responses[identifier] = rc.responses[identifier][1:]
}
}
func (rc *responseCollection) identify(method string, path string) string {
return method + ":" + path
}
func (rc *responseCollection) setup(scope string) {
if rc.responses == nil {
rc.responses = make(map[string][]Response)
}
if len(scope) > 0 && rc.responses[scope] == nil {
rc.responses[scope] = make([]Response, 0)
}
}