Best Syzkaller code snippet using main.parsePattern
router.go
Source:router.go
...11type Router struct {12 roots map[string]*node13 handlers map[string]context.Handlers14}15func parsePattern(pattern string) []string {16 s := strings.Split(pattern, "/")17 parts := make([]string, 0)18 for _, part := range s {19 if part != "" {20 parts = append(parts, part)21 if part[0] == '*' {22 break23 }24 }25 }26 return parts27}28func (router *Router) AddRouter(method string, pattern string, handler ...context.Handler) {29 key := method + separator + pattern30 _, ok := router.roots[method]31 if !ok {32 router.roots[method] = &node{children: make([]*node, 0)}33 }34 parts := parsePattern(pattern)35 err := router.roots[method].insert(pattern, parts, 0)36 if err != nil {37 panic(err)38 }39 router.handlers[key] = append(router.handlers[key], handler...)40}41// æ¥æ¾è·¯ç±42func (router *Router) getRoute(method string, path string) (*node, map[string]string) {43 searchParts := parsePattern(path)44 params := make(map[string]string)45 root, ok := router.roots[method]46 if !ok {47 return nil, nil48 }49 node := root.search(path, searchParts, 0)50 if node == nil {51 return nil, nil52 }53 // 表示æ¾å°äºpath对åºçhandler54 // å°åæ°æ½ååºæ¥55 parts := parsePattern(node.pattern)56 for i, part := range parts {57 _, paramName := pathValid(part)58 if paramName != "" {59 if part[0] == '*' && len(part) > 1 {60 params[paramName] = strings.Join(searchParts[i:], "/")61 break62 }63 params[paramName] = searchParts[i]64 }65 }66 return node, params67}68func (router *Router) Serve(ctx *context.Context) {69 node, params := router.getRoute(ctx.Method, ctx.Path)...
main_test.go
Source:main_test.go
...20 r.addRoute("GET", "/assets/*filepath", nil)21 return r22}23func TestParsePattern(t *testing.T) {24 ok := reflect.DeepEqual(parsePattern("/p/:name"), []string{"p", ":name"})25 ok = ok && reflect.DeepEqual(parsePattern("/p/*"), []string{"p", "*"})26 ok = ok && reflect.DeepEqual(parsePattern("/p/*name/*"), []string{"p", "*name"})27 if !ok {28 t.Fatal("test parsePattern failed")29 }30}31func TestGetRoute(t *testing.T) {32 r := newTestRouter()33 n, ps := r.getRoute("GET", "/hello/geektutu")34 if n == nil {35 t.Fatal("nil shouldn't be returned")36 }37 if n.pattern != "/hello/:name" {38 t.Fatal("should match /hello/:name")39 }40 if ps["name"] != "geektutu" {41 t.Fatal("name should be equal to 'geektutu'")42 }...
test.go
Source:test.go
...9 r.addRoute("GET", "asset/*filepath", nil)10 return r11}12func TestParsePattern(t *testing.T) {13 ok := reflect.DeepEqual(parsePattern("/p/:name"), []string{"p", ":name"})14 ok = ok && reflect.DeepEqual(parsePattern("/p/*"), []string{"p", "*"})15 ok = ok && reflect.DeepEqual(parsePattern("p/*name/*"), []string{"p", "*name"})16 if !ok {17 t.Fatal("test parsePattern failed")18 }19}20func TestGetRoute(t *testing.T) {21 r := newTestRouter()22 n, ps := r.getRoute("GET", "/hello/geektutu")23 if n == nil {24 t.Fatal("nil shouldn't be returned")25 }26 if n.pattern != "hello/:name" {27 t.Fatal("should match /hello/:name")28 }29 if ps["name"] != "geektutu" {30 t.Fatal("name should be equal to 'geektutu'")31 }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!