A good way to do tests for beego router

Talk is cheap, just show you the code, take a look at this image


How does it work?
Any struct implmented ServeHTTP is a http.Handler,method ServeHTTP will be invoked when one request comes in, and it will decide which fucntion(or HandlerFunc) will be invoked for different router path;Here BeeApp.Handlers is one http.Handler,the implemented code can be found in beego/router.go

Here is one example of ServeHTTP implementaion

package main

import (

type Handler struct{}

var handlerFunc map[string]http.HandlerFunc

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("r.RequestURI: %+v\n", r.RequestURI)
	if hf, ok := handlerFunc[r.RequestURI]; ok {
		hf(w, r)
	fmt.Fprintln(w, "Hello, client, no data were found")

func init() {
	handlerFunc = make(map[string]http.HandlerFunc)
	handlerFunc["/home"] = func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, client,welcome to home")
	handlerFunc["/color/city"] = func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, client,color city")

func getCheck(url string) (ouput string) {
	res, err := http.Get(url)
	if err != nil {
	greeting, err := ioutil.ReadAll(res.Body)
	if err != nil {
	return string(greeting)

func main() {
	ts := httptest.NewServer(Handler{})
	defer ts.Close()
	listOfURI := []string{
	for _, v := range listOfURI {
		output := getCheck(ts.URL + v)
		fmt.Printf("output: %+v\n", output)