Exemple #1
0
func TestHostParser(t *testing.T) {
	var tests = []struct {
		host string
		url  string
		err  string
	}{
		{"", "", "empty host"},
		{":80", "", "empty host"},
		{"localhost", "http://localhost/server-status?auto=", ""},
		{"localhost/ServerStatus", "http://localhost/ServerStatus?auto=", ""},
		{"127.0.0.1", "http://127.0.0.1/server-status?auto=", ""},
		{"https://127.0.0.1", "https://127.0.0.1/server-status?auto=", ""},
		{"[2001:db8:0:1]:80", "http://[2001:db8:0:1]:80/server-status?auto=", ""},
		{"https://*****:*****@127.0.0.1", "https://*****:*****@127.0.0.1/server-status?auto=", ""},
	}

	for _, test := range tests {
		hostData, err := hostParser(mbtest.NewTestModule(t, map[string]interface{}{}), test.host)
		if err != nil && test.err != "" {
			assert.Contains(t, err.Error(), test.err)
		} else if assert.NoError(t, err, "unexpected error") {
			assert.Equal(t, test.url, hostData.URI)
		}
	}
}
Exemple #2
0
func TestURLHostParserBuilder(t *testing.T) {
	const rawURL = "http://example.com"

	var cases = []struct {
		config  map[string]interface{}
		builder URLHostParserBuilder
		url     string
	}{
		{map[string]interface{}{"path": "/path"}, URLHostParserBuilder{PathConfigKey: "path", DefaultPath: "/default"}, "http://example.com/path"},
		{map[string]interface{}{}, URLHostParserBuilder{PathConfigKey: "path", DefaultPath: "/default"}, "http://example.com/default"},
		{map[string]interface{}{}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/default"},
		{map[string]interface{}{"username": "******"}, URLHostParserBuilder{}, "http://[email protected]"},
		{map[string]interface{}{"username": "******", "password": "******"}, URLHostParserBuilder{}, "http://*****:*****@example.com"},
	}

	for _, test := range cases {
		m := mbtest.NewTestModule(t, test.config)
		hostParser := test.builder.Build()

		hp, err := hostParser(m, rawURL)
		if err != nil {
			t.Fatal(err)
		}

		assert.Equal(t, test.url, hp.URI)
	}
}
Exemple #3
0
func TestHostParser(t *testing.T) {
	tests := []struct {
		host, expected string
	}{
		{"localhost", "tcp://localhost"},
		{"localhost:123", "tcp://localhost:123"},
		{"tcp://localhost:123", "tcp://localhost:123"},
		{"unix:///var/lib/haproxy/stats", "unix:///var/lib/haproxy/stats"},
	}

	m := mbtest.NewTestModule(t, map[string]interface{}{})

	for _, test := range tests {
		hi, err := HostParser(m, test.host)
		if err != nil {
			t.Error("failed on", test.host, err)
			continue
		}
		assert.Equal(t, test.expected, hi.URI)
	}
}
Exemple #4
0
func TestParseDSN(t *testing.T) {
	const query = "?readTimeout=10s&timeout=10s&writeTimeout=10s"

	var tests = []struct {
		host     string
		username string
		password string
		uri      string
	}{
		{"", "", "", "tcp(127.0.0.1:3306)/" + query},
		{"", "root", "secret", "root:secret@tcp(127.0.0.1:3306)/" + query},
		{"unix(/tmp/mysql.sock)/", "root", "", "root@unix(/tmp/mysql.sock)/" + query},
		{"tcp(127.0.0.1:3306)/", "", "", "tcp(127.0.0.1:3306)/" + query},
		{"tcp(127.0.0.1:3306)/", "root", "", "root@tcp(127.0.0.1:3306)/" + query},
		{"tcp(127.0.0.1:3306)/", "root", "secret", "root:secret@tcp(127.0.0.1:3306)/" + query},
	}

	for _, test := range tests {
		c := map[string]interface{}{
			"username": test.username,
			"password": test.password,
		}
		mod := mbtest.NewTestModule(t, c)
		mod.ModConfig.Timeout = 10 * time.Second

		hostData, err := ParseDSN(mod, test.host)
		if err != nil {
			t.Error(err)
			continue
		}

		assert.Equal(t, test.uri, hostData.URI)
		if test.username != "" {
			assert.NotContains(t, hostData.SanitizedURI, test.username)
		}
		if test.password != "" {
			assert.NotContains(t, hostData.SanitizedURI, test.password)
		}
	}
}
Exemple #5
0
func TestParseMongoURL(t *testing.T) {
	tests := []struct {
		Name             string
		URL              string
		Username         string
		Password         string
		ExpectedAddr     string
		ExpectedUsername string
		ExpectedPassword string
	}{
		{
			Name:     "basic test",
			URL:      "localhost:40001",
			Username: "******",
			Password: "******",

			ExpectedAddr:     "localhost:40001",
			ExpectedUsername: "******",
			ExpectedPassword: "******",
		},
		{
			Name:     "with schema",
			URL:      "mongodb://*****:*****@localhost:40001",
			Username: "",
			Password: "",

			ExpectedAddr:     "localhost:40001",
			ExpectedUsername: "******",
			ExpectedPassword: "******",
		},
		{
			Name:     "username and password do not overwride",
			URL:      "mongodb://*****:*****@localhost:40001",
			Username: "******",
			Password: "******",

			ExpectedAddr:     "localhost:40001",
			ExpectedUsername: "******",
			ExpectedPassword: "******",
		},
		{
			Name:     "with options",
			URL:      "mongodb://localhost:40001?connect=direct&authSource=me",
			Username: "******",
			Password: "******",

			ExpectedAddr:     "localhost:40001",
			ExpectedUsername: "******",
			ExpectedPassword: "******",
		},
		{
			Name:     "multiple hosts",
			URL:      "mongodb://localhost:40001,localhost:40002",
			Username: "",
			Password: "",

			ExpectedAddr:     "localhost:40001,localhost:40002",
			ExpectedUsername: "",
			ExpectedPassword: "",
		},
	}

	for _, test := range tests {
		mod := mbtest.NewTestModule(t, map[string]interface{}{
			"username": test.Username,
			"password": test.Password,
		})
		hostData, err := ParseURL(mod, test.URL)
		if err != nil {
			t.Error(err)
			continue
		}

		assert.Equal(t, test.ExpectedAddr, hostData.Host, test.Name)
		assert.Equal(t, test.ExpectedUsername, hostData.User, test.Name)
		assert.Equal(t, test.ExpectedPassword, hostData.Password, test.Name)
	}
}
Exemple #6
0
func TestParseUrl(t *testing.T) {
	tests := []struct {
		Name     string
		URL      string
		Username string
		Password string
		Timeout  time.Duration
		Expected string
	}{
		{
			Name:     "simple test",
			URL:      "postgres://*****:*****@host1:5432",
			Expected: "host=host1 password=pass port=5432 user=user",
		},
		{
			Name:     "user/pass in params",
			URL:      "postgres://*****:*****@host1:5432",
			Username: "******",
			Password: "******",
			Expected: "host=host1 password=pass port=5432 user=user1",
		},
		{
			Name:     "timeout no override",
			URL:      "postgres://*****:*****@host1:5432?connect_timeout=2",
			Username: "******",
			Password: "******",
			Timeout:  3 * time.Second,
			Expected: "connect_timeout=3 host=host1 password=pass port=5432 user=user1",
		},
		{
			Name:     "unix socket",
			URL:      "postgresql:///dbname?host=/var/lib/postgresql",
			Expected: "dbname=dbname host=/var/lib/postgresql",
		},
	}

	for _, test := range tests {
		mod := mbtest.NewTestModule(t, map[string]interface{}{
			"username": test.Username,
			"password": test.Password,
		})
		mod.ModConfig.Timeout = test.Timeout
		hostData, err := ParseURL(mod, test.URL)
		if err != nil {
			t.Error(err)
			continue
		}

		assert.Equal(t, test.Expected, hostData.URI, test.Name)
	}
}