From 285e130505144498fbbd5ff99210b2bb22e67e00 Mon Sep 17 00:00:00 2001 From: fanngyuan Date: Tue, 27 Aug 2013 22:35:48 +0800 Subject: [PATCH] add template --- apiapp.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/apiapp.go b/apiapp.go index 18b1a91..175e59f 100644 --- a/apiapp.go +++ b/apiapp.go @@ -54,6 +54,7 @@ import ( func main() { beego.RESTRouter("/object", &controllers.ObejctController{}) + beego.Router("/ping", &controllers.ObejctController{},"get:Ping") beego.Run() } ` @@ -170,8 +171,33 @@ func (this *ObejctController) Delete() { this.Data["json"] = "delete success!" this.ServeJson() } + +func (this *ObejctController) Ping() { + this.Ctx.WriteString("pong") +} + ` +var apiTests = `package tests + +import ( + "testing" + beetest "github.com/fanngyuan/beego/testing" + "io/ioutil" +) + +func TestHelloWorld(t *testing.T) { + request:=beetest.Get("/ping") + response,_:=request.Response() + defer response.Body.Close() + contents, _ := ioutil.ReadAll(response.Body) + if string(contents)!="pong"{ + t.Errorf("response sould be pong") + } +} + +` + func init() { cmdApiapp.Run = createapi } @@ -205,6 +231,10 @@ func createapi(cmd *Command, args []string) { writetofile(path.Join(apppath, "controllers", "default.go"), strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) + fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) + writetofile(path.Join(apppath, "tests", "default_test.go"), + apiTests) + fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) writetofile(path.Join(apppath, "models", "object.go"), apiModels)