-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorecaptcha_suite_test.go
67 lines (56 loc) · 1.65 KB
/
gorecaptcha_suite_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
package gorecaptcha
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestGorecaptcha(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Gorecaptcha Suite")
}
type MockRecaptcha struct {
recaptcha
}
var _ = Describe("Gorecaptcha", func() {
Context("parseErrorLine", func() {
It("Should parse status line to constant", func() {
Expect(parseErrorLine("invalid-site-private-key")).To(Equal(ErrInvalidSitePrivateKey))
Expect(parseErrorLine("somethinig not supported")).To(Equal(ErrUnknown))
})
})
Context("recaptcha", func() {
It("NewRecaptcha", func() {
key := "some-private-key"
r := NewRecaptcha(key)
Expect(r.privateKey).To(Equal(key))
})
It("Verify: return false and error on error response", func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
txt, _ := ioutil.ReadFile("test/resp_err_invalid_key.txt")
fmt.Fprintln(w, string(txt))
}))
defer ts.Close()
r := NewRecaptcha("key")
r.verifyURL = ts.URL
resp, _ := r.Verify("", "", "")
Expect(resp.Err).To(Equal(ErrInvalidSitePrivateKey))
Expect(resp.Status).To(Equal(false))
})
It("Verify: return true and no error on success response", func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
txt, _ := ioutil.ReadFile("test/response_success.txt")
fmt.Fprintln(w, string(txt))
}))
defer ts.Close()
r := NewRecaptcha("key")
r.verifyURL = ts.URL
resp, _ := r.Verify("", "", "")
Expect(resp.Err).To(BeNil())
Expect(resp.Status).To(Equal(true))
})
})
})