-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
136 lines (118 loc) · 2.84 KB
/
client_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
package maclookup
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClient_WithPrefixUri(t *testing.T) {
c := New()
assert.NotEmpty(t, c.prefixURI)
assert.Equal(t, c.prefixURI, "https://api.maclookup.app")
c.WithPrefixURI("https://example.org")
assert.Equal(t, c.prefixURI, "https://example.org")
c.WithPrefixURI("https://example.org/")
assert.Equal(t, c.prefixURI, "https://example.org")
c.WithPrefixURI("example.org/")
assert.Equal(t, c.prefixURI, "https://example.org")
c.WithPrefixURI("http://example.org/")
assert.Equal(t, c.prefixURI, "http://example.org")
c.WithPrefixURI("127.0.0.1")
assert.Equal(t, c.prefixURI, "http://127.0.0.1")
c.WithPrefixURI("127.0.0.1:8080")
assert.Equal(t, c.prefixURI, "http://127.0.0.1:8080")
c.WithPrefixURI("::1")
assert.Equal(t, c.prefixURI, "http://::1")
}
func TestClient_WithTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(500 * time.Millisecond)
fmt.Fprintln(w, `{"success":true,"found":true,"macPrefix":"000000","company":"XEROX CORPORATION","address":"M/S 105-50C, WEBSTER NY 14580, US","country":"US","blockStart":"000000000000","blockEnd":"000000FFFFFF","blockSize":16777215,"blockType":"MA-L","updated":"2015-11-17","isRand":false,"isPrivate":false}`)
}))
defer ts.Close()
timeout := 5 * time.Millisecond
client := New()
client.WithPrefixURI(ts.URL)
client.WithTimeout(timeout)
assert.Equal(t, client.timeOut, timeout)
_, err := client.Lookup("000000")
assert.NotNil(t, err)
var e *HTTPClientError
assert.True(t, errors.As(err, &e))
}
func Test_cleanMac(t *testing.T) {
type args struct {
mac string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Mac #1",
args: args{mac: "000000"},
want: "000000",
},
{
name: "Mac #2",
args: args{mac: "00:00:00"},
want: "000000",
},
{
name: "Mac #3",
args: args{mac: "00.00.00"},
want: "000000",
},
{
name: "Mac #4",
args: args{mac: "000.000"},
want: "000000",
},
{
name: "Mac #5",
args: args{mac: "00-00-00"},
want: "000000",
},
{
name: "Mac #6",
args: args{mac: "0A-0C:cc"},
want: "0A0CCC",
},
{
name: "Mac #7",
args: args{mac: "0A-0C:cc 0"},
want: "0A0CCC0",
},
{
name: "Mac #8",
args: args{mac: "0A-0C:cc 0a"},
want: "0A0CCC0",
},
{
name: "Mac #8",
args: args{mac: "0A-0C:cc 0a"},
want: "0A0CCC0",
},
{
name: "Mac #9",
args: args{mac: "0A-0C:cc 0aAb"},
want: "0A0CCC0AA",
},
{
name: "Mac #10",
args: args{mac: "0A-0C:cc 0aA"},
want: "0A0CCC0AA",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cleanMac(tt.args.mac); got != tt.want {
t.Errorf("cleanMac() = %v, want %v", got, tt.want)
}
})
}
}