mirror of
https://github.com/caddyserver/caddy.git
synced 2026-07-10 10:54:28 +00:00
Adding support for ServerIP context
This commit is contained in:
parent
c8514ad7b7
commit
73d52490d0
2 changed files with 62 additions and 0 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
|
@ -302,6 +303,44 @@ func TestIP(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
type myIP string
|
||||
|
||||
func (ip myIP) mockInterfaces() ([]net.Addr, error) {
|
||||
a := net.ParseIP(string(ip))
|
||||
|
||||
return []net.Addr{
|
||||
&net.IPNet{IP: a, Mask: nil},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestServerIP(t *testing.T) {
|
||||
context := getContextOrFail(t)
|
||||
|
||||
tests := []string{
|
||||
// Test 0 - ipv4
|
||||
"1.1.1.1",
|
||||
// Test 1 - ipv6
|
||||
"2001:db8:a0b:12f0::1",
|
||||
}
|
||||
|
||||
for i, expectedIP := range tests {
|
||||
testPrefix := getTestPrefix(i)
|
||||
|
||||
// Mock the network interface
|
||||
ip := myIP(expectedIP)
|
||||
networkInterfacesFn = ip.mockInterfaces
|
||||
defer func() {
|
||||
networkInterfacesFn = net.InterfaceAddrs
|
||||
}()
|
||||
|
||||
actualIP := context.ServerIP()
|
||||
|
||||
if actualIP != expectedIP {
|
||||
t.Errorf("%sExpected IP \"%s\", found \"%s\".", testPrefix, expectedIP, actualIP)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestURL(t *testing.T) {
|
||||
context := getContextOrFail(t)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue