-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
55 lines (39 loc) · 1.12 KB
/
example_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
package routing_test
import (
"fmt"
"github.com/golossus/routing"
"net/http"
"net/http/httptest"
)
func init() {
routing.AddHandler(MyHandler, "myhandler")
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello MyHandler")
}
func ExampleAddHandler_usingHandlerAlias() {
h, _ := routing.GetHandler("myhandler")
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h(w, r)
fmt.Println(w.Body.String())
// Output: Hello MyHandler
}
func ExampleAddHandler_usingHandlerCanonicalName() {
h, _ := routing.GetHandler("github.com/golossus/routing_test.MyHandler")
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h(w, r)
fmt.Println(w.Body.String())
// Output: Hello MyHandler
}
func ExampleRouter_NewRoute_routeRegistration() {
h, _ := routing.GetHandler("github.com/golossus/routing_test.MyHandler")
router := routing.NewRouter()
_ = router.NewRoute().Path("/").Method("GET").Handler(h).Register()
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
fmt.Println(w.Body.String())
// Output: Hello MyHandler
}