Skip to content

Commit

Permalink
Merge pull request #74 from yunify/fix-newwithendpoint
Browse files Browse the repository at this point in the history
update test case , fix newwithendpoint
  • Loading branch information
runzexia authored Dec 13, 2017
2 parents 19b51a6 + 390b85c commit 60c8f19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func NewWithEndpoint(accessKeyID, secretAccessKey, endpoint string) (*Config, er
if err != nil {
return nil, err
}
if qcURL.Opaque != ""{
return nil , fmt.Errorf("wrong URL format")
}

if !strings.Contains(qcURL.Host, ":") {
if qcURL.Scheme == "https" {
Expand All @@ -87,12 +90,13 @@ func NewWithEndpoint(accessKeyID, secretAccessKey, endpoint string) (*Config, er
return nil, err
}

_, port, err := net.SplitHostPort(qcURL.Host)
host, port, err := net.SplitHostPort(qcURL.Host)
if err != nil {
return nil, err
}

config.Port, _ = strconv.Atoi(port)
config.Host = host
config.AccessKeyID = accessKeyID
config.SecretAccessKey = secretAccessKey
config.Protocol = qcURL.Scheme
Expand Down
19 changes: 17 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,28 @@ func TestNew(t *testing.T) {
}

func TestNewWithEndpoint(t *testing.T) {
config, err := NewWithEndpoint("AccessKeyID", "SecretAccessKey", "https://api.qingcloud.com/iaas")
assert.Nil(t, err)
config, err := NewWithEndpoint("AccessKeyID", "SecretAccessKey", "test.qingcloud.com:444/iaas?a=1#hhh")
assert.NotNil(t, err)

config, err = NewWithEndpoint("AccessKeyID", "SecretAccessKey", "http:test.qingcloud.com:444/iaas?a=1#hhh")
assert.NotNil(t, err)

config, err = NewWithEndpoint("AccessKeyID", "SecretAccessKey", "https://api.qingcloud.com/iaas")
assert.Nil(t, err)
assert.Equal(t, "AccessKeyID", config.AccessKeyID)
assert.Equal(t, "SecretAccessKey", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "api.qingcloud.com", config.Host)
assert.Equal(t, 443, config.Port)
assert.Equal(t, "/iaas", config.URI)

config, err = NewWithEndpoint("AccessKeyID", "SecretAccessKey", "http://test.qingcloud.com:444/iaas")
assert.Nil(t, err)
assert.Equal(t, "AccessKeyID", config.AccessKeyID)
assert.Equal(t, "SecretAccessKey", config.SecretAccessKey)
assert.Equal(t, "http", config.Protocol)
assert.Equal(t, "test.qingcloud.com", config.Host)
assert.Equal(t, 444, config.Port)
assert.Equal(t, "/iaas", config.URI)

}

0 comments on commit 60c8f19

Please sign in to comment.