This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a2fd08
commit 3eff1a9
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_ResolveAddr(t *testing.T) { | ||
t.Parallel() | ||
|
||
tcpAddrBuilder := func(t *testing.T, address string) *net.TCPAddr { | ||
tcpAddr, err := net.ResolveTCPAddr("", address) | ||
require.NoError(t, err) | ||
|
||
return tcpAddr | ||
} | ||
|
||
cases := []struct { | ||
name string | ||
address string | ||
defaultIP string | ||
errMsg string | ||
}{ | ||
{ | ||
name: "incorrect address", | ||
address: "Foo Bar", | ||
errMsg: "failed to parse addr", | ||
}, | ||
{ | ||
name: "only port provided", | ||
address: ":8080", | ||
defaultIP: "127.0.0.1", | ||
}, | ||
{ | ||
name: "both address and port provided", | ||
address: "255.0.255.0:8080", | ||
defaultIP: "", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ipAddr, err := ResolveAddr(c.address, c.defaultIP) | ||
if c.errMsg != "" { | ||
require.ErrorContains(t, err, c.errMsg) | ||
} else { | ||
require.NoError(t, err) | ||
expectedIPAddr := tcpAddrBuilder(t, fmt.Sprintf("%s%s", c.defaultIP, c.address)) | ||
require.Equal(t, expectedIPAddr, ipAddr) | ||
} | ||
}) | ||
} | ||
} |