2020-09-21 18:24:51 +00:00
|
|
|
package command
|
2020-02-03 11:44:19 +00:00
|
|
|
|
2023-07-19 13:56:43 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-11-05 13:35:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-19 13:56:43 +00:00
|
|
|
"github.com/thegeeklab/url-parser/config"
|
|
|
|
)
|
2020-02-03 11:44:19 +00:00
|
|
|
|
2024-11-05 13:35:26 +00:00
|
|
|
func TestParse(t *testing.T) {
|
2023-02-08 09:13:55 +00:00
|
|
|
//nolint:goconst
|
2020-02-03 11:44:19 +00:00
|
|
|
urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment"
|
|
|
|
|
2024-11-05 13:35:26 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
config *config.Config
|
|
|
|
expected *URL
|
|
|
|
}{
|
2020-02-03 12:45:03 +00:00
|
|
|
{
|
2024-11-05 13:35:26 +00:00
|
|
|
name: "parse url",
|
|
|
|
config: &config.Config{
|
|
|
|
URL: urlString,
|
|
|
|
QuerySplit: true,
|
|
|
|
},
|
|
|
|
expected: &URL{
|
|
|
|
Scheme: "postgres",
|
|
|
|
Username: "user",
|
|
|
|
Password: "pass",
|
|
|
|
Hostname: "host.com",
|
|
|
|
Port: "5432",
|
|
|
|
Path: "/path/to",
|
|
|
|
Query: "key=value&other=other%20value",
|
|
|
|
RawQuery: "key=value&other=other%20value",
|
|
|
|
QueryParams: []QueryParam{
|
|
|
|
{
|
|
|
|
Key: "key",
|
|
|
|
Value: "value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "other",
|
|
|
|
Value: "other value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Fragment: "some-fragment",
|
|
|
|
},
|
2020-02-03 11:44:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-11-05 13:35:26 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
result := NewURLParser(urlString, "", false).parse()
|
|
|
|
assert.Equal(t, tt.expected.Scheme, result.Scheme)
|
|
|
|
assert.Equal(t, tt.expected.Username, result.Username)
|
|
|
|
assert.Equal(t, tt.expected.Password, result.Password)
|
|
|
|
assert.Equal(t, tt.expected.Hostname, result.Hostname)
|
|
|
|
assert.Equal(t, tt.expected.Port, result.Port)
|
|
|
|
assert.Equal(t, tt.expected.Path, result.Path)
|
|
|
|
assert.Equal(t, tt.expected.Fragment, result.Fragment)
|
|
|
|
assert.Equal(t, tt.expected.RawQuery, result.RawQuery)
|
|
|
|
assert.Equal(t, tt.expected.Query, result.Query)
|
|
|
|
assert.ElementsMatch(t, tt.expected.QueryParams, result.QueryParams)
|
|
|
|
})
|
2020-02-03 11:44:19 +00:00
|
|
|
}
|
|
|
|
}
|