Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/operators/extractors/extract_test.go
2070 views
1
package extractors
2
3
import (
4
"testing"
5
6
"github.com/stretchr/testify/require"
7
)
8
9
func TestExtractor_ExtractRegex(t *testing.T) {
10
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: RegexExtractor}, Regex: []string{`([A-Z])\w+`}}
11
err := e.CompileExtractors()
12
require.Nil(t, err)
13
14
got := e.ExtractRegex("RegEx")
15
require.Equal(t, map[string]struct{}{"RegEx": {}}, got)
16
17
got = e.ExtractRegex("regex")
18
require.Equal(t, map[string]struct{}{}, got)
19
}
20
21
func TestExtractor_ExtractKval(t *testing.T) {
22
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: KValExtractor}, KVal: []string{"content_type"}}
23
err := e.CompileExtractors()
24
require.Nil(t, err)
25
26
got := e.ExtractKval(map[string]interface{}{"content_type": "text/html"})
27
require.Equal(t, map[string]struct{}{"text/html": {}}, got)
28
29
got = e.ExtractKval(map[string]interface{}{"authorization": "Basic YWxhZGRpbjpvcGVuc2VzYW1l"})
30
require.Equal(t, map[string]struct{}{}, got)
31
32
}
33
34
func TestExtractor_ExtractXPath(t *testing.T) {
35
body := `<!doctype html>
36
<html>
37
<head>
38
<title>Example Domain</title>
39
40
<meta charset="utf-8" />
41
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
42
<meta name="viewport" content="width=device-width, initial-scale=1" />
43
</head>
44
45
<body>
46
<div>
47
<h1>Example Domain</h1>
48
<p>This domain is for use in illustrative examples in documents. You may use this
49
domain in literature without prior coordination or asking for permission.</p>
50
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
51
</div>
52
</body>
53
</html>
54
`
55
56
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[2]/a"}}
57
err := e.CompileExtractors()
58
require.Nil(t, err)
59
60
got := e.ExtractXPath(body)
61
require.Equal(t, map[string]struct{}{"More information...": {}}, got)
62
63
e = &Extractor{Type: ExtractorTypeHolder{ExtractorType: XPathExtractor}, XPath: []string{"/html/body/div/p[3]/a"}}
64
got = e.ExtractXPath(body)
65
require.Equal(t, map[string]struct{}{}, got)
66
}
67
68
func TestExtractor_ExtractJSON(t *testing.T) {
69
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: JSONExtractor}, JSON: []string{".[] | .id"}}
70
err := e.CompileExtractors()
71
require.Nil(t, err)
72
73
got := e.ExtractJSON(`[{"id": 1}]`)
74
require.Equal(t, map[string]struct{}{"1": {}}, got)
75
76
got = e.ExtractJSON(`{"id": 1}`)
77
require.Equal(t, map[string]struct{}{}, got)
78
}
79
80
func TestExtractor_ExtractDSL(t *testing.T) {
81
e := &Extractor{Type: ExtractorTypeHolder{ExtractorType: DSLExtractor}, DSL: []string{"to_upper(hello)"}}
82
err := e.CompileExtractors()
83
require.Nil(t, err)
84
85
got := e.ExtractDSL(map[string]interface{}{"hello": "hi"})
86
require.Equal(t, map[string]struct{}{"HI": {}}, got)
87
88
got = e.ExtractDSL(map[string]interface{}{"hi": "hello"})
89
require.Equal(t, map[string]struct{}{}, got)
90
}
91
92