-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
210 lines (187 loc) · 5.12 KB
/
validator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Utility to validate HTTP headers of multiple URL based on a JSON spec
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type Spec struct {
Url string
RequestHeaders map[string][]string
ResponseHeaders map[string][]string
}
type SpecContainer struct {
Default Spec
Specs []Spec
}
const (
_ = iota
FileError
UnmarshalError
InvalidRequest
FailedRequest
MissingResponseHeader
FailAssertResponseHeaderValue
)
// In case there is a proxy caching resources. Potentially appending a URL
// parameter would work better.
var NoCacheRequestHeaders = map[string][]string{
"Cache-Control": []string{"no-cache"},
}
var NoCacheClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}
const DEFAULT_SPEC_FILE = "urls.json"
const MANUAL = `Usage: validate-http-headers SPECFILES...
Iterates through SPECFILES, making requests and validating responses headers
Multiple JSON specs can be passed
$ ./validate-http-headers internal.json external.json mtv.json
Non-zero exit codes indicate failure. All failures begin with "FAIL: "; exit
code constants can be found in validator.go.
Simple spec:
{
"default": {
"requestHeaders": {
"Referer": ["https://bad-website.com"]
},
"responseHeaders": {
"X-Frame-Options": ["SAMEORIGIN"]
}
},
"specs": [
{
"url": "https://www.google.com/"
},
{
"url": "https://drive.google.com/"
}
]
}
Full documentation at: <https://github.com/talee/validate-http-headers>`
func main() {
var filenames []string
if len(os.Args) == 1 {
if _, err := os.Stat(DEFAULT_SPEC_FILE); os.IsNotExist(err) {
fmt.Println(MANUAL)
os.Exit(0)
}
filenames = []string{"urls.json"}
} else {
filenames = os.Args[1:]
}
exitCodes := make([]int, 0)
for _, filename := range filenames {
errorCodes := validateSpecFile(filename)
if len(errorCodes) > 0 {
fmt.Println("ERROR CODES:", errorCodes)
exitCodes = append(exitCodes, errorCodes...)
}
}
if len(exitCodes) > 0 {
os.Exit(exitCodes[0])
}
}
func validateSpecFile(filename string) []int {
fmt.Println("\n------------------------------------------------------------------------")
fmt.Println("FILE:", filename)
// Read file
file, e := ioutil.ReadFile(filename)
if e != nil {
fmt.Printf("ERROR: File error: %v\n", e)
return []int{FileError}
}
// Marshal from JSON
var specContainer SpecContainer
e = json.Unmarshal(file, &specContainer)
if e != nil {
fmt.Printf("ERROR: Unmarshal error: %v\n", e)
return []int{UnmarshalError}
}
specs := specContainer.Specs
defaultSpec := specContainer.Default
// Request each URL in specs
errorCodes := make([]int, 0, len(specs))
for _, spec := range specs {
// Setup request headers
req, e := http.NewRequest("GET", spec.Url, nil)
if e != nil {
fmt.Printf("ERROR: Failed to create request: %v\n", e)
errorCodes = append(errorCodes, InvalidRequest)
break
}
requestHeaders := clone(NoCacheRequestHeaders,
defaultSpec.RequestHeaders,
spec.RequestHeaders)
for key, headerValues := range requestHeaders {
for _, val := range headerValues {
req.Header.Add(key, val)
}
}
// Send request
fmt.Println("\nURL:", req.URL)
resp, e := NoCacheClient.Do(req)
if e != nil {
fmt.Printf("ERROR: Failed to request: %v\n", e)
errorCodes = append(errorCodes, FailedRequest)
break
}
resp.Body.Close()
// Validate response headers
expectedHeaders := clone(defaultSpec.ResponseHeaders,
spec.ResponseHeaders)
for key, expectedHeaderValues := range expectedHeaders {
// Assert exist/nonexistence
numResponseHeaderValues := len(resp.Header[key])
expectedNumHeaderValues := len(expectedHeaderValues)
if expectedNumHeaderValues == 1 && expectedHeaderValues[0] == "" {
expectedNumHeaderValues = 0
}
if numResponseHeaderValues != expectedNumHeaderValues {
fmt.Printf("FAIL: Mismatch of existence for response header "+
"'%v': Expected %v response headers with array values"+
"'%v'. Got header values '%v'\n",
key,
expectedNumHeaderValues,
expectedHeaderValues,
resp.Header[key])
errorCodes = append(errorCodes, MissingResponseHeader)
continue
} else if expectedNumHeaderValues == 0 {
fmt.Println("SUCCESS: Response header '"+key+"' should not",
"exist")
continue
}
// Assert equality
for i, expectedHeaderValue := range expectedHeaderValues {
respHeaderValue := resp.Header[key][i]
if expectedHeaderValue != respHeaderValue {
fmt.Printf("Header assertion failed: Expected '%v'"+
"to have '%v' instead of '%v'\n", key,
expectedHeaderValue, respHeaderValue)
errorCodes = append(errorCodes, FailAssertResponseHeaderValue)
} else {
fmt.Println("SUCCESS: Expected", "'"+key+"'",
"to have", "'"+expectedHeaderValue+"'", "match",
"'"+respHeaderValue+"'")
}
}
}
}
return errorCodes
}
// Returns a merge of the maps. Nil values ignored
func clone(maps ...map[string][]string) map[string][]string {
result := make(map[string][]string)
for _, m := range maps {
for key, values := range m {
if values != nil {
result[key] = values
}
}
}
return result
}