2024-03-14 03:08:53 +05:30
|
|
|
package component
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestURLComponent(t *testing.T) {
|
|
|
|
|
req, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com/testpath", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
urlComponent := NewPath()
|
|
|
|
|
_, err = urlComponent.Parse(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var keys []string
|
|
|
|
|
var values []string
|
|
|
|
|
_ = urlComponent.Iterate(func(key string, value interface{}) error {
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
|
values = append(values, value.(string))
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
2024-06-11 04:43:46 +05:30
|
|
|
require.Equal(t, []string{"1"}, keys, "unexpected keys")
|
2024-03-14 03:08:53 +05:30
|
|
|
require.Equal(t, []string{"/testpath"}, values, "unexpected values")
|
|
|
|
|
|
2024-06-11 04:43:46 +05:30
|
|
|
err = urlComponent.SetValue("1", "/newpath")
|
2024-03-14 03:08:53 +05:30
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rebuilt, err := urlComponent.Rebuild()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
require.Equal(t, "/newpath", rebuilt.URL.Path, "unexpected URL path")
|
|
|
|
|
require.Equal(t, "https://example.com/newpath", rebuilt.URL.String(), "unexpected full URL")
|
|
|
|
|
}
|
2024-06-11 04:43:46 +05:30
|
|
|
|
|
|
|
|
func TestURLComponent_NestedPaths(t *testing.T) {
|
|
|
|
|
path := NewPath()
|
|
|
|
|
req, err := retryablehttp.NewRequest(http.MethodGet, "https://example.com/user/753/profile", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
found, err := path.Parse(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
t.Fatal("expected path to be found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isSet := false
|
|
|
|
|
|
|
|
|
|
_ = path.Iterate(func(key string, value interface{}) error {
|
|
|
|
|
if !isSet && value.(string) == "/user/753" {
|
|
|
|
|
isSet = true
|
|
|
|
|
if setErr := path.SetValue(key, "/user/753'"); setErr != nil {
|
|
|
|
|
t.Fatal(setErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
newReq, err := path.Rebuild()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if newReq.URL.Path != "/user/753'/profile" {
|
|
|
|
|
t.Fatal("expected path to be modified")
|
|
|
|
|
}
|
|
|
|
|
}
|