Passed
Pull Request — master (#46)
by Stefano
02:51
created

pkg/dictionary/dictionary_integration_test.go   A

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 73
dl 0
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A dictionary_test.TestDictionaryFromFileWithInvalidPath 0 6 1
A dictionary_test.TestDictionaryFromFile 0 10 1
A dictionary_test.TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status 0 14 2
A dictionary_test.TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout 0 22 2
A dictionary_test.TestDictionaryFromAbsolutePath 0 13 1
A dictionary_test.TestNewDictionaryFromRemoteFile 0 26 2
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("", &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 TestDictionaryFromAbsolutePath(t *testing.T) {
27
	path, err := filepath.Abs("testdata/dict.txt")
28
	assert.NoError(t, err)
29
30
	entries, err := dictionary.NewDictionaryFrom(path, &http.Client{})
31
	assert.NoError(t, err)
32
33
	expectedValue := []string{
34
		"home",
35
		"home/index.php",
36
		"blabla",
37
	}
38
	assert.Equal(t, expectedValue, entries)
39
}
40
41
func TestDictionaryFromFileWithInvalidPath(t *testing.T) {
42
	t.Parallel()
43
44
	d, err := dictionary.NewDictionaryFrom("testdata/gibberish_nonexisting_file", &http.Client{})
45
	assert.Error(t, err)
46
	assert.Nil(t, d)
47
}
48
49
func TestNewDictionaryFromRemoteFile(t *testing.T) {
50
	srv := httptest.NewServer(
51
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52
			dict := `/home
53
/about
54
/contacts
55
something
56
potato
57
`
58
			w.WriteHeader(http.StatusOK)
59
			_, _ = w.Write([]byte(dict))
60
		}),
61
	)
62
	defer srv.Close()
63
64
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
65
	assert.NoError(t, err)
66
67
	expectedValue := []string{
68
		"/home",
69
		"/about",
70
		"/contacts",
71
		"something",
72
		"potato",
73
	}
74
	assert.Equal(t, expectedValue, entries)
75
}
76
77
func TestNewDictionaryFromRemoteFileWillReturnErrorWhenRequestTimeout(t *testing.T) {
78
	srv := httptest.NewServer(
79
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80
			time.Sleep(time.Millisecond) // out of paranoia - we dont want unstable tests
81
82
			w.WriteHeader(http.StatusOK)
83
			_, _ = w.Write([]byte("/home"))
84
		}),
85
	)
86
	defer srv.Close()
87
88
	entries, err := dictionary.NewDictionaryFrom(
89
		srv.URL,
90
		&http.Client{
91
			Timeout: time.Microsecond,
92
		},
93
	)
94
	assert.Error(t, err)
95
	assert.Contains(t, err.Error(), "failed to get")
96
	assert.Contains(t, err.Error(), "Timeout")
97
98
	assert.Nil(t, entries)
99
}
100
101
func TestNewDictionaryFromRemoteShouldFailWhenRemoteReturnNon200Status(t *testing.T) {
102
	srv := httptest.NewServer(
103
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
104
			w.WriteHeader(http.StatusForbidden)
105
		}),
106
	)
107
	defer srv.Close()
108
109
	entries, err := dictionary.NewDictionaryFrom(srv.URL, &http.Client{})
110
	assert.Error(t, err)
111
	assert.Contains(t, err.Error(), srv.URL)
112
	assert.Contains(t, err.Error(), "status code 403")
113
114
	assert.Nil(t, entries)
115
}
116