github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/utils/http/requtils_test.go (about)

     1  package httputil
     2  
     3  import (
     4  	"testing"
     5  
     6  	urlutil "github.com/projectdiscovery/utils/url"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestTrailingSlash(t *testing.T) {
    11  	testcases := []struct {
    12  		payload  string
    13  		hasSlash bool
    14  	}{
    15  		{"{{BaseURL}}", false},
    16  		{"{{BaseURL}}/", true},
    17  		{"{{RootURL}}", false},
    18  		{"{{RootURL}}/", true},
    19  		{"{{randomvar}}", false},
    20  		{"{{randomvar}}/", true},
    21  		{"later/{{randomvar}}/", false},
    22  	}
    23  
    24  	for _, v := range testcases {
    25  		if v.hasSlash != HasTrailingSlash(v.payload) {
    26  			t.Errorf("expected %v but got %v for %v", v.hasSlash, HasTrailingSlash(v.payload), v.payload)
    27  		}
    28  	}
    29  }
    30  
    31  func TestPortUpdate(t *testing.T) {
    32  	testcases := []struct {
    33  		inputURL        string // input url
    34  		CleanedInputURL string
    35  		RequestPath     string // path which contains port
    36  		CleanedPath     string // path after processing
    37  	}{
    38  		{"http://localhost:53/test", "http://localhost:8000/test", "{{BaseURL}}:8000/newpath", "{{BaseURL}}/newpath"},
    39  		{"http://localhost:53/test", "http://localhost:8000/test", "{{RootURL}}:8000/newpath", "{{RootURL}}/newpath"},
    40  		{"http://localhost:53/test", "http://localhost:53/test", "{{RootURL}}/newpath", "{{RootURL}}/newpath"},
    41  		{"http://localhost/test", "http://localhost:8000/test", "{{RootURL}}:8000/newpath", "{{RootURL}}/newpath"},
    42  		{"http://localhost/test", "http://localhost/test", "{{RootURL}}/newpath", "{{RootURL}}/newpath"},
    43  	}
    44  	for _, v := range testcases {
    45  		parsed, _ := urlutil.Parse(v.inputURL)
    46  		parsed, v.RequestPath = UpdateURLPortFromPayload(parsed, v.RequestPath)
    47  		require.Equal(t, v.CleanedInputURL, parsed.String(), "could not get correct value")
    48  		require.Equal(t, v.CleanedPath, v.RequestPath, "could not get correct data")
    49  	}
    50  }