Passed
Pull Request — master (#52)
by Stefano
02:05
created

aryFromInvalidPath   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package dictionary_test
2
3
import (
4
	"net/http"
5
	"net/http/httptest"
6
	"path/filepath"
7
	"testing"
8
	"time"
9
10
	"github.com/stefanoj3/dirstalk/pkg/dictionary"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
func TestDictionaryFromFile(t *testing.T) {
15
	entries, err := dictionary.NewDictionaryFrom("testdata/dict.txt", &http.Client{})
16
	assert.NoError(t, err)
17
18
	expectedValue := []string{
19
		"home",
20
		"home/index.php",
21
		"blabla",
22
	}
23
	assert.Equal(t, expectedValue, entries)
24
}
25
26
func TestShouldFailToCreateDictionaryFromInvalidPath(t *testing.T) {
27
	_, err := dictionary.NewDictionaryFrom("http:///home/\n", &http.Client{})
28
	assert.Error(t, err)
29
}
30
31
func TestDictionaryFromAbsolutePath(t *testing.T) {
32
	path, err := filepath.Abs("testdata/dict.txt")
33
	assert.NoError(t, err)
34
35
	entries, err := dictionary.NewDictionaryFrom(path, &http.Client{})
36
	assert.NoError(t, err)
37
38
	expectedValue := []string{
39
		"home",
40
		"home/index.php",
41
		"blabla",
42
	}
43
	assert.Equal(t, expectedValue, entries)
44
}
45
46
func TestDictionaryFromFileWithInvalidPath(t *testing.T) {
47
	t.Parallel()
48
49
	d, err := dictionary.NewDictionaryFrom("testdata/gibberish_nonexisting_file", &http.Client{})
50
	assert.Error(t, err)
51
	assert.Nil(t, d)
52
}
53
54
func TestNewDictionaryFromRemoteFile(t *testing.T) {
55
	srv := httptest.NewServer(
56
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57
			dict := `/home
58
/about
59
/contacts
60
something
61
potato
62
`
63
			w.WriteHeader(http.StatusOK)
64
			_, _ = w.Write([]byte(dict))
65
		}),
66
	)
67
	defer srv.Close()
68
69
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
70
	assert.NoError(t, err)
71
72
	expectedValue := []string{
73
		"/home",
74
		"/about",
75
		"/contacts",
76
		"something",
77
		"potato",
78
	}
79
	assert.Equal(t, expectedValue, entries)
80
}
81
82
func TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout(t *testing.T) {
83
	srv := httptest.NewServer(
84
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
85
			time.Sleep(time.Millisecond) // out of paranoia - we dont want unstable tests
86
87
			w.WriteHeader(http.StatusOK)
88
			_, _ = w.Write([]byte("/home"))
89
		}),
90
	)
91
	defer srv.Close()
92
93
	entries, err := dictionary.NewDictionaryFrom(
94
		srv.URL,
95
		&http.Client{
96
			Timeout: time.Microsecond,
97
		},
98
	)
99
	assert.Error(t, err)
100
	assert.Contains(t, err.Error(), "failed to get")
101
	assert.Contains(t, err.Error(), "Timeout")
102
103
	assert.Nil(t, entries)
104
}
105
106
func TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status(t *testing.T) {
107
	srv := httptest.NewServer(
108
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
109
			w.WriteHeader(http.StatusForbidden)
110
		}),
111
	)
112
	defer srv.Close()
113
114
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
115
	assert.Error(t, err)
116
	assert.Contains(t, err.Error(), srv.URL)
117
	assert.Contains(t, err.Error(), "status code 403")
118
119
	assert.Nil(t, entries)
120
}
121