-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
61 lines (52 loc) · 1.33 KB
/
header.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
package bacom
import (
"net/http"
"path"
"github.com/fatih/color"
)
// CompareHeaders returns a list of differences between two http.Header.
// ignore and ignoreContent are expected to be normalized http headers names.
func CompareHeaders(ignore, ignoreContent []string, lhs, rhs http.Header) ([]string, error) {
var results []string
for k := range lhs {
if ok, err := containsPattern(ignore, k); err != nil {
return results, err
} else if ok {
continue
}
if _, ok := rhs[k]; !ok {
results = append(results, missingHeader(k, lhs.Get(k)))
continue
}
if ok, err := containsPattern(ignoreContent, k); err != nil {
return results, err
} else if ok {
continue
}
if lhs.Get(k) != rhs.Get(k) {
results = append(results,
missingHeader(k, lhs.Get(k)),
excessHeader(k, rhs.Get(k)),
)
}
}
return results, nil
}
func missingHeader(name, value string) string {
return "- (Header) " + name + ": " + color.RedString("%s", value)
}
func excessHeader(name, value string) string {
return "+ (Header) " + name + ": " + color.GreenString("%s", value)
}
func containsPattern(patterns []string, needle string) (bool, error) {
for _, pattern := range patterns {
match, err := path.Match(pattern, needle)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}