46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
|
|
package wan
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestAddress(t *testing.T) {
|
||
|
|
for _, tc := range []struct {
|
||
|
|
name, body string
|
||
|
|
status int
|
||
|
|
want string
|
||
|
|
}{
|
||
|
|
{"public", `{"rows":[{"description":"WAN","ipv4":[{"ipaddr":"8.8.4.4/24"}]}]}`, 200, "8.8.4.4"},
|
||
|
|
{"lan", `{"rows":[{"description":"LAN","ipv4":[{"ipaddr":"8.8.4.4"}]}]}`, 200, ""},
|
||
|
|
{"private", `{"rows":[{"description":"WAN","ipv4":[{"ipaddr":"10.146.95.240"}]}]}`, 200, ""},
|
||
|
|
{"ambiguous", `{"rows":[{"description":"WAN","ipv4":[{"ipaddr":"8.8.4.4"},{"ipaddr":"8.8.8.8"}]}]}`, 200, ""},
|
||
|
|
{"failed", `{}`, 503, ""},
|
||
|
|
} {
|
||
|
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/interfaces/overview/interfaces_info" {
|
||
|
|
t.Errorf("unexpected API request")
|
||
|
|
}
|
||
|
|
if r.Header.Get("Content-Type") != "" {
|
||
|
|
t.Errorf("GET must not declare a JSON body")
|
||
|
|
}
|
||
|
|
key, secret, ok := r.BasicAuth()
|
||
|
|
if !ok || key != "synthetic" || secret != "fake" {
|
||
|
|
t.Errorf("missing authentication")
|
||
|
|
}
|
||
|
|
w.WriteHeader(tc.status)
|
||
|
|
fmt.Fprint(w, tc.body)
|
||
|
|
}))
|
||
|
|
defer s.Close()
|
||
|
|
c := Client{URL: s.URL, Key: "synthetic", Secret: "fake", HTTP: s.Client()}
|
||
|
|
got, err := c.Address(context.Background())
|
||
|
|
if got != tc.want || (err != nil) != (tc.want == "") {
|
||
|
|
t.Fatalf("got %q, %v", got, err)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|