-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_types_test.go
More file actions
100 lines (83 loc) · 2.15 KB
/
task_types_test.go
File metadata and controls
100 lines (83 loc) · 2.15 KB
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
package captcha
import(
"encoding/base64"
"io/ioutil"
"net/http"
"testing"
"net/url"
"strings"
"bufio"
"os"
)
const(
https = true
provider = "api.example.com"
apiKey = "api-key"
)
type DataToTest struct{
pathFile string
expected string
}
func TestRegularCaptcha(t *testing.T){
var input = []DataToTest{
{"img/test_captcha_1.jpg", "pvpg78"},
{"img/test_captcha_2.jpg", "8zjcmw"},
{"img/test_captcha_3.jpg", "y4vuj"},
}
for _, target := range input{
// Open image file
imgFile, err := os.Open(target.pathFile)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
// Read image file
reader := bufio.NewReader(imgFile)
content, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
// Encode as base64
encodedImg := base64.StdEncoding.EncodeToString(content)
client, err := Client(https, provider, apiKey)
if err != nil{
t.Fatal("Expected no error, but got:", err)
}
resolve, err := client.RegularCaptcha(encodedImg, 60)
if err != nil{
t.Fatal("Expected no error, but got:", err)
}
if resolve == ""{
t.Errorf("Expected %s, but got blank", target.expected)
} else if resolve != target.expected{
t.Errorf("Expected output to be %s, but got %s", target.expected, resolve)
}
}
}
func TestReCaptchaV2(t *testing.T){
var(
inputTarget = "https://www.google.com/recaptcha/api2/demo"
targetSitekey = "6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"
)
client, err := Client(https, provider, apiKey)
if err != nil{
t.Fatal("Expected no error, but got:", err)
}
resolve, err := client.ReCaptchaV2(inputTarget, targetSitekey, "", false, 60)
if err != nil{
t.Fatal("Expected no error, but got:", err)
}
if resolve == ""{
t.Fatal("Expected resolve, but got blank")
}
resp, err := http.PostForm(inputTarget, url.Values{"g-recaptcha-response": {resolve}})
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal("Expected no error, but got:", err)
}
if !strings.Contains(string(body), "Verification Success... Hooray!"){
t.Error("The captcha answer received is invalid")
}
}