9d84969b by astaxie

fix #153

已经支持了任意定义变量的路由形式,具体的使用请参考:

func TestManyRoute(t *testing.T) {

	r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
	w := httptest.NewRecorder()

	handler := NewControllerRegistor()
	handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{})
	handler.ServeHTTP(w, r)

	id := r.URL.Query().Get(":id")
	page := r.URL.Query().Get(":page")

	if id != "32" {
		t.Errorf("url param set to [%s]; want [%s]", id, "32")
	}
	if page != "12" {
		t.Errorf("url param set to [%s]; want [%s]", page, "12")
	}
}
1 parent 3745bb72
...@@ -94,6 +94,47 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -94,6 +94,47 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
94 j++ 94 j++
95 } 95 }
96 } 96 }
97 //url like someprefix:id(xxx).html
98 if strings.Contains(part, ":") && strings.Contains(part, "(") && strings.Contains(part, ")") {
99 var out []rune
100 var start bool
101 var startexp bool
102 var param []rune
103 var expt []rune
104 for _, v := range part {
105 if start {
106 if v != '(' {
107 param = append(param, v)
108 continue
109 }
110 }
111 if startexp {
112 if v != ')' {
113 expt = append(expt, v)
114 continue
115 }
116 }
117 if v == ':' {
118 param = make([]rune, 0)
119 param = append(param, ':')
120 start = true
121 } else if v == '(' {
122 startexp = true
123 start = false
124 params[j] = string(param)
125 j++
126 expt = make([]rune, 0)
127 expt = append(expt, '(')
128 } else if v == ')' {
129 startexp = false
130 expt = append(expt, ')')
131 out = append(out, expt...)
132 } else {
133 out = append(out, v)
134 }
135 }
136 parts[i] = string(out)
137 }
97 } 138 }
98 reflectVal := reflect.ValueOf(c) 139 reflectVal := reflect.ValueOf(c)
99 t := reflect.Indirect(reflectVal).Type() 140 t := reflect.Indirect(reflectVal).Type()
......
...@@ -69,6 +69,26 @@ func TestRouteOk(t *testing.T) { ...@@ -69,6 +69,26 @@ func TestRouteOk(t *testing.T) {
69 } 69 }
70 } 70 }
71 71
72 func TestManyRoute(t *testing.T) {
73
74 r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
75 w := httptest.NewRecorder()
76
77 handler := NewControllerRegistor()
78 handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{})
79 handler.ServeHTTP(w, r)
80
81 id := r.URL.Query().Get(":id")
82 page := r.URL.Query().Get(":page")
83
84 if id != "32" {
85 t.Errorf("url param set to [%s]; want [%s]", id, "32")
86 }
87 if page != "12" {
88 t.Errorf("url param set to [%s]; want [%s]", page, "12")
89 }
90 }
91
72 func TestNotFound(t *testing.T) { 92 func TestNotFound(t *testing.T) {
73 r, _ := http.NewRequest("GET", "/", nil) 93 r, _ := http.NewRequest("GET", "/", nil)
74 w := httptest.NewRecorder() 94 w := httptest.NewRecorder()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!