/src/test/RouterTest.fan
Unknown | 98 lines | 85 code | 13 blank | 0 comment | 0 complexity | df40ec1bf0cd810d9af0a0ca4d0ee096 MD5 | raw file
1// 2// Copyright (c) 2011, Andy Frank 3// Licensed under the MIT License 4// 5// History: 6// 14 May 2011 Andy Frank Creation 7// 8 9using web 10 11** 12** Test code for Router class. 13** 14internal class RouterTest : Test 15{ 16 Void testRoutes() 17 { 18// TODO FIXIT - {index} arguments 19 20 r := Route("/foo/bar", "GET", #foo) 21 verifyEq(r.match(`/foo/bar`, "GET"), Str:Str[:]) 22 verifyEq(r.match(`/foo/bar`, "POST"), null) 23 verifyEq(r.match(`/foo/bar`, "HEAD"), null) 24 verifyEq(r.match(`/yo`, "GET"), null) 25 verifyEq(r.match(`/foo`, "GET"), null) 26 verifyEq(r.match(`/foo/`, "GET"), null) 27 verifyEq(r.match(`/foo/ba`, "GET"), null) 28// TODO: should this match? 29// verifyEq(r.match(`/foo/bar/`, "GET"), null) 30 31 r = Route("/foo/{arg}", "GET", #foo) 32 verifyEq(r.match(`/foo/123`, "GET"), Str:Str["arg":"123"]) 33 verifyEq(r.match(`/foo/abc`, "GET"), Str:Str["arg":"abc"]) 34 verifyEq(r.match(`/foo/ax9`, "GET"), Str:Str["arg":"ax9"]) 35 verifyEq(r.match(`/foo/_4b`, "GET"), Str:Str["arg":"_4b"]) 36 37 r = Route("/foo/{a}/bar/{b}/list", "GET", #foo) 38 verifyEq(r.match(`/foo/123/bar/abc/list`, "GET"), Str:Str["a":"123", "b":"abc"]) 39 verifyEq(r.match(`/foo/123/bax/abc/list`, "GET"), null) 40 41 // vararg matches 42 r = Route("/foo/*", "GET", #foo) 43 verifyEq(r.match(`/foo/x/y/z`, "GET"), Str:Str[:]) 44 verifyEq(r.match(`/fox/x/y/z`, "GET"), null) 45 46 r = Route("/foo/{bar}/*", "GET", #foo) 47 verifyEq(r.match(`/foo/x/y/z`, "GET"), Str:Str["bar":"x"]) 48 verifyEq(r.match(`/fox/x/y/z`, "GET"), null) 49 50 r = Route("/x/y/z/*", "GET", #foo) 51 verifyEq(r.match(`/x`, "GET"), null) 52 verifyEq(r.match(`/x/y`, "GET"), null) 53 verifyEq(r.match(`/x/y/z`, "GET"), null) 54 verifyEq(r.match(`/x/y/z/foo`, "GET"), Str:Str[:]) 55 verifyEq(r.match(`/x/y/z/foo/a/b/c`, "GET"), Str:Str[:]) 56 57 // errs 58// should err? 59// verifyErr(ArgErr#) { x := Route("foo", "GET", #foo) } 60 verifyErr(ArgErr#) { x := Route("/foo/*/*", "GET", #foo) } 61 verifyErr(ArgErr#) { x := Route("/*/foo", "GET", #foo) } 62// allowed? 63// verifyErr(ArgErr#) { x := Route("/bar/", "GET", #foo) } 64 } 65 66 Void testRouter() 67 { 68 test := Router { 69 routes = [ 70 Route("/", "GET", #index), 71 Route("/foo", "GET", #foo), 72 Route("/foo/bar", "GET", #bar), 73 Route("/foo/{arg}", "GET", #fooArg), 74 ] 75 } 76 77 verifyRouter(test, `/foo`, #foo, Str:Str[:]) 78// TODO FIXIT: this needs to work 79// maybe this is illegal - can't end in / - expect for "/" 80// verifyRouter(test, `/foo/`, #fooIndex, Str:Str[:]) 81 verifyRouter(test, `/foo/bar`, #bar, Str:Str[:]) 82 verifyRouter(test, `/foo/xyz`, #fooArg, Str:Str["arg":"xyz"]) 83 } 84 85 private Void verifyRouter(Router r, Uri uri, Method? handler, [Str:Str]? args) 86 { 87 m := r.match(uri, "GET") 88 verifyEq(m?.route?.handler, handler) 89 verifyEq(m?.args, args) 90 } 91 92 // dummy handlers 93 private Void index() {} 94 private Void foo() {} 95 private Void bar() {} 96// private Void fooIndex() {} 97 private Void fooArg() {} 98}