diff --git a/internal/outboundip/ddwrt/iprequester.go b/internal/outboundip/ddwrt/iprequester.go index d6e106c..c18acf7 100644 --- a/internal/outboundip/ddwrt/iprequester.go +++ b/internal/outboundip/ddwrt/iprequester.go @@ -15,21 +15,22 @@ const ( ) type IpRequester struct { - client *http.Client - statusUrl string - login string - password string + StatusUrl string + + client *http.Client + login string + password string } func NewIpRequester(client *http.Client, statusUrl string, login string, password string) *IpRequester { - return &IpRequester{client: client, statusUrl: statusUrl, login: login, password: password} + return &IpRequester{client: client, StatusUrl: statusUrl, login: login, password: password} } func (r IpRequester) GetOutboundIp() <-chan outboundip.IpRequestResult { result := make(chan outboundip.IpRequestResult) go func() { defer close(result) - url := r.statusUrl + url := r.StatusUrl if url == "" { url = DefaultUrl } diff --git a/internal/outboundip/ddwrt/iprequester_test.go b/internal/outboundip/ddwrt/iprequester_test.go index 33fa871..feeb1a5 100644 --- a/internal/outboundip/ddwrt/iprequester_test.go +++ b/internal/outboundip/ddwrt/iprequester_test.go @@ -3,17 +3,27 @@ package ddwrt import ( "fmt" "net/http" + "net/http/httptest" "testing" - "time" ) +func init() { +} + func TestRequester_GetOutboundIp(t *testing.T) { - requester := NewIpRequester(&http.Client{ - Timeout: 30 * time.Second, - }, "", "laurent", "&951753seiko38613861") + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintln(w, `{wan_shortproto::dhcp_auth}{wan_status::Error  }{wan_uptime::1 day, 4:17:41}{pppoe_ac_name::}{wan_3g_signal::n.A.}{wan_ipaddr::92.130.63.129}{wan_netmask::255.255.248.0}{wan_gateway::92.130.56.1}{wan_dns0::192.168.1.250}{wan_dns1::80.10.246.4}{wan_dns2::81.253.149.15}{dhcp_remaining::0 days 15:24:14}{ttraff_in::142865}{ttraff_out::70849}{uptime:: 08:18:05 up 1 day, 4:18, load average: 1.00, 1.02, 1.00}{ipinfo:: IPv4: 92.130.63.129 IPv6: 2a01:cb22:112:c800:200:ff:fe00:0}`) + })) + defer s.Close() + + requester := NewIpRequester(s.Client(), s.URL, "laurent", "&951753seiko38613861") result := <-requester.GetOutboundIp() fmt.Printf("%+v\n", result) if result.Error != nil { t.Error(result.Error) } + if result.Ip != "92.130.63.129" { + t.Errorf("expected : 92.130.63.129, actual : %s", result.Ip) + } }