package main import "testing" // Without --base-url, the base URL comes from --addr, with localhost for an // unspecified host. func TestBaseURLFromAddr(t *testing.T) { tests := []struct { addr string want string }{ {":8080", "http://localhost:8080"}, {"0.0.0.0:8080", "http://localhost:8080"}, {"[::]:8080", "http://localhost:8080"}, {"127.0.0.1:18080", "http://127.0.0.1:18080"}, {"[::1]:8080", "http://[::1]:8080"}, {"mock.example.com:80", "http://mock.example.com:80"}, } for _, tt := range tests { got, err := baseURLFromAddr(tt.addr) if err != nil || got != tt.want { t.Errorf( "baseURLFromAddr(%q) = %q, %v; want %q", tt.addr, got, err, tt.want, ) } } } // An address without a port, or with port 0, is refused. func TestBaseURLFromAddrRefused(t *testing.T) { for _, addr := range []string{"localhost", ":0"} { if got, err := baseURLFromAddr(addr); err == nil { t.Errorf("baseURLFromAddr(%q) = %q; want an error", addr, got) } } }