-
Notifications
You must be signed in to change notification settings - Fork 32
/
common_test.go
61 lines (51 loc) · 1.51 KB
/
common_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
package vestigo
import (
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTrimmedParamNames(t *testing.T) {
router := NewRouter()
// Wildcard should be _name
router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
log.Println("/*")
assert.Equal(t, []string([]string{"_name"}), TrimmedParamNames(r), "Should be _name")
})
// Some random name should be b
router.Get("/a/:b", func(w http.ResponseWriter, r *http.Request) {
log.Println("/a/:b")
assert.Equal(t, []string([]string{"b"}), TrimmedParamNames(r), "Should be b")
})
// Some random name
router.Get("/:a", func(w http.ResponseWriter, r *http.Request) {
log.Println("/:a")
assert.Equal(t, []string([]string{"a"}), TrimmedParamNames(r), "Should be a")
})
// Multiple parameters random name
router.Get("/a/:b/c/:d", func(w http.ResponseWriter, r *http.Request) {
log.Println("/a/:b/c/:d")
var foundB bool
var foundD bool
for _, v := range TrimmedParamNames(r) {
if v == "b" {
foundB = true
}
if v == "d" {
foundD = true
}
}
assert.True(t, foundB, "Result should contain b")
assert.True(t, foundD, "Result should contain d")
})
rec := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/a/", nil)
req4, _ := http.NewRequest("GET", "/a", nil)
req2, _ := http.NewRequest("GET", "/a/b", nil)
req3, _ := http.NewRequest("GET", "/a/b/c/d", nil)
router.ServeHTTP(rec, req1)
router.ServeHTTP(rec, req2)
router.ServeHTTP(rec, req3)
router.ServeHTTP(rec, req4)
}