package mooh import ( "testing" ) func testRouter(req *Request) (Response, error) { resp := Response{} return resp, nil } func testBasic(t *testing.T) { d := BuildRouter() err := d.AddRoute(&Route{Method: "GET", Path: "/", Code: testRouter}) if err != nil { t.Fatal() } err = d.AddRoute(&Route{Method: "GET", Path: "/", Code: testRouter}) if err == nil { t.Fatal() } } func TestGetRouteList(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/bar", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/baz", Code: testRoute}) routes := r.GetRouteList() if len(routes) != 3 { t.Fatal() } } func TestGetHasPath(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/bar", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/baz", Code: testRoute}) if p := r.HasPath("/foo"); p == false { t.Fatal() } if p := r.HasPath("/foo/bar/baz"); p == true { t.Fatal() } } func TestRemovePath(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) err := r.RemovePath("/foo") if err != nil { t.Fatal() } err = r.RemovePath("/foo") if err == nil { t.Fatal() } } func TestGetAllRoutes(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/foo/bar", Code: testRoute}) r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) routes := r.GetAllRoutes() if len(routes) != 3 { t.Fatal() } } func TestGetAllRoutesByMethod(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/foo/bar", Code: testRoute}) r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) routes := r.GetAllRoutesByMethods("GET") if len(routes) != 2 { t.Fatal() } routes = r.GetAllRoutesByMethods("PUT") if len(routes) != 1 { t.Fatal() } routes = r.GetAllRoutesByMethods("foobar") if len(routes) != 0 { t.Fatal() } } func TestGetMethodsForPath(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "GET", Path: "/foo/bar", Code: testRoute}) r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "POST", Path: "/foo", Code: testRoute}) m := r.GetMethodsForPath("/foo") if len(m) != 3 { t.Fatal() } m = r.GetMethodsForPath("/foo/bar") if len(m) != 1 { t.Fatal() } m = r.GetMethodsForPath("/foo/bar/baz") if len(m) != 0 { t.Fatal() } } func TestWithOptions(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) r = r.AddOptions() m := r.GetMethodsForPath("/foo") if len(m) != 3 { t.Fatal() } } func TestAddNotAllowed(t *testing.T) { r := BuildRouter() r.AddRoute(&Route{Method: "GET", Path: "/foo", Code: testRoute}) r.AddRoute(&Route{Method: "PUT", Path: "/foo", Code: testRoute}) r = r.AddNotAllowed() m := r.GetMethodsForPath("/foo") if len(m) != 6 { t.Fatal() } }