Completed
Pull Request — master (#62)
by Stefano
02:19
created

dFolderShouldFail   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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