func Test_APITokenOnly(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/", APITokenOnly(), wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { res.WriteString("OK") }), ) req := ts.GET("/") res := req.RouteTo(router) assert.HTTPStatus(401, res) req = ts.GET("/") req.Request.User = &request.APITokenUser{ &models.APIToken{ Token: "testuser", Description: "test api token user", CreatedAt: time.Now(), AlertOn: time.Duration(0), LastAccess: time.Now(), }, } res = req.RouteTo(router) assert.HTTPStatus(200, res) }
func Test_ParseForm(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.POST("/*", ParseForm(func(v *validators.FormValidator) { v.IntField("foo").Required() }), wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { res.WriteString("OK") }), ) req := ts.POSTForm("/api/", nil) res := req.RouteTo(router) var got map[string]interface{} assert.HTTPStatus(400, res) assert.JSONResponse(&got, res) assert.OK(len(got["fields"].(map[string]interface{})) > 0, "'fields' field") req = ts.POSTForm("/api/", url.Values{ "foo": []string{"a"}, }) res = req.RouteTo(router) assert.HTTPStatus(200, res) }
func Test_EntityQuery(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityQuery(testEntity.Query().Limit(2)), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "5", "ID": "5"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "6", "ID": "6"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "7", "ID": "7"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "8", "ID": "8"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "9", "ID": "9"}, }) var got entities.Pagination req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqInt(2, got.Length()) assert.EqInt(2, got.Next.N) assert.EqInt(1, got.Next.P) assert.Nil(got.Previous) // TODO: got does not has []TEntity, but has []map[string]interface{}. // Need to provide a way to make got have []TEntity or []*TEntity }
func Test_EntityStreaming(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityStreaming(testEntity.Query(), false), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"}, }) req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.HTTPBodyString( `{"id":"1","value":0,"updated_at":"0001-01-01T00:00:00Z"} {"id":"2","value":0,"updated_at":"0001-01-01T00:00:00Z"} {"id":"3","value":0,"updated_at":"0001-01-01T00:00:00Z"} {"id":"4","value":0,"updated_at":"0001-01-01T00:00:00Z"} `, res, ) }
func Test_Session(t *testing.T) { assert := gaetest.NewAssert(t) handler := wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { if req.Session == nil { res.WriteString("No Session") } else { res.WriteString("Has Session") } }) router := wcg.NewRouter() Use(router, func() { router.GET("/api/foo", handler) router.GET("/static/foo", handler) router.GET("/cron/foo", handler) }) req := ts.GET("/api/foo") assert.EqStr("Has Session", string(req.RouteTo(router).Body), "Has Session") req = ts.GET("/api/foo") req.AddHeader(request.APITokenHeader, "FOO") assert.EqStr("No Session", string(req.RouteTo(router).Body), "No Session") req = ts.GET("/api/foo") req.AddHeader(request.TaskQueueHeader, "true") assert.EqStr("No Session", string(req.RouteTo(router).Body), "No Session") req = ts.GET("/static/foo") assert.EqStr("No Session", string(req.RouteTo(router).Body), "No Session") req = ts.GET("/cron/foo") req.AddHeader(request.CronHeader, "true") assert.EqStr("No Session", string(req.RouteTo(router).Body), "No Session") }
func Test_EntityPut(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.PUT("/api/:key.json", ParseForm(nil), EntityPut(testEntity.Put(), "key"), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, }) var got TEntity req := ts.PUTForm("/api/1.json", url.Values{ "value": []string{"1"}, }) res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqStr("1", got.ID) assert.EqInt(1, got.Value) _, one := testEntity.Get().Key(got.ID).MustOne(ts.GET("/").Request) assert.EqStr(got.ID, one.(*TEntity).ID) assert.EqInt(1, one.(*TEntity).Value) }
func Test_ByHeader(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_HeaderAuth.json", nil), "fixture") router := wcg.NewRouter() handler := wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { res.WriteString(req.User.ID()) }) byHeader(router, func() { router.GET("/api/foo", handler) router.GET("/cron/foo", handler) }) req := ts.GET("/api/foo") assert.EqStr("-", string(req.RouteTo(router).Body), "Guest") req = ts.GET("/api/foo") req.AddHeader(request.APITokenHeader, "2e489f08-3be9-4d28-af86-b14841b2181a") assert.EqStr("api:2e489f08-3be9-4d28-af86-b14841b2181a", string(req.RouteTo(router).Body), "APITokenUser") req = ts.GET("/api/foo") req.AddHeader(request.APITokenHeader, "invalidtoken") assert.EqStr("-", string(req.RouteTo(router).Body), "Guest") req = ts.GET("/api/foo") req.AddHeader(request.TaskQueueHeader, "true") assert.EqStr(fmt.Sprintf("api:%s", request.APITokenForTask), string(req.RouteTo(router).Body), "Guest") req = ts.GET("/cron/foo") req.AddHeader(request.CronHeader, "true") assert.EqStr(fmt.Sprintf("api:%s", request.APITokenForCron), string(req.RouteTo(router).Body), "CronUser") }
// Handle to test handler func (r *TestRequest) Handle(funcs ...func(*wcg.Response, *wcg.Request)) *TestResponse { router := wcg.NewRouter() for _, fun := range funcs { router.All("/*", wcg.AnonymousHandler(fun)) } return r.RouteTo(router) }
// NewApp creates a new *App under the given key path. func NewApp(key string) *App { app := &App{} app.Key = key app.routes = wcg.NewRouter() app.crons = make([]*Cron, 0) app.pushQueues = make([]*PushQueue, 0) return app }
func Test_EntityGet_NotFound(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/:key.json", EntityGet(testEntity.Get(), "key"), ) gaetest.CleanupDatastore(ts.Context) req := ts.GET("/1.json") res := req.RouteTo(router) assert.HTTPStatus(404, res) }
func Test_PageResponse_GetTemplates(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/*", wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { pr := NewPageResponseWithStatus("test", 200) res.WriteString(pr.GetTemplates(req)[0]) res.Close() })) assert.EqStr("default.html", string(ts.GET("/foo").RouteTo(router).Body), "index.html") assert.EqStr("./testapp/a.html", string(ts.GET("/testapp/a.html").RouteTo(router).Body), "/testapp/a.html") }
func Test_EntityAll_Empty(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityAll(testEntity.Query()), ) gaetest.CleanupDatastore(ts.Context) req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.HTTPBodyString("[]", res) }
func Test_EntityQuery_Empty(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityQuery(testEntity.Query().Limit(2)), ) gaetest.CleanupDatastore(ts.Context) var got entities.Pagination req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqInt(0, got.Length()) }
func Test_IsUserOptIn(t *testing.T) { assert := gaetest.NewAssert(t) assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_IsUserOptIn.json", nil), "fixture") router := wcg.NewRouter() router.GET("/test_is_user_opt_in", wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { assert.OK(IsUserOptIn(req, "12345", "67890"), "IsUserOptIn should return true.") assert.OK(!IsUserOptIn(req, "aaaa", "aaaa"), "IsUserOptIn should return false.") res.WriteString("OK") })) req := ts.GET("/test_is_user_opt_in") res := req.RouteTo(router) assert.HTTPStatus(200, res) }
func Test_IsAPI(t *testing.T) { assert := gaetest.NewAssert(t) handler := wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { if IsAPI(req) { res.WriteString("IsAPI") } else { res.WriteString("Not IsAPI") } }) router := wcg.NewRouter() router.GET("/api/foo", handler) router.GET("/foo", handler) assert.EqStr("IsAPI", string(ts.GET("/api/foo").RouteTo(router).Body), "IsAPI") assert.EqStr("Not IsAPI", string(ts.GET("/foo").RouteTo(router).Body), "Not IsAPI") }
func Test_EntityPut_NotFound(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.PUT("/api/:key.json", ParseForm(nil), EntityPut(testEntity.Put(), "key"), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, }) req := ts.PUTForm("/api/2.json", url.Values{ "value": []string{"1"}, }) res := req.RouteTo(router) assert.HTTPStatus(404, res) }
func Test_EntityDelete(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/:key.json", EntityDelete(testEntity.Delete(), "key"), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, }) req := ts.GET("/api/1.json") res := req.RouteTo(router) assert.HTTPStatus(200, res) _, obj := testEntity.Get().Key("1").MustOne(ts.GET("/").Request) assert.Nil(obj) }
func Test_Gate(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/", Gate("foo"), wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { res.WriteString("OK") }), ) req := ts.GET("/") res := req.RouteTo(router) assert.HTTPStatus(401, res) req = ts.GET("/") testhelper.TemporaryAllow(req, "foo", func() { res := req.RouteTo(router) assert.HTTPStatus(200, res) }) }
func Test_EntityPost_BadRequest(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.POST("/api/", ParseForm(nil), EntityPost(testEntity.Put()), ) gaetest.CleanupDatastore(ts.Context) var err map[string]string req := ts.POSTForm("/api/", url.Values{ "value": []string{"a"}, }) res := req.RouteTo(router) assert.HTTPStatus(400, res) assert.JSONResponse(&err, res) assert.EqStr("bad_request", err["error"]) assert.EqStr(ErrInvalidFormParameters.Error(), err["message"]) }
func Test_RequireMessengerOptIn(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/", RequireMessengerOptIn(""), wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { res.WriteString("OK") }), ) req := ts.GET("/") res := req.RouteTo(router) assert.HTTPStatus(403, res) configs.TemporarySetConfig(ts.GET("dummy").Request, "facebook_page_id", "1234", func() { messenger.TemporaryOptIn(ts.GET("dummy").Request, "1234", wcg.GuestUser.ID(), func() { req := ts.GET("/") res := req.RouteTo(router) assert.HTTPStatus(200, res) }) }) }
func Test_EntityTail(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/", EntityTail(testEntity.Query()), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "2", "ID": "2"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "3", "ID": "3"}, map[string]interface{}{"_kind": testEntity.String(), "_key": "4", "ID": "4"}, }) var got TEntity req := ts.GET("/api/") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqStr("4", got.ID) }
func Test_EntityPut_BadRequest(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.PUT("/api/:key.json", ParseForm(nil), EntityPut(testEntity.Put(), "key"), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, }) var got map[string]string req := ts.PUTForm("/api/1.json", url.Values{ "value": []string{"a"}, }) res := req.RouteTo(router) assert.HTTPStatus(400, res) assert.JSONResponse(&got, res) assert.EqStr("bad_request", got["error"]) assert.EqStr(ErrInvalidFormParameters.Error(), got["message"]) }
func Test_EntityGet(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.GET("/api/:key.json", EntityGet(testEntity.Get(), "key"), ) gaetest.CleanupDatastore(ts.Context) gaetest.FixtureFromMap(ts.Context, []map[string]interface{}{ map[string]interface{}{"_kind": testEntity.String(), "_key": "1", "ID": "1"}, }) var got TEntity req := ts.GET("/api/1.json") res := req.RouteTo(router) assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.EqStr("1", got.ID) req = ts.GET("/2.json") res = req.RouteTo(router) assert.HTTPStatus(404, res) }
func Test_Parallel(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() funA := server.Handler(func(req *wcg.Request) response.Response { return response.NewJSONResponse(true) }) funB := server.Handler(func(req *wcg.Request) response.Response { return response.NewJSONResponse(false) }) router.GET("/*", Parallel(map[string]server.Handler{ "a": funA, "b": funB, })) req := ts.GET("/a/") res := req.RouteTo(router) var got map[string]bool assert.HTTPStatus(200, res) assert.JSONResponse(&got, res) assert.OK(got["a"]) assert.Not(got["b"]) }
func Test_IsTask(t *testing.T) { assert := gaetest.NewAssert(t) handler := wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) { if IsTask(req) { res.WriteString("IsTask") } else { res.WriteString("Not IsTask") } }) router := wcg.NewRouter() router.GET("/api/foo", handler) router.GET("/cron/foo", handler) assert.EqStr("Not IsTask", string(ts.GET("/api/foo").RouteTo(router).Body), "Not IsTask") assert.EqStr("Not IsTask", string(ts.GET("/cron/foo").RouteTo(router).Body), "Not IsTask") req := ts.GET("/cron/foo") req.AddHeader(TaskQueueHeader, "true") assert.EqStr("IsTask", string(req.RouteTo(router).Body), "IsTask") req = ts.GET("/api/foo") assert.EqStr("Not IsTask", string(req.RouteTo(router).Body), "Not IsTask") }
func Test_EntityPost(t *testing.T) { assert := gaetest.NewAssert(t) router := wcg.NewRouter() router.POST("/api/", ParseForm(nil), EntityPost(testEntity.Put()), ) gaetest.CleanupDatastore(ts.Context) var got TEntity req := ts.POSTForm("/api/", url.Values{ "value": []string{"1"}, }) res := req.RouteTo(router) assert.HTTPStatus(201, res) assert.JSONResponse(&got, res) assert.OK("" != got.ID, "ID must be assigned in response") assert.EqInt(1, got.Value) _, one := testEntity.Get().Key(got.ID).MustOne(ts.GET("/").Request) assert.EqStr(got.ID, one.(*TEntity).ID) assert.EqInt(1, one.(*TEntity).Value) }
// NewServer creates a new GAE server object func NewServer() wcg.Server { s := new(Server) s.routes = wcg.NewRouter() return s }