本文整理汇总了Golang中lib/wcg.NewRouter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRouter函数的具体用法?Golang NewRouter怎么用?Golang NewRouter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRouter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Test_APITokenOnly
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)
}
示例2: Test_ParseForm
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)
}
示例3: Test_EntityQuery
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
}
示例4: Test_EntityStreaming
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,
)
}
示例5: Test_Session
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")
}
示例6: Test_EntityPut
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)
}
示例7: Test_ByHeader
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")
}
示例8: Handle
// 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)
}
示例9: NewApp
// 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
}
示例10: Test_EntityGet_NotFound
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)
}
示例11: Test_PageResponse_GetTemplates
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")
}
示例12: Test_EntityAll_Empty
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)
}
示例13: Test_EntityQuery_Empty
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())
}
示例14: Test_IsUserOptIn
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)
}
示例15: Test_IsAPI
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")
}