Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/input/formats/openapi/openapi_test.go
2070 views
1
package openapi
2
3
import (
4
"os"
5
"strings"
6
"testing"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
9
"github.com/stretchr/testify/require"
10
)
11
12
const baseURL = "http://hackthebox:5000"
13
14
var methodToURLs = map[string][]string{
15
"GET": {
16
"{{baseUrl}}/createdb",
17
"{{baseUrl}}/",
18
"{{baseUrl}}/users/v1/John.Doe",
19
"{{baseUrl}}/users/v1",
20
"{{baseUrl}}/users/v1/_debug",
21
"{{baseUrl}}/books/v1",
22
"{{baseUrl}}/books/v1/bookTitle77",
23
},
24
"POST": {
25
"{{baseUrl}}/users/v1/register",
26
"{{baseUrl}}/users/v1/login",
27
"{{baseUrl}}/books/v1",
28
},
29
"PUT": {
30
"{{baseUrl}}/users/v1/name1/email",
31
"{{baseUrl}}/users/v1/name1/password",
32
},
33
"DELETE": {
34
"{{baseUrl}}/users/v1/name1",
35
},
36
}
37
38
func TestOpenAPIParser(t *testing.T) {
39
format := New()
40
41
proxifyInputFile := "../testdata/openapi.yaml"
42
43
gotMethodsToURLs := make(map[string][]string)
44
45
file, err := os.Open(proxifyInputFile)
46
require.Nilf(t, err, "error opening proxify input file: %v", err)
47
defer func() {
48
_ = file.Close()
49
}()
50
51
err = format.Parse(file, func(rr *types.RequestResponse) bool {
52
gotMethodsToURLs[rr.Request.Method] = append(gotMethodsToURLs[rr.Request.Method],
53
strings.Replace(rr.URL.String(), baseURL, "{{baseUrl}}", 1))
54
return false
55
}, proxifyInputFile)
56
if err != nil {
57
t.Fatal(err)
58
}
59
60
if len(gotMethodsToURLs) != len(methodToURLs) {
61
t.Fatalf("invalid number of methods: %d", len(gotMethodsToURLs))
62
}
63
64
for method, urls := range gotMethodsToURLs {
65
if len(urls) != len(methodToURLs[method]) {
66
t.Fatalf("invalid number of urls for method %s: %d", method, len(urls))
67
}
68
require.ElementsMatch(t, urls, methodToURLs[method], "invalid urls for method %s", method)
69
}
70
}
71
72