-
Notifications
You must be signed in to change notification settings - Fork 130
/
easy_test.go
77 lines (65 loc) · 1.54 KB
/
easy_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
package curl
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"sync"
)
func setupTestServer(serverContent string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, serverContent)
}))
}
func TestEasyInterface(t *testing.T) {
ts := setupTestServer("")
defer ts.Close()
easy := EasyInit()
defer easy.Cleanup()
easy.Setopt(OPT_URL, ts.URL)
if err := easy.Perform(); err != nil {
t.Fatal(err)
}
}
func TestCallbackFunction(t *testing.T) {
serverContent := "A random string"
ts := setupTestServer(serverContent)
defer ts.Close()
easy := EasyInit()
defer easy.Cleanup()
easy.Setopt(OPT_URL, ts.URL)
easy.Setopt(OPT_WRITEFUNCTION, func(buf []byte, userdata interface{}) bool {
result := string(buf)
expected := serverContent + "\n"
if result != expected {
t.Errorf("output should be %q and is %q.", expected, result)
}
return true
})
if err := easy.Perform(); err != nil {
t.Fatal(err)
}
}
func TestEscape(t *testing.T) {
easy := EasyInit()
defer easy.Cleanup()
payload := `payload={"msg": "First line\nSecond Line"}`
expected := `payload%3D%7B%22msg%22%3A%20%22First%20line%5CnSecond%20Line%22%7D`
result := easy.Escape(payload)
if result != expected {
t.Errorf("escaped output should be %q and is %q.", expected, result)
}
}
func TestConcurrentInitAndCleanup(t *testing.T) {
c := 2
var wg sync.WaitGroup
wg.Add(c)
for i := 0; i < c; i++ {
go func() {
wg.Done()
easy := EasyInit()
defer easy.Cleanup()
}()
}
wg.Wait()
}