mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-07-07 08:36:40 +00:00
11b7b7247f
* util: support IPv6 host port parsing * Update weed/util/parse.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package util
|
|
|
|
import "testing"
|
|
|
|
func TestParseHostPort(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
hostPort string
|
|
wantHost string
|
|
wantPort int64
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "hostname",
|
|
hostPort: "localhost:8888",
|
|
wantHost: "localhost",
|
|
wantPort: 8888,
|
|
},
|
|
{
|
|
name: "ipv4",
|
|
hostPort: "127.0.0.1:8888",
|
|
wantHost: "127.0.0.1",
|
|
wantPort: 8888,
|
|
},
|
|
{
|
|
name: "bracketed ipv6",
|
|
hostPort: "[::1]:8888",
|
|
wantHost: "::1",
|
|
wantPort: 8888,
|
|
},
|
|
{
|
|
name: "missing port",
|
|
hostPort: "localhost",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid port",
|
|
hostPort: "localhost:bad",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotHost, gotPort, err := ParseHostPort(tt.hostPort)
|
|
if gotErr := err != nil; gotErr != tt.wantErr {
|
|
t.Fatalf("ParseHostPort(%q) error = %v, wantErr %v", tt.hostPort, err, tt.wantErr)
|
|
}
|
|
if tt.wantErr {
|
|
return
|
|
}
|
|
if gotHost != tt.wantHost || gotPort != tt.wantPort {
|
|
t.Fatalf("ParseHostPort(%q) = (%q, %d), want (%q, %d)", tt.hostPort, gotHost, gotPort, tt.wantHost, tt.wantPort)
|
|
}
|
|
})
|
|
}
|
|
}
|