-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.go
81 lines (64 loc) 路 2.26 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
package server
import (
"fmt"
"strings"
"github.com/DATA-DOG/godog/gherkin"
"github.com/tomatool/tomato/resource"
)
type Resource interface {
resource.Resource
SetResponse(method string, requestPath string, responseCode int, responseBody []byte) error
SetResponseFromFile(method string, requestPath string, responseCode int, file string) error
GetRequestsCount(method, path string) (int, error)
}
type Handler struct {
r map[string]Resource
}
func New(r map[string]Resource) *Handler {
return &Handler{r}
}
func (h *Handler) setResponse(resourceName, path string, code int, body *gherkin.DocString) error {
return h.setResponseWithMethod(resourceName, "GET", path, code, body)
}
func (h *Handler) setResponseFromFile(resourceName, path string, code int, file string) error {
return h.setResponseWithMethodAndBodyFromFile(resourceName, "GET", path, code, file)
}
func (h *Handler) setResponseWithMethodAndNoBody(resourceName, method string, path string, code int) error {
return h.setResponseWithMethod(resourceName, method, path, code, nil)
}
func (h *Handler) setResponseWithMethodAndBodyFromFile(resourceName, method string, path string, code int, file string) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found to set response", resourceName)
}
return r.SetResponseFromFile(method, path, code, file)
}
func (h *Handler) setResponseWithMethod(resourceName, method string, path string, code int, body *gherkin.DocString) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found to set response", resourceName)
}
var bodyBytes []byte
if body != nil {
bodyBytes = []byte(body.Content)
}
return r.SetResponse(method, path, code, bodyBytes)
}
func (h *Handler) verifyRequestsCount(resourceName, target string, expectedRequestCount int) error {
r, ok := h.r[resourceName]
if !ok {
return fmt.Errorf("%s not found to set response", resourceName)
}
tt := strings.Split(target, " ")
if len(tt) != 2 {
return fmt.Errorf("unrecognized target format: %s, should follow `[METHOD] [PATH]`", target)
}
count, err := r.GetRequestsCount(tt[0], tt[1])
if err != nil {
return err
}
if count != expectedRequestCount {
return fmt.Errorf("expecting request count to be %d, got %d", expectedRequestCount, count)
}
return nil
}