當前位置: 首頁>>代碼示例>>Golang>>正文


Golang wcg.NewRouter函數代碼示例

本文整理匯總了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)
}
開發者ID:speedland,項目名稱:service,代碼行數:26,代碼來源:access_control_test.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:25,代碼來源:parse_form_test.go

示例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
}
開發者ID:speedland,項目名稱:service,代碼行數:31,代碼來源:entity_test.go

示例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,
	)
}
開發者ID:speedland,項目名稱:service,代碼行數:25,代碼來源:entity_test.go

示例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")
}
開發者ID:speedland,項目名稱:service,代碼行數:35,代碼來源:session_test.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:26,代碼來源:entity_test.go

示例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")

}
開發者ID:speedland,項目名稱:service,代碼行數:32,代碼來源:header_test.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:8,代碼來源:httptest.go

示例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
}
開發者ID:speedland,項目名稱:service,代碼行數:9,代碼來源:app.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:12,代碼來源:entity_test.go

示例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")
}
開發者ID:speedland,項目名稱:service,代碼行數:12,代碼來源:page_test.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:13,代碼來源:entity_test.go

示例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())
}
開發者ID:speedland,項目名稱:service,代碼行數:15,代碼來源:entity_test.go

示例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)
}
開發者ID:speedland,項目名稱:service,代碼行數:15,代碼來源:auth_test.go

示例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")
}
開發者ID:speedland,項目名稱:service,代碼行數:16,代碼來源:api_test.go


注:本文中的lib/wcg.NewRouter函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。