-
Notifications
You must be signed in to change notification settings - Fork 1
/
recaptcha_test.go
153 lines (148 loc) · 2.67 KB
/
recaptcha_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
package recaptcha
import (
"errors"
"net/http"
"reflect"
"testing"
"time"
)
func TestNew(t *testing.T) {
type args struct {
secret string
options []Option
}
tests := []struct {
name string
args args
want *Recaptcha
wantErr bool
err error
}{
{
name: "Valid secret and default values",
args: args{
secret: "test-secret",
},
want: &Recaptcha{
secret: "test-secret",
client: http.DefaultClient,
version: 3,
action: "",
score: 0.5,
},
wantErr: false,
},
{
name: "Invalid secret (empty)",
args: args{
secret: "",
},
wantErr: true,
err: errMissingSecret,
},
{
name: "Version 2 with custom http client",
args: args{
secret: "test-secret",
options: []Option{
WithVersion(2),
WithHTTPClient(&http.Client{
Timeout: time.Second * 10,
}),
},
},
want: &Recaptcha{
secret: "test-secret",
client: &http.Client{
Timeout: time.Second * 10,
},
version: 2,
action: "",
score: 0.5,
},
wantErr: false,
},
{
name: "Version 3 with custom http client",
args: args{
secret: "test-secret",
options: []Option{
WithVersion(3),
WithHTTPClient(&http.Client{
Timeout: time.Second * 10,
}),
},
},
want: &Recaptcha{
secret: "test-secret",
client: &http.Client{
Timeout: time.Second * 10,
},
version: 3,
action: "",
score: 0.5,
},
wantErr: false,
},
{
name: "Version 3 with custom action name",
args: args{
secret: "test-secret",
options: []Option{
WithVersion(3),
WithAction("test-action"),
},
},
want: &Recaptcha{
secret: "test-secret",
client: http.DefaultClient,
version: 3,
action: "test-action",
score: 0.5,
},
wantErr: false,
},
{
name: "Version 3 with custom score",
args: args{
secret: "test-secret",
options: []Option{
WithVersion(3),
WithScore(0.8),
},
},
want: &Recaptcha{
secret: "test-secret",
client: http.DefaultClient,
version: 3,
action: "",
score: 0.8,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.secret, tt.args.options...)
if err != nil && !tt.wantErr {
t.Error(err)
return
}
if !tt.wantErr {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() got = %v, want %v", got, tt.want)
return
}
} else {
if err == nil {
t.Errorf("New() expected error got nil")
return
}
if !errors.Is(err, tt.err) {
t.Errorf("New() error = %v, want %v", err, tt.err)
return
}
}
})
}
}