forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_test.go
More file actions
90 lines (69 loc) · 2.77 KB
/
extract_test.go
File metadata and controls
90 lines (69 loc) · 2.77 KB
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
package extractors
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestExtractor_ExtractRegex(t *testing.T) {
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: RegexExtractor}, Regex: []string{`([A-Z])\w+`}}
err := e.CompileExtractors()
require.Nil(t, err)
got := e.ExtractRegex("RegEx")
require.Equal(t, map[string]struct{}{"RegEx": {}}, got)
got = e.ExtractRegex("regex")
require.Equal(t, map[string]struct{}{}, got)
}
func TestExtractor_ExtractKval(t *testing.T) {
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: KValExtractor}, KVal: []string{"content_type"}}
err := e.CompileExtractors()
require.Nil(t, err)
got := e.ExtractKval(map[string]interface{}{"content_type": "text/html"})
require.Equal(t, map[string]struct{}{"text/html": {}}, got)
got = e.ExtractKval(map[string]interface{}{"authorization": "Basic YWxhZGRpbjpvcGVuc2VzYW1l"})
require.Equal(t, map[string]struct{}{}, got)
}
func TestExtractor_ExtractXPath(t *testing.T) {
body := `<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
`
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[2]/a"}}
err := e.CompileExtractors()
require.Nil(t, err)
got := e.ExtractXPath(body)
require.Equal(t, map[string]struct{}{"More information...": {}}, got)
e = &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[3]/a"}}
got = e.ExtractXPath(body)
require.Equal(t, map[string]struct{}{}, got)
}
func TestExtractor_ExtractJSON(t *testing.T) {
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: JSONExtractor}, JSON: []string{".[] | .id"}}
err := e.CompileExtractors()
require.Nil(t, err)
got := e.ExtractJSON(`[{"id": 1}]`)
require.Equal(t, map[string]struct{}{"1": {}}, got)
got = e.ExtractJSON(`{"id": 1}`)
require.Equal(t, map[string]struct{}{}, got)
}
func TestExtractor_ExtractDSL(t *testing.T) {
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: DSLExtractor}, DSL: []string{"to_upper(hello)"}}
err := e.CompileExtractors()
require.Nil(t, err)
got := e.ExtractDSL(map[string]interface{}{"hello": "hi"})
require.Equal(t, map[string]struct{}{"HI": {}}, got)
got = e.ExtractDSL(map[string]interface{}{"hi": "hello"})
require.Equal(t, map[string]struct{}{}, got)
}