-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrules.go
74 lines (58 loc) · 1.45 KB
/
rules.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
)
type Rules struct {
Rules []Rule `json:"rules"`
}
type Rule struct {
Endpoint string `json:"endpoint"`
Method string `json:"method"`
Items []EndpointRule `json:"items"`
}
type EndpointRule struct {
QueryString string `json:"queryString"`
Body string `json:"body"`
Counter *int `json:"counter,omitempty"`
Response EndpointResponse `json:"response"`
}
type EndpointResponse struct {
Status int `json:"status"`
Delay int `json:"delay"`
Headers []string `json:"headers"`
Body string `json:"body"`
}
func LoadRulesFromFile(fileName string) (Rules, error) {
var rules Rules
if len(fileName) == 0 {
return rules, nil
}
jsonFile, err := os.Open(fileName)
if err != nil {
return rules, err
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
if err := json.Unmarshal(byteValue, &rules); err != nil {
return rules, err
}
return rules, nil
}
func IsQueryStringMatchRule(requestQueryString string, ruleQueryString string) bool {
if len(ruleQueryString) > 0 {
r := regexp.MustCompile(fmt.Sprintf("%s%s", `(?m)`, ruleQueryString))
return r.MatchString(requestQueryString)
}
return true
}
func IsBodyMatchRule(bRequest string, bRule string) bool {
if len(bRule) > 0 {
sampleRegexp := regexp.MustCompile(bRule)
return sampleRegexp.MatchString(bRequest)
}
return true
}