namespace_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "strconv"
  19. "testing"
  20. "github.com/astaxie/beego/context"
  21. )
  22. func TestNamespaceGet(t *testing.T) {
  23. r, _ := http.NewRequest("GET", "/v1/user", nil)
  24. w := httptest.NewRecorder()
  25. ns := NewNamespace("/v1")
  26. ns.Get("/user", func(ctx *context.Context) {
  27. ctx.Output.Body([]byte("v1_user"))
  28. })
  29. AddNamespace(ns)
  30. BeeApp.Handlers.ServeHTTP(w, r)
  31. if w.Body.String() != "v1_user" {
  32. t.Errorf("TestNamespaceGet can't run, get the response is " + w.Body.String())
  33. }
  34. }
  35. func TestNamespacePost(t *testing.T) {
  36. r, _ := http.NewRequest("POST", "/v1/user/123", nil)
  37. w := httptest.NewRecorder()
  38. ns := NewNamespace("/v1")
  39. ns.Post("/user/:id", func(ctx *context.Context) {
  40. ctx.Output.Body([]byte(ctx.Input.Param(":id")))
  41. })
  42. AddNamespace(ns)
  43. BeeApp.Handlers.ServeHTTP(w, r)
  44. if w.Body.String() != "123" {
  45. t.Errorf("TestNamespacePost can't run, get the response is " + w.Body.String())
  46. }
  47. }
  48. func TestNamespaceNest(t *testing.T) {
  49. r, _ := http.NewRequest("GET", "/v1/admin/order", nil)
  50. w := httptest.NewRecorder()
  51. ns := NewNamespace("/v1")
  52. ns.Namespace(
  53. NewNamespace("/admin").
  54. Get("/order", func(ctx *context.Context) {
  55. ctx.Output.Body([]byte("order"))
  56. }),
  57. )
  58. AddNamespace(ns)
  59. BeeApp.Handlers.ServeHTTP(w, r)
  60. if w.Body.String() != "order" {
  61. t.Errorf("TestNamespaceNest can't run, get the response is " + w.Body.String())
  62. }
  63. }
  64. func TestNamespaceNestParam(t *testing.T) {
  65. r, _ := http.NewRequest("GET", "/v1/admin/order/123", nil)
  66. w := httptest.NewRecorder()
  67. ns := NewNamespace("/v1")
  68. ns.Namespace(
  69. NewNamespace("/admin").
  70. Get("/order/:id", func(ctx *context.Context) {
  71. ctx.Output.Body([]byte(ctx.Input.Param(":id")))
  72. }),
  73. )
  74. AddNamespace(ns)
  75. BeeApp.Handlers.ServeHTTP(w, r)
  76. if w.Body.String() != "123" {
  77. t.Errorf("TestNamespaceNestParam can't run, get the response is " + w.Body.String())
  78. }
  79. }
  80. func TestNamespaceRouter(t *testing.T) {
  81. r, _ := http.NewRequest("GET", "/v1/api/list", nil)
  82. w := httptest.NewRecorder()
  83. ns := NewNamespace("/v1")
  84. ns.Router("/api/list", &TestController{}, "*:List")
  85. AddNamespace(ns)
  86. BeeApp.Handlers.ServeHTTP(w, r)
  87. if w.Body.String() != "i am list" {
  88. t.Errorf("TestNamespaceRouter can't run, get the response is " + w.Body.String())
  89. }
  90. }
  91. func TestNamespaceAutoFunc(t *testing.T) {
  92. r, _ := http.NewRequest("GET", "/v1/test/list", nil)
  93. w := httptest.NewRecorder()
  94. ns := NewNamespace("/v1")
  95. ns.AutoRouter(&TestController{})
  96. AddNamespace(ns)
  97. BeeApp.Handlers.ServeHTTP(w, r)
  98. if w.Body.String() != "i am list" {
  99. t.Errorf("user define func can't run")
  100. }
  101. }
  102. func TestNamespaceFilter(t *testing.T) {
  103. r, _ := http.NewRequest("GET", "/v1/user/123", nil)
  104. w := httptest.NewRecorder()
  105. ns := NewNamespace("/v1")
  106. ns.Filter("before", func(ctx *context.Context) {
  107. ctx.Output.Body([]byte("this is Filter"))
  108. }).
  109. Get("/user/:id", func(ctx *context.Context) {
  110. ctx.Output.Body([]byte(ctx.Input.Param(":id")))
  111. })
  112. AddNamespace(ns)
  113. BeeApp.Handlers.ServeHTTP(w, r)
  114. if w.Body.String() != "this is Filter" {
  115. t.Errorf("TestNamespaceFilter can't run, get the response is " + w.Body.String())
  116. }
  117. }
  118. func TestNamespaceCond(t *testing.T) {
  119. r, _ := http.NewRequest("GET", "/v2/test/list", nil)
  120. w := httptest.NewRecorder()
  121. ns := NewNamespace("/v2")
  122. ns.Cond(func(ctx *context.Context) bool {
  123. if ctx.Input.Domain() == "beego.me" {
  124. return true
  125. }
  126. return false
  127. }).
  128. AutoRouter(&TestController{})
  129. AddNamespace(ns)
  130. BeeApp.Handlers.ServeHTTP(w, r)
  131. if w.Code != 405 {
  132. t.Errorf("TestNamespaceCond can't run get the result " + strconv.Itoa(w.Code))
  133. }
  134. }
  135. func TestNamespaceInside(t *testing.T) {
  136. r, _ := http.NewRequest("GET", "/v3/shop/order/123", nil)
  137. w := httptest.NewRecorder()
  138. ns := NewNamespace("/v3",
  139. NSAutoRouter(&TestController{}),
  140. NSNamespace("/shop",
  141. NSGet("/order/:id", func(ctx *context.Context) {
  142. ctx.Output.Body([]byte(ctx.Input.Param(":id")))
  143. }),
  144. ),
  145. )
  146. AddNamespace(ns)
  147. BeeApp.Handlers.ServeHTTP(w, r)
  148. if w.Body.String() != "123" {
  149. t.Errorf("TestNamespaceInside can't run, get the response is " + w.Body.String())
  150. }
  151. }