1
|
|
|
package dictionary_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"net/http/httptest" |
6
|
|
|
"os" |
7
|
|
|
"path/filepath" |
8
|
|
|
"strings" |
9
|
|
|
"testing" |
10
|
|
|
"time" |
11
|
|
|
|
12
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
13
|
|
|
"github.com/stefanoj3/dirstalk/pkg/dictionary" |
14
|
|
|
"github.com/stretchr/testify/assert" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func TestDictionaryFromFile(t *testing.T) { |
18
|
|
|
entries, err := dictionary.NewDictionaryFrom("testdata/dict.txt", &http.Client{}) |
19
|
|
|
assert.NoError(t, err) |
20
|
|
|
|
21
|
|
|
expectedValue := []string{ |
22
|
|
|
"home", |
23
|
|
|
"home/index.php", |
24
|
|
|
"blabla", |
25
|
|
|
} |
26
|
|
|
assert.Equal(t, expectedValue, entries) |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
func TestShouldFailToCreateDictionaryFromInvalidPath(t *testing.T) { |
30
|
|
|
_, err := dictionary.NewDictionaryFrom("http:///home/\n", &http.Client{}) |
31
|
|
|
assert.Error(t, err) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func TestDictionaryFromAbsolutePath(t *testing.T) { |
35
|
|
|
path, err := filepath.Abs("testdata/dict.txt") |
36
|
|
|
assert.NoError(t, err) |
37
|
|
|
|
38
|
|
|
entries, err := dictionary.NewDictionaryFrom(path, &http.Client{}) |
39
|
|
|
assert.NoError(t, err) |
40
|
|
|
|
41
|
|
|
expectedValue := []string{ |
42
|
|
|
"home", |
43
|
|
|
"home/index.php", |
44
|
|
|
"blabla", |
45
|
|
|
} |
46
|
|
|
assert.Equal(t, expectedValue, entries) |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
func TestDictionaryWithUnableToReadFolderShouldFail(t *testing.T) { |
50
|
|
|
newFolderPath := "testdata/" + test.RandStringRunes(10) |
51
|
|
|
|
52
|
|
|
err := os.Mkdir(newFolderPath, 0200) |
53
|
|
|
assert.NoError(t, err) |
54
|
|
|
|
55
|
|
|
defer removeTestDirectory(t, newFolderPath) |
56
|
|
|
|
57
|
|
|
_, err = dictionary.NewDictionaryFrom(newFolderPath, &http.Client{}) |
58
|
|
|
assert.Error(t, err) |
59
|
|
|
assert.Contains(t, err.Error(), "permission denied") |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
func TestDictionaryFromFileWithInvalidPath(t *testing.T) { |
63
|
|
|
t.Parallel() |
64
|
|
|
|
65
|
|
|
d, err := dictionary.NewDictionaryFrom("testdata/gibberish_nonexisting_file", &http.Client{}) |
66
|
|
|
assert.Error(t, err) |
67
|
|
|
assert.Nil(t, d) |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
func TestNewDictionaryFromRemoteFile(t *testing.T) { |
71
|
|
|
srv := httptest.NewServer( |
72
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
73
|
|
|
dict := `/home |
74
|
|
|
/about |
75
|
|
|
/contacts |
76
|
|
|
something |
77
|
|
|
potato |
78
|
|
|
` |
79
|
|
|
w.WriteHeader(http.StatusOK) |
80
|
|
|
_, _ = w.Write([]byte(dict)) |
81
|
|
|
}), |
82
|
|
|
) |
83
|
|
|
defer srv.Close() |
84
|
|
|
|
85
|
|
|
entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{}) |
86
|
|
|
assert.NoError(t, err) |
87
|
|
|
|
88
|
|
|
expectedValue := []string{ |
89
|
|
|
"/home", |
90
|
|
|
"/about", |
91
|
|
|
"/contacts", |
92
|
|
|
"something", |
93
|
|
|
"potato", |
94
|
|
|
} |
95
|
|
|
assert.Equal(t, expectedValue, entries) |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
func TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout(t *testing.T) { |
99
|
|
|
srv := httptest.NewServer( |
100
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
101
|
|
|
time.Sleep(time.Millisecond) // out of paranoia - we dont want unstable tests |
102
|
|
|
|
103
|
|
|
w.WriteHeader(http.StatusOK) |
104
|
|
|
_, _ = w.Write([]byte("/home")) |
105
|
|
|
}), |
106
|
|
|
) |
107
|
|
|
defer srv.Close() |
108
|
|
|
|
109
|
|
|
entries, err := dictionary.NewDictionaryFrom( |
110
|
|
|
srv.URL, |
111
|
|
|
&http.Client{ |
112
|
|
|
Timeout: time.Microsecond, |
113
|
|
|
}, |
114
|
|
|
) |
115
|
|
|
assert.Error(t, err) |
116
|
|
|
assert.Contains(t, err.Error(), "failed to get") |
117
|
|
|
assert.Contains(t, err.Error(), "Timeout") |
118
|
|
|
|
119
|
|
|
assert.Nil(t, entries) |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
func TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status(t *testing.T) { |
123
|
|
|
srv := httptest.NewServer( |
124
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
125
|
|
|
w.WriteHeader(http.StatusForbidden) |
126
|
|
|
}), |
127
|
|
|
) |
128
|
|
|
defer srv.Close() |
129
|
|
|
|
130
|
|
|
entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{}) |
131
|
|
|
assert.Error(t, err) |
132
|
|
|
assert.Contains(t, err.Error(), srv.URL) |
133
|
|
|
assert.Contains(t, err.Error(), "status code 403") |
134
|
|
|
|
135
|
|
|
assert.Nil(t, entries) |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
func removeTestDirectory(t *testing.T, path string) { |
139
|
|
|
if !strings.Contains(path, "testdata") { |
140
|
|
|
t.Fatalf("cannot delete `%s`, it is not in a `testdata` folder", path) |
141
|
|
|
return |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
stats, err := os.Stat(path) |
145
|
|
|
if err != nil { |
146
|
|
|
t.Fatalf("failed to read `%s` properties", path) |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if !stats.IsDir() { |
150
|
|
|
t.Fatalf("cannot delete `%s`, it is not a directory", path) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
err = os.Remove(path) |
154
|
|
|
if err != nil { |
155
|
|
|
t.Fatalf("failed to remove `%s`: %s", path, err.Error()) |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|