当前位置: 首页>>代码示例>>Golang>>正文


Golang Router.HandleFunc方法代码示例

本文整理汇总了Golang中github.com/taironas/route.Router.HandleFunc方法的典型用法代码示例。如果您正苦于以下问题:Golang Router.HandleFunc方法的具体用法?Golang Router.HandleFunc怎么用?Golang Router.HandleFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/taironas/route.Router的用法示例。


在下文中一共展示了Router.HandleFunc方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestSpaceInvadersCache

func TestSpaceInvadersCache(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/spaceinvaders/:key", SpaceInvaders)
	var etag string
	test := tgTesting.GenerateHandlerFunc(t, SpaceInvaders)

	if recorder := test("/spaceinvaders/somekey", "GET", nil, r); recorder != nil {
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
		etag = recorder.Header().Get("Etag")
	}

	// test caching
	if req, err := http.NewRequest("GET", "/spaceinvaders/somekey", nil); err != nil {
		t.Errorf("%v", err)
	} else {
		req.Header.Set("If-None-Match", etag)
		recorder := httptest.NewRecorder()
		r.ServeHTTP(recorder, req)
		if recorder.Code != http.StatusNotModified {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusNotModified)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:26,代码来源:spaceinvaders_test.go

示例2: TestHexa16

func TestHexa16(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/isogrids/labs/hexa16", Hexa)
	r.HandleFunc("/isogrids/labs/hexa16/:key", Hexa)

	test := tgTesting.GenerateHandlerFunc(t, Hexa)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/isogrids/labs/hexa16", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
		recorder = test("/isogrids/labs/hexa16/somekey", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}

	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/isogrids/labs/hexa16", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
		recorder = test("/isogrids/labs/hexa16/somekey", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}

	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:31,代码来源:hexa16_test.go

示例3: TestTheme

func TestTheme(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/themes", Theme)
	r.HandleFunc("/themes/:key", Theme)

	test := tgTesting.GenerateHandlerFunc(t, Theme)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/themes", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
		recorder = test("/themes/somekey", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/themes", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
		recorder = test("/themes/somekey", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:29,代码来源:themes_test.go

示例4: TestGenerateHandlerFunc

func TestGenerateHandlerFunc(t *testing.T) {
	t.Parallel()
	handler := func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "hello, world")
	}

	r := new(route.Router)
	r.HandleFunc("/test", handler)

	test := GenerateHandlerFunc(t, handler)

	recorder := test("/test", "GET", map[string]string{}, r)
	if recorder.Code != http.StatusOK {
		t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:16,代码来源:testing_test.go

示例5: TestBannerRandomGradient

func TestBannerRandomGradient(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/isogrids/banner/random/gradient", BannerRandomGradient)

	test := tgTesting.GenerateHandlerFunc(t, BannerRandomGradient)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/isogrids/banner/random/gradient", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/isogrids/banner/random/gradient", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:20,代码来源:banner_test.go

示例6: TestRandom

func TestRandom(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/squares/random", Random)

	test := tgTesting.GenerateHandlerFunc(t, Random)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/squares/random", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/squares/random", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:20,代码来源:random_test.go

示例7: TestIsogrids

func TestIsogrids(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/isogrids/:key", Isogrids)

	test := tgTesting.GenerateHandlerFunc(t, Isogrids)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/isogrids/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/isogrids/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:20,代码来源:isogrids_test.go

示例8: TestCheckerboard

func TestCheckerboard(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/checkerboard", Checkerboard)

	test := tgTesting.GenerateHandlerFunc(t, Checkerboard)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/checkerboard", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/checkerboard", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

}
开发者ID:insionng,项目名称:tinygraphs,代码行数:21,代码来源:checkerboard_test.go

示例9: TestSquares

func TestSquares(t *testing.T) {
	t.Parallel()
	r := new(route.Router)
	r.HandleFunc("/squares/:key", Square)

	test := tgTesting.GenerateHandlerFunc(t, Square)
	for _, p := range tgTesting.GoodParams {
		recorder := test("/squares/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	for _, p := range tgTesting.BadParams {
		recorder := test("/squares/test", "GET", p, r)
		if recorder.Code != http.StatusOK {
			t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
		}
	}

	// test caching:
	recorder := test("/squares/cache", "GET", nil, r)
	if recorder.Code != http.StatusOK {
		t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
	}

	if req, err := http.NewRequest("GET", "/squares/cache", nil); err != nil {
		t.Errorf("%v", err)
	} else {
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("If-None-Match", recorder.Header().Get("Etag"))
		recorder2 := httptest.NewRecorder()
		r.ServeHTTP(recorder2, req)
		if recorder2.Code != http.StatusNotModified {
			t.Errorf("returned %v. Expected %v.", recorder2.Code, http.StatusNotModified)
		}
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:38,代码来源:square_test.go

示例10: main

func main() {
	r := new(route.Router)

	r.HandleFunc("/squares", squares.Random)
	r.HandleFunc("/squares/banner/random", squares.BannerRandom)
	r.HandleFunc("/squares/banner/random/gradient", squares.BannerRandomGradient)
	r.HandleFunc("/squares/:key", squares.Square) //cached

	r.HandleFunc("/isogrids/banner/random", isogrids.BannerRandom)
	r.HandleFunc("/isogrids/banner/random/gradient", isogrids.BannerRandomGradient)

	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)
	r.HandleFunc("/spaceinvaders/:key", spaceinvaders.SpaceInvaders)

	r.HandleFunc("/themes/:theme", themes.Theme)
	r.HandleFunc("/labs/checkerboard", checkerboard.Checkerboard)
	r.HandleFunc("/labs/squares/random", squares.Random)
	r.HandleFunc("/labs/isogrids/hexa", isogrids.Hexa)
	r.HandleFunc("/labs/isogrids/hexa/:key", isogrids.Hexa)
	r.HandleFunc("/labs/isogrids/hexa16/:key", isogrids.Hexa16)
	r.HandleFunc("/labs/isogrids/skeleton", isogrids.Skeleton)
	r.HandleFunc("/labs/isogrids/diagonals", isogrids.Diagonals)
	r.HandleFunc("/labs/isogrids/halfdiagonals", isogrids.HalfDiagonals)
	r.HandleFunc("/labs/isogrids/random", isogrids.Random)
	r.HandleFunc("/labs/isogrids/random-mirror", isogrids.RandomMirror)

	r.HandleFunc("/labs/squares/banner/gradient", squares.BannerGradient)
	r.HandleFunc("/labs/isogrids/banner/gradient", isogrids.BannerGradient)

	r.AddStaticResource(root)

	log.Println("Listening on " + os.Getenv("PORT"))
	err := http.ListenAndServe(":"+os.Getenv("PORT"), r)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}
开发者ID:insionng,项目名称:tinygraphs,代码行数:37,代码来源:main.go


注:本文中的github.com/taironas/route.Router.HandleFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。