-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
123 lines (81 loc) · 2.33 KB
/
util_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetEnv(t *testing.T) {
os.Setenv("key", "val")
val := GetEnv("key", "")
assert.Equal(t, "val", val)
os.Unsetenv("key")
val = GetEnv("ke2", "default")
assert.Equal(t, "default", val)
}
func TestGetEnvAsInt(t *testing.T) {
os.Setenv("key", "123")
val := GetEnvAsInt("key", 0)
assert.Equal(t, 123, val)
os.Unsetenv("key")
val = GetEnvAsInt("key", 0)
assert.Equal(t, 0, val)
os.Setenv("key", "blabla")
val = GetEnvAsInt("key", 1)
assert.Equal(t, 1, val)
}
func TestFileExists(t *testing.T) {
f, err := ioutil.TempFile(os.TempDir(), "util_test")
if err != nil {
t.Error(err)
}
defer func() {
f.Close()
}()
assert.True(t, FileExists(f.Name()))
assert.False(t, FileExists(""))
}
func TestGetFileType(t *testing.T) {
assert.Equal(t, PHOTO, GetFileType("attachments/smile.jpg"))
assert.Equal(t, VIDEO, GetFileType("attachments/puppy.mp4"))
assert.Equal(t, AUDIO, GetFileType("attachments/music.mp3"))
assert.Equal(t, OTHER, GetFileType("attachments/document.txt"))
}
func TestParseFileType(t *testing.T) {
assert.Equal(t, PHOTO, ParseFileType("photo"))
assert.Equal(t, VIDEO, ParseFileType("video"))
assert.Equal(t, AUDIO, ParseFileType("audio"))
assert.Equal(t, OTHER, ParseFileType("docx"))
}
func TestIsValidUrl(t *testing.T) {
assert.False(t, isValidUrl(""))
assert.True(t, isValidUrl("http://www.kg"))
}
func TestReadFile(t *testing.T) {
s, err := ReadFile("attachments/document.txt")
assert.NotEmpty(t, s)
assert.Nil(t, err)
s, err = ReadFile("attachments/blabla")
assert.Empty(t, s)
assert.NotNil(t, err)
}
func TestDoGet2(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("OK"))
}))
defer server.Close()
res, err := doGET(server.URL, map[string]interface{}{"a": "b"}, map[string]interface{}{"c": "d"}, 10)
assert.NotEmpty(t, res)
assert.Nil(t, err)
}
func TestDoPost2(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("OK"))
}))
defer server.Close()
res, err := doPOST(server.URL, map[string]interface{}{"a": "b"}, map[string]interface{}{"c": "d"}, 10)
assert.NotEmpty(t, res)
assert.Nil(t, err)
}