-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
83 lines (71 loc) · 1.73 KB
/
Copy pathconfig_test.go
File metadata and controls
83 lines (71 loc) · 1.73 KB
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
package main
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewConfig_ConfigNotFound(t *testing.T) {
_, err := NewConfig(map[string]interface{}{})
assert.EqualError(t, err, "configuration not found")
}
func TestNewConfig_DecodeOK(t *testing.T) {
raw := []byte(`{
"onliner/krakend-fallback": {
"routes": [
{
"path": "/products/{product}/positions",
"required": ["product"],
"default": {"positions": [], "shops": null}
},
{
"path": "/health",
"required": [],
"default": {}
}
]
}
}`)
var input map[string]interface{}
err := json.Unmarshal(raw, &input)
assert.NoError(t, err)
cfg, err := NewConfig(input)
assert.NoError(t, err)
assert.NotNil(t, cfg)
assert.Equal(t, 2, len(cfg.Routes))
r0 := cfg.Routes[0]
assert.Equal(t, "/products/{product}/positions", r0.Path)
assert.Equal(t, len(r0.Required), 1)
assert.Equal(t, "product", r0.Required[0])
_, ok := r0.Default["positions"]
assert.True(t, ok)
_, ok = r0.Default["shops"]
assert.True(t, ok)
}
func TestConfig_MatchRoute_MatchesTemplate(t *testing.T) {
cfg := &Config{
Routes: []Route{
{
Path: "/products/{product}/positions",
Required: []string{"product"},
Default: map[string]interface{}{"positions": []interface{}{}},
},
{
Path: "/health",
Required: nil,
Default: nil,
},
},
}
route, ok := cfg.MatchRoute("/products/iphonex64s/positions")
assert.True(t, ok)
assert.Equal(t, "/products/{product}/positions", route.Path)
}
func TestConfig_MatchRoute_NoMatch(t *testing.T) {
cfg := &Config{
Routes: []Route{
{Path: "/a/{x}/b"},
},
}
_, ok := cfg.MatchRoute("/a/1/c")
assert.False(t, ok)
}