2014-08-22 07:59:59 -04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func TestFixURL(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
defer viper.Reset()
|
|
|
|
|
2014-08-22 07:59:59 -04:00
|
|
|
type data struct {
|
|
|
|
TestName string
|
2015-03-11 13:34:57 -04:00
|
|
|
CLIBaseURL string
|
|
|
|
CfgBaseURL string
|
2014-08-22 07:59:59 -04:00
|
|
|
AppendPort bool
|
|
|
|
Port int
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
tests := []data{
|
2015-01-16 05:13:03 -05:00
|
|
|
{"Basic http localhost", "", "http://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic https production, http localhost", "", "https://foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Basic subdir", "", "http://foo.com/bar", true, 1313, "http://localhost:1313/bar/"},
|
|
|
|
{"Basic production", "http://foo.com", "http://foo.com", false, 80, "http://foo.com/"},
|
|
|
|
{"Production subdir", "http://foo.com/bar", "http://foo.com/bar", false, 80, "http://foo.com/bar/"},
|
|
|
|
{"No http", "", "foo.com", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"Override configured port", "", "foo.com:2020", true, 1313, "http://localhost:1313/"},
|
|
|
|
{"No http production", "foo.com", "foo.com", false, 80, "http://foo.com/"},
|
|
|
|
{"No http production with port", "foo.com", "foo.com", true, 2020, "http://foo.com:2020/"},
|
2014-08-22 07:59:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
2015-03-11 13:34:57 -04:00
|
|
|
BaseURL = test.CLIBaseURL
|
|
|
|
viper.Set("BaseURL", test.CfgBaseURL)
|
2014-08-22 07:59:59 -04:00
|
|
|
serverAppend = test.AppendPort
|
|
|
|
serverPort = test.Port
|
2015-03-11 13:34:57 -04:00
|
|
|
result, err := fixURL(BaseURL)
|
2014-08-22 07:59:59 -04:00
|
|
|
if err != nil {
|
2015-03-06 08:56:44 -05:00
|
|
|
t.Errorf("Test #%d %s: unexpected error %s", i, test.TestName, err)
|
2014-08-22 07:59:59 -04:00
|
|
|
}
|
|
|
|
if result != test.Result {
|
|
|
|
t.Errorf("Test #%d %s: expected %q, got %q", i, test.TestName, test.Result, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|