url-parser/command/commands_test.go

65 lines
1.6 KiB
Go
Raw Normal View History

2020-09-21 18:24:51 +00:00
package command
2020-02-03 11:44:19 +00:00
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/thegeeklab/url-parser/config"
)
2020-02-03 11:44:19 +00:00
func TestParse(t *testing.T) {
//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"
tests := []struct {
name string
config *config.Config
expected *URL
}{
2020-02-03 12:45:03 +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
},
}
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
}
}