forked from go-zoo/bone
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbone_test.go
More file actions
61 lines (51 loc) · 1.12 KB
/
Copy pathbone_test.go
File metadata and controls
61 lines (51 loc) · 1.12 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
package bone
import (
"net/http"
"net/http/httptest"
"testing"
)
// Test if the route is valid
func TestRouting(t *testing.T) {
mux := New()
call := false
mux.Get("/a/:id", func(http.ResponseWriter, *http.Request) {
call = true
})
r, _ := http.NewRequest("GET", "/b/123", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if call {
t.Error("handler should not be called")
}
}
// Test if the http method is valid
func TestRoutingMethod(t *testing.T) {
mux := New()
call := false
mux.Get("/t", func(http.ResponseWriter, *http.Request) {
call = true
})
r, _ := http.NewRequest("POST", "/t", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if call {
t.Error("response to a wrong method")
}
}
// Test if the mux don't handle by prefix
func TestRoutingPath(t *testing.T) {
mux := New()
call := false
mux.Get("/t", func(http.ResponseWriter, *http.Request) {
call = true
})
mux.Get("/t/x", func(http.ResponseWriter, *http.Request) {
call = false
})
r, _ := http.NewRequest("GET", "/t/x", nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if call {
t.Error("response with the wrong path")
}
}