-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
85 lines (69 loc) · 2.33 KB
/
config_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
package short
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
t.Run("WithHost", func(t *testing.T) {
t.Run("default", func(t *testing.T) {
c := DefaultConfig()
require.Equal(t, "localhost:8080", c.getConfig().host)
})
helperValid := func(t *testing.T, host string) {
t.Helper()
c := DefaultConfig().WithHost(host)
require.Nil(t, c.getConfig().err)
require.Equal(t, host, c.getConfig().host)
}
helperInvalid := func(t *testing.T, host string) {
t.Helper()
c := DefaultConfig().WithHost(host)
require.NotNil(t, c.getConfig().err)
}
t.Run("valid english", func(t *testing.T) {
helperValid(t, "url.com")
})
t.Run("valid english + port", func(t *testing.T) {
helperValid(t, "url.com:1234")
})
t.Run("valid non-english", func(t *testing.T) {
helperValid(t, "ヒキワリ.ナットウ.ニホン")
})
t.Run("valid non-english + port", func(t *testing.T) {
helperValid(t, "ヒキワリ.ナットウ.ニホン:80")
})
t.Run("invalid hostname", func(t *testing.T) {
helperInvalid(t, "myurl%.com")
})
t.Run("invalid port", func(t *testing.T) {
helperInvalid(t, "myurl.com:fsdfsdf")
})
t.Run("valid hostname with https schema", func(t *testing.T) {
c := DefaultConfig().WithHost("https://myurl.com")
require.Nil(t, c.getConfig().err)
require.Equal(t, "myurl.com", c.getConfig().host)
})
t.Run("valid hostname with http schema", func(t *testing.T) {
c := DefaultConfig().WithHost("http://myurl.com")
require.Nil(t, c.getConfig().err)
require.Equal(t, "myurl.com", c.getConfig().host)
})
})
t.Run("WithMongoUri", func(t *testing.T) {
t.Run("default", func(t *testing.T) {
c := DefaultConfig()
require.Equal(t, "mongodb://localhost:27017", c.getConfig().mongoUri)
})
t.Run("valid", func(t *testing.T) {
mongoUri := "mongodb://root:[email protected]:27017,198.342.121.23:27017,142.32.32.21:3001/databasename"
c := DefaultConfig().WithMongoUri(mongoUri)
require.Nil(t, c.getConfig().err)
require.Equal(t, mongoUri, c.getConfig().mongoUri)
})
t.Run("invalid", func(t *testing.T) {
mongoUri := "mongodb://root:[email protected]:27017198.342.121.23:27017,142.32.32.21:3001/databasename"
c := DefaultConfig().WithMongoUri(mongoUri)
require.NotNil(t, c.getConfig().err)
})
})
}