|
7 | 7 | . "github.com/SpectoLabs/hoverfly/core/util"
|
8 | 8 | "github.com/SpectoLabs/hoverfly/core/models"
|
9 | 9 | "github.com/SpectoLabs/hoverfly/core/views"
|
10 |
| - "reflect" |
| 10 | + "github.com/ryanuber/go-glob" |
| 11 | + "strings" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | type RequestTemplateStore []RequestTemplateResponsePair
|
@@ -43,25 +44,25 @@ func (this *RequestTemplateStore) GetResponse(req models.RequestDetails, webserv
|
43 | 44 | // TODO: need to enable regex matches
|
44 | 45 | // TODO: enable matching on scheme
|
45 | 46 |
|
46 |
| - if entry.RequestTemplate.Body != nil && *entry.RequestTemplate.Body != req.Body { |
| 47 | + if entry.RequestTemplate.Body != nil && !glob.Glob(*entry.RequestTemplate.Body, req.Body) { |
47 | 48 | continue
|
48 | 49 | }
|
49 | 50 |
|
50 | 51 | if !webserver {
|
51 |
| - if entry.RequestTemplate.Destination != nil && *entry.RequestTemplate.Destination != req.Destination { |
| 52 | + if entry.RequestTemplate.Destination != nil && !glob.Glob(*entry.RequestTemplate.Destination, req.Destination) { |
52 | 53 | continue
|
53 | 54 | }
|
54 | 55 | }
|
55 |
| - if entry.RequestTemplate.Path != nil && *entry.RequestTemplate.Path != req.Path { |
| 56 | + if entry.RequestTemplate.Path != nil && !glob.Glob(*entry.RequestTemplate.Path, req.Path) { |
56 | 57 | continue
|
57 | 58 | }
|
58 |
| - if entry.RequestTemplate.Query != nil && *entry.RequestTemplate.Query != req.Query { |
| 59 | + if entry.RequestTemplate.Query != nil && !glob.Glob(*entry.RequestTemplate.Query, req.Query) { |
59 | 60 | continue
|
60 | 61 | }
|
61 | 62 | if !headerMatch(entry.RequestTemplate.Headers, req.Headers) {
|
62 | 63 | continue
|
63 | 64 | }
|
64 |
| - if entry.RequestTemplate.Method != nil && *entry.RequestTemplate.Method != req.Method { |
| 65 | + if entry.RequestTemplate.Method != nil && !glob.Glob(*entry.RequestTemplate.Method, req.Method) { |
65 | 66 | continue
|
66 | 67 | }
|
67 | 68 |
|
@@ -99,18 +100,31 @@ func (this *RequestTemplateStore) Wipe() {
|
99 | 100 | /**
|
100 | 101 | Check keys and corresponding values in template headers are also present in request headers
|
101 | 102 | */
|
102 |
| -func headerMatch(tmplHeaders, reqHeaders map[string][]string) bool { |
| 103 | +func headerMatch(templateHeaders, requestHeaders map[string][]string) bool { |
103 | 104 |
|
104 |
| - for headerName, headerVal := range tmplHeaders { |
105 |
| - // TODO: case insensitive lookup |
106 |
| - // TODO: is order of values in slice really important? |
| 105 | + for templateHeaderKey, templateHeaderValues := range templateHeaders { |
| 106 | + for requestHeaderKey, requestHeaderValues := range requestHeaders { |
| 107 | + delete(requestHeaders, requestHeaderKey) |
| 108 | + requestHeaders[strings.ToLower(requestHeaderKey)] = requestHeaderValues |
107 | 109 |
|
108 |
| - reqHeaderVal, ok := reqHeaders[headerName] |
109 |
| - if ok && reflect.DeepEqual(headerVal, reqHeaderVal) { |
110 |
| - continue |
111 |
| - } else { |
| 110 | + } |
| 111 | + requestTemplateValues, templateHeaderMatched := requestHeaders[strings.ToLower(templateHeaderKey)] |
| 112 | + if !templateHeaderMatched { |
112 | 113 | return false
|
113 | 114 | }
|
| 115 | + |
| 116 | + for _, templateHeaderValue := range templateHeaderValues { |
| 117 | + templateValueMatched := false |
| 118 | + for _, requestHeaderValue := range requestTemplateValues { |
| 119 | + if glob.Glob(strings.ToLower(templateHeaderValue), strings.ToLower(requestHeaderValue)) { |
| 120 | + templateValueMatched = true |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if !templateValueMatched { |
| 125 | + return false |
| 126 | + } |
| 127 | + } |
114 | 128 | }
|
115 | 129 | return true
|
116 | 130 | }
|
|
0 commit comments