-
Notifications
You must be signed in to change notification settings - Fork 0
/
recaptcha_test.go
71 lines (58 loc) · 1.54 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
package recaptcha
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestSecretGetsSet(t *testing.T) {
secretKey := "My Secret Key"
r := New(secretKey)
if r.secret != secretKey {
t.Errorf("Expected %s, got %s", secretKey, r.secret)
}
}
func TestDefaultHTTPClientIsUsedIfNotSet(t *testing.T) {
r := New("")
if r.httpClient != http.DefaultClient {
t.Error("Expected httpClient to be default but was not")
}
}
func TestGetErrorsReturnsNilWhenNoErrors(t *testing.T) {
r := New("")
if r.GetErrors() != nil {
t.Error("Expected nil, got ", r.GetErrors())
}
}
func TestVerifySetsSuccess(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
json.NewEncoder(w).Encode(verifyResponse{
Success: true,
})
}))
defer server.Close()
postUrl = server.URL
r := New("")
if success, _ := r.Verify("", ""); success != true {
t.Error("Expected true, got false")
}
}
func TestVerifySetsErrorCodes(t *testing.T) {
errorCodes := []string{"Error1", "Error2"}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
json.NewEncoder(w).Encode(verifyResponse{
Success: false,
ErrorCodes: errorCodes,
})
}))
defer server.Close()
postUrl = server.URL
r := New("")
r.Verify("", "")
if !reflect.DeepEqual(r.GetErrors(), errorCodes) {
t.Error("Expected ", errorCodes, " got ", r.GetErrors())
}
}