-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
136 lines (107 loc) · 2.82 KB
/
client_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package iterate
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
const accessToken = "abc123"
type Results struct {
Token string
}
func TestList(t *testing.T) {
_, server := mockServer(Response{
Results: []Survey{{Id: "123"}},
})
defer server.Close()
client := New(accessToken)
client.host = server.URL
surveys, err := client.ListSurveys()
if err != nil {
t.Error(err)
}
if len(surveys) == 0 {
t.Error("Should have listed surveys")
}
}
func TestSend(t *testing.T) {
requests, server := mockServer(Response{})
defer server.Close()
surveyId := "58223a83d5167e00010012d1"
email := "art@vandelayindustries.com"
firstName := "Art"
lastName := "Vandelay"
delay := 7 * 24 * time.Hour
date := time.Now().Add(24 * time.Hour)
client := New(accessToken)
client.host = server.URL
t.Run("with basic send parameters", func(t *testing.T) {
client.EmailSurvey(surveyId, SendParams{Email: email})
r := <-requests
if r.FormValue("access_token") != accessToken {
t.Error("Request should contain the api key")
}
if r.FormValue("v") != client.version {
t.Error("Request should contain the api version")
}
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
t.Error("Request should have content-type application/x-www-form-urlencoded")
}
if !strings.Contains(r.RequestURI, surveyId) {
t.Error("Request url should contain the survey id")
}
if r.FormValue("email") != email {
t.Error("Request should have the email")
}
})
t.Run("with delay", func(t *testing.T) {
client.EmailSurvey(surveyId, SendParams{
Email: email,
FirstName: firstName,
LastName: lastName,
Delay: delay,
})
r := <-requests
if r.FormValue("email") != email {
t.Error("Request should have the email")
}
if r.FormValue("first_name") != firstName {
t.Error("Request should have the first name")
}
if r.FormValue("last_name") != lastName {
t.Error("Request should have the last name")
}
if r.FormValue("delay") != "604800" {
t.Error("Request should have the delay")
}
})
t.Run("with date", func(t *testing.T) {
client.EmailSurvey(surveyId, SendParams{
Email: email,
Date: date,
})
r := <-requests
if r.FormValue("email") != email {
t.Error("Request should have the email")
}
if r.FormValue("date") != fmt.Sprintf("%v", date.Unix()) {
t.Error("Request should have the date")
}
})
}
func mockServer(response interface{}) (chan *http.Request, *httptest.Server) {
requests := make(chan *http.Request, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if response != nil {
json, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
requests <- r
}))
return requests, server
}