mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
48 lines
864 B
Go
48 lines
864 B
Go
package gee
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func newTestRouter() *router {
|
|
r := newRouter()
|
|
r.addRoute("GET", "/", nil)
|
|
r.addRoute("GET", "/hello/:name", nil)
|
|
r.addRoute("GET", "/hello/b/c", nil)
|
|
r.addRoute("GET", "/hi/:name", nil)
|
|
return r
|
|
}
|
|
|
|
func TestGetRoute(t *testing.T) {
|
|
r := newTestRouter()
|
|
n, ps := r.getRoute("GET", "/hello/geektutu")
|
|
|
|
if n == nil {
|
|
t.Fatal("nil shouldn't be returned")
|
|
}
|
|
|
|
if n.pattern != "/hello/:name" {
|
|
t.Fatal("should match /hello/:name")
|
|
}
|
|
|
|
if ps["name"] != "geektutu" {
|
|
t.Fatal("name should be equal to 'geektutu'")
|
|
}
|
|
|
|
fmt.Printf("matched path: %s, params['name']: %s\n", n.pattern, ps["name"])
|
|
|
|
}
|
|
|
|
func TestGetRoutes(t *testing.T) {
|
|
r := newTestRouter()
|
|
nodes := r.getRoutes("GET")
|
|
for i, n := range nodes {
|
|
fmt.Println(i+1, n)
|
|
}
|
|
|
|
if len(nodes) != 4 {
|
|
t.Fatal("the number of routes shoule be 4")
|
|
}
|
|
}
|