-
Notifications
You must be signed in to change notification settings - Fork 20
/
tools_test.go
614 lines (520 loc) · 16.8 KB
/
tools_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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package toolbox
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"image"
"image/png"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
)
// RoundTripFunc is used to satisfy the interface requirements for http.Client
type RoundTripFunc func(req *http.Request) *http.Response
// RoundTrip is used to satisfy the interface requirements for http.Client
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
// NewTestClient returns *http.Client with Transport replaced to avoid making real calls
func NewTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}
type testData struct {
Data any `json:"bar"`
}
var pushTests = []struct {
name string
payload any
errorExpected bool
}{
{
name: "valid",
payload: testData{
Data: "bar",
},
errorExpected: false,
},
{
name: "invalid",
payload: make(chan int),
errorExpected: true,
},
}
func TestNew(t *testing.T) {
tools := New()
if tools.MaxXMLSize != defaultMaxUpload {
t.Error("wrong MaxXMLSize")
}
}
func TestTools_PushJSONToRemote(t *testing.T) {
for _, e := range pushTests {
client := NewTestClient(func(req *http.Request) *http.Response {
// Test request parameters
return &http.Response{
StatusCode: http.StatusOK,
// Send response to be tested
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
})
var testTools Tools
_, _, err := testTools.PushJSONToRemote("http://example.com/some/path", e.payload, client)
if err == nil && e.errorExpected {
t.Errorf("%s: error expected, but none received", e.name)
}
if err != nil && !e.errorExpected {
t.Errorf("%s: no error expected, but one received: %v", e.name, err)
}
}
}
var jsonTests = []struct {
name string
json string
errorExpected bool
maxSize int
allowUnknown bool
contentType string
}{
{name: "good json", json: `{"foo": "bar"}`, errorExpected: false, maxSize: 1024, allowUnknown: false},
{name: "badly formatted json", json: `{"foo":"}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "incorrect type", json: `{"foo": 1}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "incorrect type", json: `{1: 1}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "two json files", json: `{"foo": "bar"}{"alpha": "beta"}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "empty body", json: ``, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "syntax error in json", json: `{"foo": 1"}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "unknown field in json", json: `{"fooo": "bar"}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "incorrect type for field", json: `{"foo": 10.2}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "allow unknown field in json", json: `{"fooo": "bar"}`, errorExpected: false, maxSize: 1024, allowUnknown: true},
{name: "missing field name", json: `{jack: "bar"}`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "file too large", json: `{"foo": "bar"}`, errorExpected: true, maxSize: 5, allowUnknown: false},
{name: "not json", json: `Hello, world`, errorExpected: true, maxSize: 1024, allowUnknown: false},
{name: "wrong header", json: `{"foo": "bar"}`, errorExpected: true, maxSize: 1024, allowUnknown: false, contentType: "application/xml"},
}
func TestTools_ReadJSON(t *testing.T) {
for _, e := range jsonTests {
var testTools Tools
// set max file size
testTools.MaxJSONSize = e.maxSize
// allow/disallow unknown fields.
testTools.AllowUnknownFields = e.allowUnknown
// declare a variable to read the decoded json into.
var decodedJSON struct {
Foo string `json:"foo"`
}
// create a request with the body.
req, err := http.NewRequest("POST", "/", bytes.NewReader([]byte(e.json)))
if err != nil {
t.Log("Error", err)
}
if e.contentType != "" {
req.Header.Add("Content-Type", e.contentType)
} else {
req.Header.Add("Content-Type", "application/json")
}
// create a test response recorder, which satisfies the requirements
// for a ResponseWriter.
rr := httptest.NewRecorder()
// call ReadJSON and check for an error.
err = testTools.ReadJSON(rr, req, &decodedJSON)
// if we expect an error, but do not get one, something went wrong.
if e.errorExpected && err == nil {
t.Errorf("%s: error expected, but none received", e.name)
}
// if we do not expect an error, but get one, something went wrong.
if !e.errorExpected && err != nil {
t.Errorf("%s: error not expected, but one received: %s \n%s", e.name, err.Error(), e.json)
}
req.Body.Close()
}
}
func TestTools_ReadJSONAndMarshal(t *testing.T) {
// set max file size
var testTools Tools
// create a request with the body
req, err := http.NewRequest("POST", "/", bytes.NewReader([]byte(`{"foo": "bar"}`)))
if err != nil {
t.Log("Error", err)
}
// create a test response recorder, which satisfies the requirements
// for a ResponseWriter
rr := httptest.NewRecorder()
// call readJSON and check for an error; since we are using nil for the final parameter,
// we should get an error
err = testTools.ReadJSON(rr, req, nil)
// we expect an error, but did not get one, so something went wrong
if err == nil {
t.Error("error expected, but none received")
}
req.Body.Close()
}
var writeJSONTests = []struct {
name string
payload any
errorExpected bool
}{
{
name: "valid",
payload: JSONResponse{
Error: false,
Message: "foo",
},
errorExpected: false,
},
{
name: "invalid",
payload: make(chan int),
errorExpected: true,
},
}
func TestTools_WriteJSON(t *testing.T) {
for _, e := range writeJSONTests {
// create a variable of type toolbox.Tools, and just use the defaults.
var testTools Tools
rr := httptest.NewRecorder()
headers := make(http.Header)
headers.Add("FOO", "BAR")
err := testTools.WriteJSON(rr, http.StatusOK, e.payload, headers)
if err == nil && e.errorExpected {
t.Errorf("%s: expected error, but did not get one", e.name)
}
if err != nil && !e.errorExpected {
t.Errorf("%s: did not expect error, but got one: %v", e.name, err)
}
}
}
func TestTools_ErrorJSON(t *testing.T) {
var testTools Tools
rr := httptest.NewRecorder()
err := testTools.ErrorJSON(rr, errors.New("some error"), http.StatusServiceUnavailable)
if err != nil {
t.Error(err)
}
var requestPayload JSONResponse
decoder := json.NewDecoder(rr.Body)
err = decoder.Decode(&requestPayload)
if err != nil {
t.Error("received error when decoding ErrorJSON payload:", err)
}
if !requestPayload.Error {
t.Error("error set to false in response from ErrorJSON, and should be set to true")
}
if rr.Code != http.StatusServiceUnavailable {
t.Errorf("wrong status code returned; expected 503, but got %d", rr.Code)
}
}
func TestTools_RandomString(t *testing.T) {
var testTools Tools
s := testTools.RandomString(10)
if len(s) != 10 {
t.Error("wrong length random string returned")
}
}
func TestTools_DownloadLargeStaticFile(t *testing.T) {
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
var testTools Tools
testTools.DownloadStaticFile(rr, req, "./testdata", "tgg.jpg", "gatsby.jpg")
res := rr.Result()
defer res.Body.Close()
if res.Header["Content-Length"][0] != "240366" {
t.Error("wrong content length of", res.Header["Content-Length"][0])
}
if res.Header["Content-Disposition"][0] != "attachment; filename=\"gatsby.jpg\"" {
t.Error("wrong content disposition of", res.Header["Content-Disposition"][0])
}
_, err := io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
}
var uploadTests = []struct {
name string
allowedTypes []string
renameFile bool
errorExpected bool
maxSize int
uploadDir string
}{
{name: "allowed no rename", allowedTypes: []string{"image/jpeg", "image/png"}, renameFile: false, errorExpected: false, maxSize: 0, uploadDir: ""},
{name: "allowed rename", allowedTypes: []string{"image/jpeg", "image/png"}, renameFile: true, errorExpected: false, maxSize: 0, uploadDir: ""},
{name: "allowed no filetype specified", allowedTypes: []string{}, renameFile: true, errorExpected: false, maxSize: 0, uploadDir: ""},
{name: "not allowed", allowedTypes: []string{"image/jpeg"}, errorExpected: true, maxSize: 0, uploadDir: ""},
{name: "too big", allowedTypes: []string{"image/jpeg,", "image/png"}, errorExpected: true, maxSize: 10, uploadDir: ""},
{name: "invalid directory", allowedTypes: []string{"image/jpeg,", "image/png"}, errorExpected: true, maxSize: 0, uploadDir: "//"},
}
func TestTools_UploadFiles(t *testing.T) {
for _, e := range uploadTests {
// set up a pipe to avoid buffering
pr, pw := io.Pipe()
writer := multipart.NewWriter(pw)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer writer.Close()
defer wg.Done()
// create the form data field 'file'
part, err := writer.CreateFormFile("file", "./testdata/img.png")
if err != nil {
t.Error(err)
}
f, err := os.Open("./testdata/img.png")
if err != nil {
t.Error(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
t.Error("error decoding image", err)
}
err = png.Encode(part, img)
if err != nil {
t.Error(err)
}
}()
// read from the pipe which receives data
request := httptest.NewRequest("POST", "/", pr)
request.Header.Add("Content-Type", writer.FormDataContentType())
var testTools Tools
testTools.AllowedFileTypes = e.allowedTypes
if e.maxSize > 0 {
testTools.MaxFileSize = e.maxSize
}
var uploadDir = "./testdata/uploads/"
if e.uploadDir != "" {
uploadDir = e.uploadDir
}
uploadedFiles, err := testTools.UploadFiles(request, uploadDir, e.renameFile)
if err != nil && !e.errorExpected {
t.Error(err)
}
if !e.errorExpected {
if _, err := os.Stat(fmt.Sprintf("./testdata/uploads/%s", uploadedFiles[0].NewFileName)); os.IsNotExist(err) {
t.Errorf("%s: expected file to exist: %s", e.name, err.Error())
}
// clean up
_ = os.Remove(fmt.Sprintf("./testdata/uploads/%s", uploadedFiles[0].NewFileName))
}
if e.errorExpected && err == nil {
t.Errorf("%s: error expected, but none received", e.name)
}
// we're running table tests, so have to use a waitgroup
wg.Wait()
}
}
var uploadOneTests = []struct {
name string
uploadDir string
errorExpected bool
}{
{name: "valid", uploadDir: "./testdata/uploads/", errorExpected: false},
{name: "invalid", uploadDir: "//", errorExpected: true},
}
func TestTools_UploadOneFile(t *testing.T) {
for _, e := range uploadOneTests {
// set up a pipe to avoid buffering
pr, pw := io.Pipe()
writer := multipart.NewWriter(pw)
go func() {
defer writer.Close()
// create the form data field 'file'
part, err := writer.CreateFormFile("file", "./testdata/img.png")
if err != nil {
t.Error(err)
}
f, err := os.Open("./testdata/img.png")
if err != nil {
t.Error(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
t.Error("error decoding image", err)
}
err = png.Encode(part, img)
if err != nil {
t.Error(err)
}
}()
// read from the pipe which receives data
request := httptest.NewRequest("POST", "/", pr)
request.Header.Add("Content-Type", writer.FormDataContentType())
var testTools Tools
testTools.AllowedFileTypes = []string{"image/png"}
uploadedFiles, err := testTools.UploadOneFile(request, e.uploadDir, true)
if e.errorExpected && err == nil {
t.Errorf("%s: error expected, but none received", e.name)
}
if !e.errorExpected {
if _, err := os.Stat(fmt.Sprintf("./testdata/uploads/%s", uploadedFiles.NewFileName)); os.IsNotExist(err) {
t.Errorf("%s: expected file to exist: %s", e.name, err.Error())
}
// clean up
_ = os.Remove(fmt.Sprintf("./testdata/uploads/%s", uploadedFiles.NewFileName))
}
}
}
func TestTools_CreateDirIfNotExist(t *testing.T) {
var testTool Tools
err := testTool.CreateDirIfNotExist("./testdata/myDir")
if err != nil {
t.Error(err)
}
err = testTool.CreateDirIfNotExist("./testdata/myDir")
if err != nil {
t.Error(err)
}
_ = os.Remove("./testdata/myDir")
}
func TestTools_CreateDirIfNotExistInvalidDirectory(t *testing.T) {
var testTool Tools
// we should not be able to create a directory at the root level (no permissions)
err := testTool.CreateDirIfNotExist("/mydir")
if err == nil {
t.Error(errors.New("able to create a directory where we should not be able to"))
}
}
var slugTests = []struct {
name string
s string
expected string
errorExpected bool
}{
{name: "valid string", s: "now is the time", expected: "now-is-the-time", errorExpected: false},
{name: "empty string", s: "", expected: "", errorExpected: true},
{name: "complex string", s: "Now is the time for all GOOD men! + Fish & such &^?123", expected: "now-is-the-time-for-all-good-men-fish-such-123", errorExpected: false},
{name: "japanese string", s: "こんにちは世界", expected: "", errorExpected: true},
{name: "japanese string plus roman characters", s: "こんにちは世界 hello world", expected: "hello-world", errorExpected: false},
}
func TestTools_Slugify(t *testing.T) {
var testTool Tools
for _, e := range slugTests {
slug, err := testTool.Slugify(e.s)
if err != nil && !e.errorExpected {
t.Errorf("%s: error received when none expected: %s", e.name, err.Error())
}
if !e.errorExpected && slug != e.expected {
t.Errorf("%s: wrong slug returned; expected %s but got %s", e.name, e.expected, slug)
}
}
}
var writeXMLTests = []struct {
name string
payload any
errorExpected bool
}{
{
name: "valid",
payload: XMLResponse{
Error: false,
Message: "foo",
},
errorExpected: false,
},
{
name: "invalid",
payload: make(chan int),
errorExpected: true,
},
}
func TestTools_WriteXML(t *testing.T) {
for _, e := range writeXMLTests {
// create a variable of type toolbox.Tools, and just use the defaults.
var testTools Tools
rr := httptest.NewRecorder()
headers := make(http.Header)
headers.Add("FOO", "BAR")
err := testTools.WriteXML(rr, http.StatusOK, e.payload, headers)
if err != nil && !e.errorExpected {
t.Errorf("%s, failed to write XML: %v", e.name, err)
}
if err == nil && e.errorExpected {
t.Errorf("%s: error expected, but none received", e.name)
}
}
}
var xmlTests = []struct {
name string
xml string
maxBytes int
errorExpected bool
}{
{
name: "good xml",
xml: `<?xml version="1.0" encoding="UTF-8"?><note><to>John Smith</to><from>Jane Jones</from></note>`,
errorExpected: false,
},
{
name: "badly formatted xml",
xml: `<?xml version="1.0" encoding="UTF-8"?><note><xx>John Smith</to><from>Jane Jones</from></note>`,
errorExpected: true,
},
{
name: "too big",
xml: `<?xml version="1.0" encoding="UTF-8"?><note><to>John Smith</to><from>Jane Jones</from></note>`,
maxBytes: 10,
errorExpected: true,
},
{
name: "double xml",
xml: `<?xml version="1.0" encoding="UTF-8"?><note><to>John Smith</to><from>Jane Jones</from></note>
<?xml version="1.0" encoding="UTF-8"?><note><to>Luke Skywalker</to><from>R2D2</from></note>`,
errorExpected: true,
},
}
func TestTools_ReadXML(t *testing.T) {
for _, e := range xmlTests {
// create a variable of type toolbox.Tools, and just use the defaults.
var tools Tools
if e.maxBytes != 0 {
tools.MaxXMLSize = e.maxBytes
}
// create a request with the body.
req, err := http.NewRequest("POST", "/", bytes.NewReader([]byte(e.xml)))
if err != nil {
t.Log("Error", err)
}
// create a test response recorder, which satisfies the requirements
// for a ResponseWriter.
rr := httptest.NewRecorder()
// call ReadXML and check for an error.
var note struct {
To string `xml:"to"`
From string `xml:"from"`
}
err = tools.ReadXML(rr, req, ¬e)
if e.errorExpected && err == nil {
t.Errorf("%s: expected an error, but did not get one", e.name)
} else if !e.errorExpected && err != nil {
t.Errorf("%s: did not expect an error, but got one: %s", e.name, err)
}
}
}
func TestTools_ErrorXML(t *testing.T) {
var testTools Tools
rr := httptest.NewRecorder()
err := testTools.ErrorXML(rr, errors.New("some error"), http.StatusServiceUnavailable)
if err != nil {
t.Error(err)
}
var requestPayload XMLResponse
decoder := xml.NewDecoder(rr.Body)
err = decoder.Decode(&requestPayload)
if err != nil {
t.Error("received error when decoding ErrorXML payload:", err)
}
if !requestPayload.Error {
t.Error("error set to false in response from ErrorXML, and should be set to true")
}
if rr.Code != http.StatusServiceUnavailable {
t.Errorf("wrong status code returned; expected 503, but got %d", rr.Code)
}
}