mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-16 15:56:28 +03:00
* Fix issue #1258 * address comments * add a test * address comments 2
This commit is contained in:
parent
7dc23b18ae
commit
d0bf3e1647
2 changed files with 43 additions and 3 deletions
4
caddy.go
4
caddy.go
|
@ -232,7 +232,7 @@ func HasListenerWithAddress(addr string) bool {
|
||||||
func listenerAddrEqual(ln net.Listener, addr string) bool {
|
func listenerAddrEqual(ln net.Listener, addr string) bool {
|
||||||
lnAddr := ln.Addr().String()
|
lnAddr := ln.Addr().String()
|
||||||
hostname, port, err := net.SplitHostPort(addr)
|
hostname, port, err := net.SplitHostPort(addr)
|
||||||
if err != nil || hostname != "" {
|
if err != nil {
|
||||||
return lnAddr == addr
|
return lnAddr == addr
|
||||||
}
|
}
|
||||||
if lnAddr == net.JoinHostPort("::", port) {
|
if lnAddr == net.JoinHostPort("::", port) {
|
||||||
|
@ -241,7 +241,7 @@ func listenerAddrEqual(ln net.Listener, addr string) bool {
|
||||||
if lnAddr == net.JoinHostPort("0.0.0.0", port) {
|
if lnAddr == net.JoinHostPort("0.0.0.0", port) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return hostname != "" && lnAddr == addr
|
||||||
}
|
}
|
||||||
|
|
||||||
// TCPServer is a type that can listen and serve connections.
|
// TCPServer is a type that can listen and serve connections.
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package caddy
|
package caddy
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -56,3 +60,39 @@ func TestIsLoopback(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListenerAddrEqual(t *testing.T) {
|
||||||
|
ln1, err := net.Listen("tcp", "[::]:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ln1.Close()
|
||||||
|
|
||||||
|
ln1port := strconv.Itoa(ln1.Addr().(*net.TCPAddr).Port)
|
||||||
|
|
||||||
|
ln2, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ln2.Close()
|
||||||
|
|
||||||
|
ln2port := strconv.Itoa(ln2.Addr().(*net.TCPAddr).Port)
|
||||||
|
|
||||||
|
for i, test := range []struct {
|
||||||
|
ln net.Listener
|
||||||
|
addr string
|
||||||
|
expect bool
|
||||||
|
}{
|
||||||
|
{ln1, ":1234", false},
|
||||||
|
{ln1, "0.0.0.0:1234", false},
|
||||||
|
{ln1, ":" + ln1port + "", true},
|
||||||
|
{ln1, "0.0.0.0:" + ln1port + "", true},
|
||||||
|
{ln2, "127.0.0.1:1234", false},
|
||||||
|
{ln2, ":" + ln2port + "", false},
|
||||||
|
{ln2, "127.0.0.1:" + ln2port + "", true},
|
||||||
|
} {
|
||||||
|
if got, want := listenerAddrEqual(test.ln, test.addr), test.expect; got != want {
|
||||||
|
t.Errorf("Test %d (%s == %s): expected %v but was %v", i, test.addr, test.ln.Addr().String(), want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue