-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrecaptcha_test.go
47 lines (41 loc) · 950 Bytes
/
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
package recaptcha
import (
"fmt"
"github.com/haisum/recaptcha"
"log"
"net/http"
)
func ExampleR_Verify() {
sitekey := "{Your site key here}"
re := recaptcha.R{
Secret: "{Your secret here}",
}
form := fmt.Sprintf(`
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<form action="/submit" method="post">
<div class="g-recaptcha" data-sitekey="%s"></div>
<input type="submit">
</form>
</body>
</html>
`, sitekey)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, form)
})
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
isValid := re.Verify(*r)
if isValid {
fmt.Fprintf(w, "Valid")
} else {
fmt.Fprintf(w, "Invalid! These errors ocurred: %v", re.LastError())
}
})
err := http.ListenAndServe(":8100", nil)
if err != nil {
log.Printf("Could not start server. %s", err)
}
}