Skip to content

Commit 00651cf

Browse files
authored
Add GetHostPort function to extract the host and port from network addresses (#29)
This patch adds a new utility function called `GetHostPort` to help extracting the host and port from network addresses. This function will be used in the generated `main.go` to configure the webhook server host and port. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent bf25ef6 commit 00651cf

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

pkg/util/address.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/url"
7+
"strconv"
8+
)
9+
10+
// GetHostPort extracts the host and port from a given address string.
11+
func GetHostPort(address string) (string, int, error) {
12+
u, err := url.Parse(fmt.Sprintf("//%s", address))
13+
if err != nil {
14+
return "", 0, err
15+
}
16+
17+
host, portString, err := net.SplitHostPort(u.Host)
18+
if err != nil {
19+
return "", 0, err
20+
}
21+
22+
port, err := strconv.Atoi(portString)
23+
if err != nil {
24+
return "", 0, fmt.Errorf("cannot parse port: %v", err)
25+
}
26+
27+
return host, port, nil
28+
}

pkg/util/address_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package util_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/aws-controllers-k8s/runtime/pkg/util"
10+
)
11+
12+
func TestGetHostPort(t *testing.T) {
13+
assert := assert.New(t)
14+
type args struct {
15+
address string
16+
}
17+
testCases := []struct {
18+
name string
19+
args args
20+
wantErr bool
21+
wantHost string
22+
wantPort int
23+
}{
24+
{
25+
name: "empty string",
26+
args: args{
27+
address: "",
28+
},
29+
wantErr: true,
30+
},
31+
{
32+
name: "malformed address",
33+
args: args{
34+
address: "host::1",
35+
},
36+
wantErr: true,
37+
},
38+
{
39+
name: "port only",
40+
args: args{
41+
address: ":500",
42+
},
43+
wantErr: false,
44+
wantHost: "",
45+
wantPort: 500,
46+
},
47+
{
48+
name: "host only - port should be specified",
49+
args: args{
50+
address: "localhost",
51+
},
52+
wantErr: true,
53+
},
54+
{
55+
name: "ipv4",
56+
args: args{
57+
address: "0.0.0.0:80",
58+
},
59+
wantErr: false,
60+
wantHost: "0.0.0.0",
61+
wantPort: 80,
62+
},
63+
{
64+
name: "ipv6",
65+
args: args{
66+
address: "[0::0]:443",
67+
},
68+
wantErr: false,
69+
wantHost: "0::0",
70+
wantPort: 443,
71+
},
72+
}
73+
74+
for _, tc := range testCases {
75+
t.Run(tc.name, func(t *testing.T) {
76+
host, port, err := util.GetHostPort(
77+
tc.args.address,
78+
)
79+
if (err != nil) != tc.wantErr {
80+
assert.Fail(fmt.Sprintf("GetHostPort(%s) error = %v, wantErr %v", tc.name, err, tc.wantErr))
81+
} else if !tc.wantErr {
82+
assert.Equal(tc.wantHost, host)
83+
assert.Equal(tc.wantPort, port)
84+
}
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)