add router test
Showing
1 changed file
with
99 additions
and
0 deletions
router_test.go
0 → 100644
| 1 | package beego | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "io/ioutil" | ||
| 5 | "net/http" | ||
| 6 | "net/http/httptest" | ||
| 7 | "os" | ||
| 8 | "testing" | ||
| 9 | ) | ||
| 10 | |||
| 11 | type TestController struct { | ||
| 12 | Controller | ||
| 13 | } | ||
| 14 | |||
| 15 | func (this *TestController) Get() { | ||
| 16 | this.Data["Username"] = "astaxie" | ||
| 17 | this.Ctx.Output.Body([]byte("ok")) | ||
| 18 | } | ||
| 19 | |||
| 20 | func (this *TestController) List() { | ||
| 21 | this.Ctx.Output.Body([]byte("i am list")) | ||
| 22 | } | ||
| 23 | |||
| 24 | func TestUserFunc(t *testing.T) { | ||
| 25 | r, _ := http.NewRequest("GET", "/api/list", nil) | ||
| 26 | w := httptest.NewRecorder() | ||
| 27 | |||
| 28 | handler := NewControllerRegistor() | ||
| 29 | handler.Add("/api/list", &TestController{}, "*:List") | ||
| 30 | handler.ServeHTTP(w, r) | ||
| 31 | if w.Body.String() != "i am list" { | ||
| 32 | t.Errorf("user define func can't run") | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | func TestAutoFunc(t *testing.T) { | ||
| 37 | r, _ := http.NewRequest("GET", "/test/list", nil) | ||
| 38 | w := httptest.NewRecorder() | ||
| 39 | |||
| 40 | handler := NewControllerRegistor() | ||
| 41 | handler.AddAuto(&TestController{}) | ||
| 42 | handler.ServeHTTP(w, r) | ||
| 43 | if w.Body.String() != "i am list" { | ||
| 44 | t.Errorf("user define func can't run") | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | func TestRouteOk(t *testing.T) { | ||
| 49 | |||
| 50 | r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil) | ||
| 51 | w := httptest.NewRecorder() | ||
| 52 | |||
| 53 | handler := NewControllerRegistor() | ||
| 54 | handler.Add("/person/:last/:first", &TestController{}) | ||
| 55 | handler.ServeHTTP(w, r) | ||
| 56 | |||
| 57 | lastNameParam := r.URL.Query().Get(":last") | ||
| 58 | firstNameParam := r.URL.Query().Get(":first") | ||
| 59 | learnParam := r.URL.Query().Get("learn") | ||
| 60 | |||
| 61 | if lastNameParam != "anderson" { | ||
| 62 | t.Errorf("url param set to [%s]; want [%s]", lastNameParam, "anderson") | ||
| 63 | } | ||
| 64 | if firstNameParam != "thomas" { | ||
| 65 | t.Errorf("url param set to [%s]; want [%s]", firstNameParam, "thomas") | ||
| 66 | } | ||
| 67 | if learnParam != "kungfu" { | ||
| 68 | t.Errorf("url param set to [%s]; want [%s]", learnParam, "kungfu") | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | func TestNotFound(t *testing.T) { | ||
| 73 | r, _ := http.NewRequest("GET", "/", nil) | ||
| 74 | w := httptest.NewRecorder() | ||
| 75 | |||
| 76 | handler := NewControllerRegistor() | ||
| 77 | handler.ServeHTTP(w, r) | ||
| 78 | |||
| 79 | if w.Code != http.StatusNotFound { | ||
| 80 | t.Errorf("Code set to [%v]; want [%v]", w.Code, http.StatusNotFound) | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | // TestStatic tests the ability to serve static | ||
| 85 | // content from the filesystem | ||
| 86 | func TestStatic(t *testing.T) { | ||
| 87 | r, _ := http.NewRequest("GET", "/router_test.go", nil) | ||
| 88 | w := httptest.NewRecorder() | ||
| 89 | pwd, _ := os.Getwd() | ||
| 90 | |||
| 91 | handler := NewControllerRegistor() | ||
| 92 | SetStaticPath("/", pwd) | ||
| 93 | handler.ServeHTTP(w, r) | ||
| 94 | |||
| 95 | testFile, _ := ioutil.ReadFile(pwd + "/routes_test.go") | ||
| 96 | if w.Body.String() != string(testFile) { | ||
| 97 | t.Errorf("handler.Static failed to serve file") | ||
| 98 | } | ||
| 99 | } |
-
Please register or sign in to post a comment