-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools_test.go
127 lines (108 loc) · 2.97 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
package toolkit
import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestTools_RandomString(t *testing.T) {
var testTools Tools
tests := []struct {
name string
length int
}{
{"Ten", 10},
{"Twenty", 20},
{"One", 1},
}
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
s := testTools.RandomString(entry.length)
if len(s) != entry.length {
t.Errorf("expected length %d, received %d", entry.length, len(s))
}
})
}
}
func TestTools_CreateNewDirectory(t *testing.T) {
tests := []struct {
name string
dirPath string
err error
}{
{"Non-existing path", "./testFolder", nil},
{"Non-existing path", "./testUploads", nil},
}
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
var tools Tools
// Call the CreateNewDirectory function
err := tools.CreateNewDirectory(entry.dirPath)
if err != nil && entry.err == nil {
t.Errorf("expected no error, but received %+v", err)
}
// Remove the directory if it was created
if _, err = os.Stat(entry.dirPath); err == nil {
err = os.RemoveAll(entry.dirPath)
if err != nil {
t.Errorf("failed to remove path %s. %+v", entry.dirPath, err)
}
}
})
}
}
func TestTools_Slugify(t *testing.T) {
tests := []struct {
name string
expected string
err bool
}{
{"Hello, World!", "hello-world", false},
{" !Hello && World!", "hello-world", false},
// Cyrillic characters
{"Привет, мир!Hello, World!", "hello-world", false},
{"Привет, мир!", "", true},
{"88GoLang!PyThon===Java? TYPESCRIPT@ ", "88golang-python-java-typescript", false},
{" ", "", true},
{"!!!!", "", true},
}
var tools Tools
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result, err := tools.Slugify(entry.name)
if result != entry.expected {
t.Errorf("expected %s, but received %s", entry.expected, result)
}
if err != nil && !entry.err {
t.Errorf("expected no error, but received %+v", err)
}
})
}
}
func TestTools_DownloadStaticFile(t *testing.T) {
// Define and initialize response recorder and request
resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/download", nil)
// Initalize tools
tools := &Tools{}
// Call TestTools_DownloadStaticFile
tools.DownloadStaticFile(resp, req, "./testdata", "img.png", "hello-world.png")
// Get result of the response
result := resp.Result()
// Close the body to ensure no resource leak
defer result.Body.Close()
// Check if the file downloaded entirely by checking content length
if result.Header["Content-Length"][0] != "5003" {
t.Error("wrong content-length of", result.Header["Content-Length"][0])
}
// Check headers
if result.Header["Content-Disposition"][0] != "attachment; filename=\"hello-world.png\"" {
t.Error("wrong content-length of", result.Header["Content-Length"][0])
}
// Check for an error
_, err := io.ReadAll(result.Body)
if err != nil {
t.Error(err)
}
}