From 90e7d252a7ff9db0b49e9f9ec3a32d97bfe813f2 Mon Sep 17 00:00:00 2001 From: Gavin Fang Date: Thu, 10 Mar 2016 21:59:50 +0800 Subject: [PATCH] fix static pattern match for leaf --- tree.go | 2 +- tree_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tree.go b/tree.go index 594e9999..0601099c 100644 --- a/tree.go +++ b/tree.go @@ -389,7 +389,7 @@ type leafInfo struct { func (leaf *leafInfo) match(wildcardValues []string, ctx *context.Context) (ok bool) { //fmt.Println("Leaf:", wildcardValues, leaf.wildcards, leaf.regexps) if leaf.regexps == nil { - if len(wildcardValues) == 0 { // static path + if len(wildcardValues) == 0 && len(leaf.wildcards) == 0 { // static path return true } // match * diff --git a/tree_test.go b/tree_test.go index 531df046..81ff7edd 100644 --- a/tree_test.go +++ b/tree_test.go @@ -97,6 +97,21 @@ func TestTreeRouters(t *testing.T) { } } +func TestStaticPath(t *testing.T) { + tr := NewTree() + tr.AddRouter("/topic/:id", "wildcard") + tr.AddRouter("/topic", "static") + ctx := context.NewContext() + obj := tr.Match("/topic", ctx) + if obj == nil || obj.(string) != "static" { + t.Fatal("/topic is a static route") + } + obj = tr.Match("/topic/1", ctx) + if obj == nil || obj.(string) != "wildcard" { + t.Fatal("/topic/1 is a wildcard route") + } +} + func TestAddTree(t *testing.T) { tr := NewTree() tr.AddRouter("/shop/:id/account", "astaxie")