-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line arguments --bind and --port to set host and port, de…
…fault is localhost and 7070.
- Loading branch information
1 parent
7fa581a
commit b2c3ae1
Showing
9 changed files
with
214 additions
and
34 deletions.
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
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
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
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
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
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
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
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
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,73 @@ | ||
package netutil | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestGetLocalHostByIPType(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
ipType string | ||
want bool | ||
}{ | ||
{name: "Test with all IPs", ipType: "all", want: true}, | ||
{name: "Test with IPv4", ipType: "ipv4", want: true}, | ||
{name: "Test unknow ipType", ipType: "test", want: false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ips := GetLocalHostByIPType(tt.ipType) | ||
if got := len(ips); got > 0 != tt.want { | ||
t.Errorf("GetLocalHostByIPType(%v) = %v; want %v", tt.ipType, got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetHostAndIP(t *testing.T) { | ||
ipv4IPs := GetLocalHostByIPType("ipv4") | ||
var tests = []struct { | ||
bindStr string | ||
portStr string | ||
wantHost string | ||
wantPort int | ||
}{ | ||
{"", "", "localhost", 7070}, // default host and port | ||
{ipv4IPs[0], "", ipv4IPs[0], 7070}, // provide host | ||
{"", "8080", "localhost", 8080}, // provide port | ||
{ipv4IPs[0], "8080", ipv4IPs[0], 8080}, // provide host and port | ||
} | ||
|
||
for _, tt := range tests { | ||
host, port := GetHostAndIP(tt.bindStr, tt.portStr) | ||
if host != tt.wantHost || port != tt.wantPort { | ||
t.Errorf("GetHostAndIP(%v, %v) => (%v, %v), want (%v, %v)", tt.bindStr, tt.portStr, host, port, tt.wantHost, tt.wantPort) | ||
} | ||
} | ||
} | ||
|
||
func TestBuildAndDisplayURL(t *testing.T) { | ||
ipv4IPs := GetLocalHostByIPType("ipv4") | ||
url := fmt.Sprintf("http://%s:7070", ipv4IPs[0]) | ||
var tests = []struct { | ||
host string | ||
port int | ||
wantURL string | ||
}{ | ||
{"localhost", 7070, "http://localhost:7070"}, | ||
{"127.0.0.1", 7070, "http://127.0.0.1:7070"}, | ||
{"0.0.0.0", 7070, "http://0.0.0.0:7070"}, | ||
{ipv4IPs[0], 7070, url}, | ||
{"::1", 7070, "http://::1:7070"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
gotURL := BuildAndDisplayURL(tt.host, tt.port) | ||
if gotURL != tt.wantURL { | ||
t.Errorf("BuildAndDisplayURL(%v, %v) => %v, want %v", tt.host, tt.port, gotURL, tt.wantURL) | ||
} | ||
} | ||
} |