nuclei/pkg/protocols/utils/http/requtils_test.go
Tarun Koyalwar dc44105baf
nuclei v3 : misc updates (#4247)
* use parsed options while signing

* update project layout to v3

* fix .gitignore

* remove example template

* misc updates

* bump tlsx version

* hide template sig warning with env

* js: retain value while using log

* fix nil pointer derefernce

* misc doc update

---------

Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-10-17 17:44:13 +05:30

51 lines
1.7 KiB
Go

package httputil
import (
"testing"
urlutil "github.com/projectdiscovery/utils/url"
"github.com/stretchr/testify/require"
)
func TestTrailingSlash(t *testing.T) {
testcases := []struct {
payload string
hasSlash bool
}{
{"{{BaseURL}}", false},
{"{{BaseURL}}/", true},
{"{{RootURL}}", false},
{"{{RootURL}}/", true},
{"{{randomvar}}", false},
{"{{randomvar}}/", true},
{"later/{{randomvar}}/", false},
}
for _, v := range testcases {
if v.hasSlash != HasTrailingSlash(v.payload) {
t.Errorf("expected %v but got %v for %v", v.hasSlash, HasTrailingSlash(v.payload), v.payload)
}
}
}
func TestPortUpdate(t *testing.T) {
testcases := []struct {
inputURL string // input url
CleanedInputURL string
RequestPath string // path which contains port
CleanedPath string // path after processing
}{
{"http://localhost:53/test", "http://localhost:8000/test", "{{BaseURL}}:8000/newpath", "{{BaseURL}}/newpath"},
{"http://localhost:53/test", "http://localhost:8000/test", "{{RootURL}}:8000/newpath", "{{RootURL}}/newpath"},
{"http://localhost:53/test", "http://localhost:53/test", "{{RootURL}}/newpath", "{{RootURL}}/newpath"},
{"http://localhost/test", "http://localhost:8000/test", "{{RootURL}}:8000/newpath", "{{RootURL}}/newpath"},
{"http://localhost/test", "http://localhost/test", "{{RootURL}}/newpath", "{{RootURL}}/newpath"},
}
for _, v := range testcases {
parsed, _ := urlutil.Parse(v.inputURL)
parsed, v.RequestPath = UpdateURLPortFromPayload(parsed, v.RequestPath)
require.Equal(t, v.CleanedInputURL, parsed.String(), "could not get correct value")
require.Equal(t, v.CleanedPath, v.RequestPath, "could not get correct data")
}
}