forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips_test.go
72 lines (59 loc) · 1.64 KB
/
ips_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cloudflare
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
type MockTransport struct {
http.Transport
Server *httptest.Server
Path string
}
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
url, err := url.Parse(m.Server.URL + m.Path)
if err != nil {
return nil, err
}
req.URL = url
return m.Transport.RoundTrip(req)
}
func Test_IPs(t *testing.T) {
setup()
defer teardown()
mux := http.NewServeMux()
server = httptest.NewServer(mux)
defer server.Close()
defaultTransport := http.DefaultTransport
http.DefaultTransport = &MockTransport{
Server: server,
Path: "/ips",
}
defer func() { http.DefaultTransport = defaultTransport }()
mux.HandleFunc("/ips", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"ipv4_cidrs": ["199.27.128.0/21"],
"ipv6_cidrs": ["ffff:ffff::/32"],
"china_colos": ["42.81.6.0/25", "2408:871a:1801:7::/72"]
}
}`)
})
ipRanges, err := IPs()
assert.NoError(t, err)
assert.Len(t, ipRanges.IPv4CIDRs, 1)
assert.Equal(t, "199.27.128.0/21", ipRanges.IPv4CIDRs[0])
assert.Len(t, ipRanges.IPv6CIDRs, 1)
assert.Equal(t, "ffff:ffff::/32", ipRanges.IPv6CIDRs[0])
assert.Len(t, ipRanges.ChinaIPv4CIDRs, 1)
assert.Equal(t, "42.81.6.0/25", ipRanges.ChinaIPv4CIDRs[0])
assert.Len(t, ipRanges.ChinaIPv6CIDRs, 1)
assert.Equal(t, "2408:871a:1801:7::/72", ipRanges.ChinaIPv6CIDRs[0])
}