From 03080b3ef280c2e8ada5436d670dcc0772dd2a5b Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 20 May 2014 15:53:41 +0800 Subject: [PATCH 1/3] beego:1.2.0 --- router.go | 5 ----- router_test.go | 39 +++++++++++++++++---------------------- utils/file.go | 12 +++++++++--- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/router.go b/router.go index 9220234f..4291b81f 100644 --- a/router.go +++ b/router.go @@ -797,14 +797,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } if len(route.params) > 0 { - //add url parameters to the query param map - values := r.URL.Query() for i, match := range matches[1:] { - values.Add(route.params[i], match) params[route.params[i]] = match } - //reassemble query params and add to RawQuery - r.URL.RawQuery = url.Values(values).Encode() } runMethod = p.getRunMethod(r.Method, context, route) if runMethod != "" { diff --git a/router_test.go b/router_test.go index a5e1a23e..8c9e69b2 100644 --- a/router_test.go +++ b/router_test.go @@ -43,6 +43,15 @@ func (this *TestController) GetUrl() { this.Ctx.Output.Body([]byte(this.UrlFor(".Myext"))) } +func (t *TestController) GetParams() { + t.Ctx.WriteString(t.Ctx.Input.Query(":last") + "+" + + t.Ctx.Input.Query(":first") + "+" + t.Ctx.Input.Query("learn")) +} + +func (t *TestController) GetManyRouter() { + t.Ctx.WriteString(t.Ctx.Input.Query(":id") + t.Ctx.Input.Query(":page")) +} + type ResStatus struct { Code int Msg string @@ -147,21 +156,11 @@ func TestRouteOk(t *testing.T) { w := httptest.NewRecorder() handler := NewControllerRegistor() - handler.Add("/person/:last/:first", &TestController{}) + handler.Add("/person/:last/:first", &TestController{}, "get:GetParams") handler.ServeHTTP(w, r) - - lastNameParam := r.URL.Query().Get(":last") - firstNameParam := r.URL.Query().Get(":first") - learnParam := r.URL.Query().Get("learn") - - if lastNameParam != "anderson" { - t.Errorf("url param set to [%s]; want [%s]", lastNameParam, "anderson") - } - if firstNameParam != "thomas" { - t.Errorf("url param set to [%s]; want [%s]", firstNameParam, "thomas") - } - if learnParam != "kungfu" { - t.Errorf("url param set to [%s]; want [%s]", learnParam, "kungfu") + body := w.Body.String() + if body != "anderson+thomas+kungfu" { + t.Errorf("url param set to [%s];", body) } } @@ -171,17 +170,13 @@ func TestManyRoute(t *testing.T) { w := httptest.NewRecorder() handler := NewControllerRegistor() - handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}) + handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}, "get:GetManyRouter") handler.ServeHTTP(w, r) - id := r.URL.Query().Get(":id") - page := r.URL.Query().Get(":page") + body := w.Body.String() - 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") + if body != "3212" { + t.Errorf("url param set to [%s];", body) } } diff --git a/utils/file.go b/utils/file.go index 0f08045b..5246d88c 100644 --- a/utils/file.go +++ b/utils/file.go @@ -64,24 +64,30 @@ func GrepFile(patten string, filename string) (lines []string, err error) { lines = make([]string, 0) reader := bufio.NewReader(fd) prefix := "" + isLongLine := false for { byteLine, isPrefix, er := reader.ReadLine() if er != nil && er != io.EOF { return nil, er } + if er == io.EOF { + break + } line := string(byteLine) if isPrefix { prefix += line continue + } else { + isLongLine = true } line = prefix + line + if isLongLine { + prefix = "" + } if re.MatchString(line) { lines = append(lines, line) } - if er == io.EOF { - break - } } return lines, nil } From 61008fe75c74565a1bb4d017bbd81e0f466a7120 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 May 2014 14:12:21 -0500 Subject: [PATCH 2/3] udpated timezone in templatefunc_test. changed error message to be more descriptive when tests fail --- templatefunc_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/templatefunc_test.go b/templatefunc_test.go index aa1ba1ed..488a9154 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -36,25 +36,27 @@ func TestHtml2str(t *testing.T) { func TestDateFormat(t *testing.T) { ts := "Mon, 01 Jul 2013 13:27:42 CST" tt, _ := time.Parse(time.RFC1123, ts) - if DateFormat(tt, "2006-01-02 15:04:05") != "2013-07-01 13:27:42" { - t.Error("should be equal") + + if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 14:27:42" { + t.Errorf("2013-07-01 14:27:42 does not equal %v", ss) } } func TestDate(t *testing.T) { ts := "Mon, 01 Jul 2013 13:27:42 CST" tt, _ := time.Parse(time.RFC1123, ts) - if Date(tt, "Y-m-d H:i:s") != "2013-07-01 13:27:42" { - t.Error("should be equal") + + if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 14:27:42" { + t.Errorf("2013-07-01 14:27:42 does not equal %v", ss) } - if Date(tt, "y-n-j h:i:s A") != "13-7-1 01:27:42 PM" { - t.Error("should be equal") + if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 02:27:42 PM" { + t.Errorf("13-7-1 02:27:42 PM does not equal %v", ss) } - if Date(tt, "D, d M Y g:i:s a") != "Mon, 01 Jul 2013 1:27:42 pm" { - t.Error("should be equal") + if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 2:27:42 pm" { + t.Errorf("Mon, 01 Jul 2013 2:27:42 pm does not equal %v", ss) } - if Date(tt, "l, d F Y G:i:s") != "Monday, 01 July 2013 13:27:42" { - t.Error("should be equal") + if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 14:27:42" { + t.Errorf("Monday, 01 July 2013 14:27:42 does not equal %v", ss) } } From a673a85d4afaf1166152c0e6c5b68dfe580a5d2e Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 May 2014 23:48:23 -0500 Subject: [PATCH 3/3] added tests config/json_test that test missing key usecases. created a template function to fetch AppConfig values --- config/json_test.go | 24 ++++++++++++++++++++++++ template.go | 1 + templatefunc.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/config/json_test.go b/config/json_test.go index 4e83470f..e0046084 100644 --- a/config/json_test.go +++ b/config/json_test.go @@ -100,4 +100,28 @@ func TestJson(t *testing.T) { t.Fatal("get host err") } } + + if _, err := jsonconf.Int("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int") + } + + if _, err := jsonconf.Int64("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int64") + } + + if _, err := jsonconf.Float("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Float") + } + + if _, err := jsonconf.DIY("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an interface{}") + } + + if val := jsonconf.String("unknown"); val != "" { + t.Error("unknown keys should return an empty string when expecting a String") + } + + if _, err := jsonconf.Bool("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Bool") + } } diff --git a/template.go b/template.go index 95deb443..9411930c 100644 --- a/template.go +++ b/template.go @@ -44,6 +44,7 @@ func init() { beegoTplFuncMap["renderform"] = RenderForm beegoTplFuncMap["assets_js"] = AssetsJs beegoTplFuncMap["assets_css"] = AssetsCss + beegoTplFuncMap["config"] = Config // go1.2 added template funcs // Comparisons diff --git a/templatefunc.go b/templatefunc.go index 62584781..f89d12e3 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -131,6 +131,43 @@ func Compare(a, b interface{}) (equal bool) { return } +func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) { + switch returnType { + case "String": + value = AppConfig.String(key) + case "Bool": + value, err = AppConfig.Bool(key) + case "Int": + value, err = AppConfig.Int(key) + case "Int64": + value, err = AppConfig.Int64(key) + case "Float": + value, err = AppConfig.Float(key) + case "DIY": + value, err = AppConfig.DIY(key) + default: + err = errors.New("Config keys must be of type String, Bool, Int, Int64, Float, or DIY!") + } + + if err != nil { + if reflect.TypeOf(returnType) != reflect.TypeOf(defaultVal) { + err = errors.New("defaultVal type does not match returnType!") + } else { + value, err = defaultVal, nil + } + } else if reflect.TypeOf(value).Kind() == reflect.String { + if value == "" { + if reflect.TypeOf(defaultVal).Kind() != reflect.String { + err = errors.New("defaultVal type must be a String if the returnType is a String") + } else { + value = defaultVal.(string) + } + } + } + + return +} + // Convert string to template.HTML type. func Str2html(raw string) template.HTML { return template.HTML(raw)