-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanonicalPath_test.go
89 lines (67 loc) · 2.43 KB
/
CanonicalPath_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
86
87
88
89
package gorest_test
import (
"testing"
"github.com/adamluzsi/testcase"
"github.com/stretchr/testify/require"
"github.com/adamluzsi/gorest"
)
func TestCleanPath(t *testing.T) {
s := testcase.NewSpec(t)
var subject = func(t *testcase.T) string {
return gorest.CanonicalPath(t.I(`path`).(string))
}
s.When(`path is a canonical non root path`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `/a/canonical/path` })
s.Then(`it will leave it as is`, func(t *testcase.T) {
require.Equal(t, `/a/canonical/path`, subject(t))
})
})
s.When(`path is a canonical root path`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `/` })
s.Then(`it will leave it as is`, func(t *testcase.T) {
require.Equal(t, `/`, subject(t))
})
})
s.When(`path is empty`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `` })
s.Then(`it will `, func(t *testcase.T) {
require.Equal(t, `/`, subject(t))
})
})
s.When(`path is has no leading slash`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `test` })
s.Then(`it will add the leading slash`, func(t *testcase.T) {
require.Equal(t, `/test`, subject(t))
})
})
s.When(`path is has multiple leading slash`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `//test` })
s.Then(`it will remove the extra leading slash`, func(t *testcase.T) {
require.Equal(t, `/test`, subject(t))
})
})
s.When(`path is starting with leading dot`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `./test` })
s.Then(`it will remove the leading dot`, func(t *testcase.T) {
require.Equal(t, `/test`, subject(t))
})
})
s.When(`path is has parent directory reference as double dot`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `/../test` })
s.Then(`it will remove the parent directory reference double dot`, func(t *testcase.T) {
require.Equal(t, `/test`, subject(t))
})
})
s.When(`path has trailing slash`, func(s *testcase.Spec) {
s.Let(`path`, func(t *testcase.T) interface{} { return `/test/` })
s.Then(`it will preserve the trailing slash`, func(t *testcase.T) {
require.Equal(t, `/test/`, subject(t))
})
})
}
func BenchmarkCanonicalPath(b *testing.B) {
const path = `/canonical/path`
for i := 0; i < b.N; i++ {
gorest.CanonicalPath(path)
}
}