Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/input/formats/yaml/multidoc_test.go
2852 views
1
package yaml
2
3
import (
4
"os"
5
"strings"
6
"testing"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/input/formats"
9
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
10
"github.com/stretchr/testify/require"
11
)
12
13
func TestYamlFormatterParse(t *testing.T) {
14
format := New()
15
16
proxifyInputFile := "../testdata/ginandjuice.proxify.yaml"
17
18
expectedUrls := []string{
19
"https://ginandjuice.shop/blog/post?postId=3&source=proxify",
20
"https://ginandjuice.shop/users/3",
21
}
22
23
file, err := os.Open(proxifyInputFile)
24
require.Nilf(t, err, "error opening proxify input file: %v", err)
25
defer func() {
26
_ = file.Close()
27
}()
28
29
var urls []string
30
err = format.Parse(file, func(request *types.RequestResponse) bool {
31
urls = append(urls, request.URL.String())
32
return false
33
}, proxifyInputFile)
34
require.Nilf(t, err, "error parsing yaml file: %v", err)
35
require.Len(t, urls, len(expectedUrls), "invalid number of urls")
36
require.ElementsMatch(t, urls, expectedUrls, "invalid urls")
37
}
38
39
func TestYamlFormatterParseWithVariables(t *testing.T) {
40
format := New()
41
proxifyYttFile := "../testdata/ytt/ginandjuice.ytt.yaml"
42
43
expectedUrls := []string{
44
"https://ginandjuice.shop/users/3",
45
}
46
47
format.SetOptions(formats.InputFormatOptions{
48
VarsTextTemplating: true,
49
Variables: map[string]interface{}{
50
"foo": "catalog",
51
"bar": "product",
52
},
53
})
54
file, err := os.Open(proxifyYttFile)
55
require.Nilf(t, err, "error opening proxify ytt input file: %v", err)
56
defer func() {
57
_ = file.Close()
58
}()
59
60
var urls []string
61
err = format.Parse(file, func(request *types.RequestResponse) bool {
62
urls = append(urls, request.URL.String())
63
expectedRaw := `POST /users/3 HTTP/1.1
64
Host: ginandjuice.shop
65
Authorization: Bearer 3x4mpl3t0k3n
66
Accept-Encoding: gzip
67
Content-Type: application/x-www-form-urlencoded
68
Connection: close
69
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
70
71
foo="catalog"&bar=product&debug=false`
72
normalised := strings.ReplaceAll(request.Request.Raw, "\r\n", "\n")
73
require.Equal(t, expectedRaw, strings.TrimSuffix(normalised, "\n"), "request raw does not match expected value")
74
75
return false
76
}, proxifyYttFile)
77
78
require.Nilf(t, err, "error parsing yaml file: %v", err)
79
require.Len(t, urls, len(expectedUrls), "invalid number of urls")
80
require.ElementsMatch(t, urls, expectedUrls, "invalid urls")
81
82
}
83
84