This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
244 lines (203 loc) · 5.1 KB
/
server_test.go
File metadata and controls
244 lines (203 loc) · 5.1 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package pushq
// First create config.json.
// goapp serve in a separate console window before running tests.
// go test to test localhost
// go test -args [env] to test an environment configured in config.json
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
)
// Environment is a config entry for running tests against local, beta, etc.
type Environment struct {
EnvName string
APIURL string
APITestKey string
APITestSecret string
}
// Config represents config.json
type Config struct {
Environments []Environment
}
var config Config
var testEnv Environment
func init() {
// Read the config file
fb, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Println(err.Error())
return
}
// Parse it
err = json.Unmarshal(fb, &config)
if err != nil {
fmt.Println(err.Error())
return
}
var localEnv Environment
// Check for local environment in the config file
for _, env := range config.Environments {
if env.EnvName == "local" {
localEnv = env
}
}
if localEnv.EnvName == "" {
fmt.Println("config.json missing local environment")
return
}
args := os.Args // e.g. go test -args beta
if len(args) == 2 {
for _, env := range config.Environments {
if env.EnvName == args[1] {
testEnv = env
}
}
}
// Default to local to handle "go test" with no args
if testEnv.EnvName == "" {
testEnv = localEnv
}
}
// getClient creates an http client that does not follow redirects
func getClient() *http.Client {
var netClient = &http.Client{
Timeout: time.Second * 10,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
return netClient
}
func setAuth(r *http.Request) {
r.Header.Set(XAPIKEY, testEnv.APITestKey)
r.Header.Set(XAPISECRET, testEnv.APITestSecret)
}
func TestBadQueueName(t *testing.T) {
url := testEnv.APIURL + "/enq"
client := &http.Client{
Timeout: time.Second * 10,
}
var task Task
task.DelaySeconds = 1
var headers []TaskHeader
task.Headers = headers
task.Payload = "ABC"
task.QueueName = "InvalidName!"
task.TimeoutSeconds = 5
task.URL = testEnv.APIURL + "/test"
jsonb, err := json.Marshal(task)
if err != nil {
t.Fatal("Unable to marshal test task")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonb))
setAuth(req)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
t.Fatalf(err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusNotAcceptable {
t.Fatalf("Did not get http.StatusNotAcceptable from %s: %s", url, body)
}
}
func TestEnq(t *testing.T) {
url := testEnv.APIURL + "/enq"
client := &http.Client{
Timeout: time.Second * 10,
}
var task Task
task.DelaySeconds = 1
var headers []TaskHeader
task.Headers = headers
task.Payload = "ABC"
task.QueueName = "default"
task.TimeoutSeconds = 5
task.URL = testEnv.APIURL + "/test"
//fmt.Printf("%+v\n", task)
jsonb, err := json.Marshal(task)
if err != nil {
t.Fatal("Unable to marshal test task")
}
//fmt.Printf("%s\n", string(jsonb))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonb))
setAuth(req)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
t.Fatalf(err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Did not get 200 OK from %s: %s", url, body)
}
}
func TestEnqErr(t *testing.T) {
url := testEnv.APIURL + "/enq"
client := &http.Client{
Timeout: time.Second * 10,
}
var task Task
task.DelaySeconds = 1
var headers []TaskHeader
task.Headers = headers
task.Payload = "XYZ"
task.QueueName = "crm"
task.TimeoutSeconds = 5
task.URL = testEnv.APIURL + "/testerr"
//fmt.Printf("%+v\n", task)
jsonb, err := json.Marshal(task)
if err != nil {
t.Fatal("Unable to marshal testerr task")
}
//fmt.Printf("%s\n", string(jsonb))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonb))
setAuth(req)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
t.Fatalf(err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Did not get 200 OK from %s: %s", url, body)
}
}
func TestCounts(t *testing.T) {
url := testEnv.APIURL + "/counts"
req, err := http.NewRequest("GET", url, nil)
setAuth(req)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: time.Second * 10,
}
time.Sleep(500 * time.Millisecond) // Wait for counts to persist
resp, err := client.Do(req)
if err != nil {
t.Fatalf(err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Did not get 200 OK from %s: %s", url, body)
}
var totals []CounterTotal
err = json.Unmarshal(body, &totals)
if err != nil {
t.Fatal("Unable to unmarshal JSON CounterTotal")
}
if len(totals) == 0 {
t.Fatal("Expected totals to have at least one entry")
}
for _, t := range totals {
fmt.Println(t.Name, ": ", t.Total)
}
}