url-parser/command/run_test.go

89 lines
2.0 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 (
"encoding/json"
2020-02-03 11:44:19 +00:00
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/thegeeklab/url-parser/config"
2020-02-03 11:44:19 +00:00
"github.com/urfave/cli/v2"
"github.com/zenizh/go-capturer"
2020-02-03 11:44:19 +00:00
)
func TestRun(t *testing.T) {
urlString := "postgres://user:pass@host.com:5432/path/to?key=value&other=other%20value#some-fragment"
tests := []struct {
name string
config *config.Config
expected string
}{
2020-02-03 12:45:03 +00:00
{
name: "get url",
config: &config.Config{URL: urlString},
expected: urlString,
2020-02-03 11:44:19 +00:00
},
{
name: "get url with query split",
config: &config.Config{
URL: urlString,
QuerySplit: true,
JSONOutput: true,
},
expected: `{
"scheme": "postgres",
"hostname": "host.com",
"port": "5432",
"path": "/path/to",
"fragment": "some-fragment",
"rawQuery": "key=value&other=other%20value",
"queryParams": [
{
"key": "key",
"value": "value"
},
{
"key": "other",
"value": "other value"
}
],
"username": "user",
"password": "pass"
}`,
},
2020-02-03 11:44:19 +00:00
}
for _, tt := range tests {
2020-02-03 11:44:19 +00:00
app := cli.NewApp()
ctx := cli.NewContext(app, nil, nil)
2020-02-03 11:44:19 +00:00
t.Run(tt.name, func(t *testing.T) {
result := strings.TrimSpace(capturer.CaptureStdout(func() { _ = Run(tt.config)(ctx) }))
if tt.config.JSONOutput {
got := &URL{}
expected := &URL{}
_ = json.Unmarshal([]byte(result), &got)
_ = json.Unmarshal([]byte(tt.expected), &expected)
assert.Equal(t, expected.Scheme, got.Scheme)
assert.Equal(t, expected.Username, got.Username)
assert.Equal(t, expected.Password, got.Password)
assert.Equal(t, expected.Hostname, got.Hostname)
assert.Equal(t, expected.Port, got.Port)
assert.Equal(t, expected.Path, got.Path)
assert.Equal(t, expected.Fragment, got.Fragment)
assert.Equal(t, expected.RawQuery, got.RawQuery)
assert.Equal(t, expected.Query, got.Query)
assert.ElementsMatch(t, expected.QueryParams, got.QueryParams)
return
}
2020-02-03 11:44:19 +00:00
assert.Equal(t, tt.expected, result)
})
2020-02-03 11:44:19 +00:00
}
}