/
whois_test.go
153 lines (133 loc) · 3.04 KB
/
whois_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2014-2024 Li Kexian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Go module for domain and ip whois information query
* https://www.likexian.com/
*/
package whois
import (
"errors"
"strings"
"testing"
"time"
"github.com/likexian/gokit/assert"
"golang.org/x/net/proxy"
)
func TestVersion(t *testing.T) {
assert.Contains(t, Version(), ".")
assert.Contains(t, Author(), "likexian")
assert.Contains(t, License(), "Apache License")
}
func TestClient_SetDisableReferral(t *testing.T) {
client := NewClient()
resp, err := client.Whois("likexian.com")
assert.Nil(t, err)
assert.Equal(t, strings.Count(resp, "Domain Name: LIKEXIAN.COM"), 2)
client.SetDisableReferral(true)
resp, err = client.Whois("likexian.com")
assert.Nil(t, err)
assert.Equal(t, strings.Count(resp, "Domain Name: LIKEXIAN.COM"), 1)
}
func TestWhoisFail(t *testing.T) {
tests := []struct {
domain string
err error
}{
{"", ErrDomainEmpty},
{"likexian.jp?e", ErrWhoisServerNotFound},
{"1.1.1.1!", ErrWhoisServerNotFound},
}
for _, v := range tests {
_, err := Whois(v.domain)
assert.NotNil(t, err)
if !errors.Is(err, v.err) {
t.Fatalf("expect %v but got %v", v.err, err)
}
}
_, err := Whois("likexian.com", "127.0.0.1")
assert.NotNil(t, err)
}
func TestWhois(t *testing.T) {
tests := []string{
"com",
"xxx",
"cn",
"name.com",
"name.net",
"name.org",
"name.mobi",
"name.cn",
"name.com.cn",
"name.in",
"name.jp/e",
"1.1.1.1",
"2.1.1.1",
"3.1.1.1",
"4.1.1.1",
"5.1.1.1",
"2001:dc7::1",
"1",
"as2",
"as1878",
"as4610",
"as27648",
"as36865",
"172.109.217.241",
"144.200.46.16",
}
for _, v := range tests {
b, err := Whois(v)
assert.Nil(t, err)
assert.NotEqual(t, b, "")
}
_, err := Whois("likexian.com", "com.whois-servers.net")
assert.Nil(t, err)
}
func TestNewClient(t *testing.T) {
c := NewClient()
var err error
c.SetTimeout(10 * time.Millisecond)
_, err = c.Whois("likexian.com")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "timeout")
c.SetTimeout(10 * time.Second)
_, err = c.Whois("likexian.com")
assert.Nil(t, err)
c.SetDialer(proxy.FromEnvironment())
_, err = c.Whois("likexian.com")
assert.Nil(t, err)
}
func TestIsASN(t *testing.T) {
tests := []struct {
in string
out bool
}{
{"", false},
{"a", false},
{"ab", false},
{"as", false},
{"ab1", false},
{"as1a", false},
{"as1", true},
{"As1", true},
{"AS1", true},
{"AS123", true},
{"1", true},
{"123", true},
}
for _, v := range tests {
assert.Equal(t, IsASN(v.in), v.out)
}
}