forked from wjdp/htmltest
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
181 lines (151 loc) · 4.49 KB
/
options.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
package htmltest
import (
"fmt"
"github.com/imdario/mergo"
"github.com/wjdp/htmltest/issues"
"path"
"reflect"
"regexp"
"strings"
)
// Options struct for htmltest, user and default options are merged and mapped
// into an instance of this struct.
type Options struct {
DirectoryPath string
DirectoryIndex string
FilePath string
FileExtension string
CheckDoctype bool
CheckAnchors bool
CheckLinks bool
CheckImages bool
CheckScripts bool
CheckMeta bool
CheckGeneric bool
CheckExternal bool
CheckInternal bool
CheckInternalHash bool
CheckMailto bool
CheckTel bool
CheckFavicon bool
CheckMetaRefresh bool
EnforceHTML5 bool
EnforceHTTPS bool
IgnoreURLs []interface{}
IgnoreDirs []interface{}
IgnoreInternalEmptyHash bool
IgnoreEmptyHref bool
IgnoreCanonicalBrokenLinks bool
IgnoreAltMissing bool
IgnoreDirectoryMissingTrailingSlash bool
IgnoreSSLVerify bool
IgnoreTagAttribute string
HTTPHeaders map[interface{}]interface{}
TestFilesConcurrently bool
DocumentConcurrencyLimit int
HTTPConcurrencyLimit int
LogLevel int
LogSort string
ExternalTimeout int
StripQueryString bool
StripQueryExcludes []string
EnableCache bool
EnableLog bool
OutputDir string
OutputCacheFile string
OutputLogFile string
CacheExpires string // Accepts golang time period strings, hours (16h) is really only useful option
// --- Internals below here ---
NoRun bool // When true does not run tests, used to inspect state in unit tests
VCREnable bool // When true patches the govcr httpClient to mock network calls
Version string // Instigator should set this to a version string
}
// DefaultOptions returns a map of default options.
func DefaultOptions() map[string]interface{} {
// Specify defaults here
return map[string]interface{}{
"DirectoryIndex": "index.html",
"FileExtension": ".html",
"CheckDoctype": true,
"CheckAnchors": true,
"CheckLinks": true,
"CheckImages": true,
"CheckScripts": true,
"CheckMeta": true,
"CheckGeneric": true,
"CheckExternal": true,
"CheckInternal": true,
"CheckInternalHash": true,
"CheckMailto": true,
"CheckTel": true,
"CheckFavicon": false,
"CheckMetaRefresh": true,
"EnforceHTML5": false,
"EnforceHTTPS": false,
"IgnoreURLs": []interface{}{},
"IgnoreDirs": []interface{}{},
"IgnoreInternalEmptyHash": false,
"IgnoreEmptyHref": false,
"IgnoreCanonicalBrokenLinks": true,
"IgnoreAltMissing": false,
"IgnoreDirectoryMissingTrailingSlash": false,
"IgnoreSSLVerify": false,
"IgnoreTagAttribute": "data-proofer-ignore",
"HTTPHeaders": map[string]string{
"Range": "bytes=0-0", // If server supports prevents body being sent
"Accept": "*/*", // We accept all content types
},
"TestFilesConcurrently": false,
"DocumentConcurrencyLimit": 128,
"HTTPConcurrencyLimit": 16,
"LogLevel": issues.LevelWarning,
"LogSort": "document",
"ExternalTimeout": 15,
"StripQueryString": true,
"StripQueryExcludes": []string{"fonts.googleapis.com"},
"EnableCache": true,
"EnableLog": true,
"OutputDir": path.Join("tmp", ".htmltest"),
"OutputCacheFile": "refcache.json",
"OutputLogFile": "htmltest.log",
"CacheExpires": "336h",
"NoRun": false,
"VCREnable": false,
"Version": "dev",
}
}
func (hT *HTMLTest) setOptions(optsUser map[string]interface{}) {
// Merge user and default options, set Opts var
optsMap := DefaultOptions()
mergo.MergeWithOverwrite(&optsMap, optsUser)
hT.opts = Options{}
mergo.MapWithOverwrite(&hT.opts, optsMap)
// If debug dump the options struct
if hT.opts.LogLevel == issues.LevelDebug {
s := reflect.ValueOf(&hT.opts).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
fmt.Printf("%d: %s %s = %v\n", i,
typeOfT.Field(i).Name, f.Type(), f.Interface())
}
}
}
// InList tests if key is in a slice/list.
func InList(list []string, key string) bool {
for _, item := range list {
if strings.Contains(key, item) {
return true
}
}
return false
}
// Is the given URL ignored by the current configuration
func (opts *Options) isURLIgnored(url string) bool {
for _, item := range opts.IgnoreURLs {
if ok, _ := regexp.MatchString(item.(string), url); ok {
return true
}
}
return false
}