router_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. type RouterNoMethodAction struct {
  9. }
  10. func TestRouter1(t *testing.T) {
  11. buff := bytes.NewBufferString("")
  12. recorder := httptest.NewRecorder()
  13. recorder.Body = buff
  14. o := Classic()
  15. o.Get("/:name", new(RouterNoMethodAction))
  16. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  17. if err != nil {
  18. t.Error(err)
  19. }
  20. o.ServeHTTP(recorder, req)
  21. expect(t, recorder.Code, http.StatusNotFound)
  22. refute(t, len(buff.String()), 0)
  23. }
  24. type RouterGetAction struct {
  25. }
  26. func (a *RouterGetAction) Get() string {
  27. return "get"
  28. }
  29. func (a *RouterGetAction) Post() string {
  30. return "post"
  31. }
  32. func TestRouter2(t *testing.T) {
  33. buff := bytes.NewBufferString("")
  34. recorder := httptest.NewRecorder()
  35. recorder.Body = buff
  36. o := Classic()
  37. o.Get("/:name", new(RouterGetAction))
  38. o.Post("/:name", new(RouterGetAction))
  39. req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
  40. if err != nil {
  41. t.Error(err)
  42. }
  43. o.ServeHTTP(recorder, req)
  44. expect(t, recorder.Code, http.StatusOK)
  45. refute(t, len(buff.String()), 0)
  46. expect(t, buff.String(), "get")
  47. buff.Reset()
  48. req, err = http.NewRequest("POST", "http://localhost:8000/foobar", nil)
  49. if err != nil {
  50. t.Error(err)
  51. }
  52. o.ServeHTTP(recorder, req)
  53. expect(t, recorder.Code, http.StatusOK)
  54. refute(t, len(buff.String()), 0)
  55. expect(t, buff.String(), "post")
  56. }
  57. type RouterSpecAction struct {
  58. a string
  59. }
  60. func (RouterSpecAction) Method1() string {
  61. return "1"
  62. }
  63. func (r *RouterSpecAction) Method2() string {
  64. return r.a
  65. }
  66. /*
  67. func TestRouter3(t *testing.T) {
  68. buff := bytes.NewBufferString("")
  69. recorder := httptest.NewRecorder()
  70. recorder.Body = buff
  71. o := Classic()
  72. o.Get("/1", new(RouterSpecAction).Method1)
  73. o.Get("/2", new(RouterSpecAction).Method2)
  74. req, err := http.NewRequest("GET", "http://localhost:8000/1", nil)
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. o.ServeHTTP(recorder, req)
  79. expect(t, recorder.Code, http.StatusOK)
  80. refute(t, len(buff.String()), 0)
  81. }*/
  82. func TestRouterFunc(t *testing.T) {
  83. buff := bytes.NewBufferString("")
  84. recorder := httptest.NewRecorder()
  85. recorder.Body = buff
  86. o := Classic()
  87. o.Get("/", func() string {
  88. return "func"
  89. })
  90. o.Post("/", func(ctx *Context) {
  91. ctx.Write([]byte("func(*Context)"))
  92. })
  93. o.Put("/", func(resp http.ResponseWriter, req *http.Request) {
  94. resp.Write([]byte("func(http.ResponseWriter, *http.Request)"))
  95. })
  96. o.Options("/", func(resp http.ResponseWriter) {
  97. resp.Write([]byte("func(http.ResponseWriter)"))
  98. })
  99. o.Delete("/", func(req *http.Request) string {
  100. return "func(*http.Request)"
  101. })
  102. // plain
  103. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. o.ServeHTTP(recorder, req)
  108. expect(t, recorder.Code, http.StatusOK)
  109. refute(t, len(buff.String()), 0)
  110. expect(t, buff.String(), "func")
  111. // context
  112. buff.Reset()
  113. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  114. if err != nil {
  115. t.Error(err)
  116. }
  117. o.ServeHTTP(recorder, req)
  118. expect(t, recorder.Code, http.StatusOK)
  119. refute(t, len(buff.String()), 0)
  120. expect(t, buff.String(), "func(*Context)")
  121. // http
  122. buff.Reset()
  123. req, err = http.NewRequest("PUT", "http://localhost:8000/", nil)
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. o.ServeHTTP(recorder, req)
  128. expect(t, recorder.Code, http.StatusOK)
  129. refute(t, len(buff.String()), 0)
  130. expect(t, buff.String(), "func(http.ResponseWriter, *http.Request)")
  131. // response
  132. buff.Reset()
  133. req, err = http.NewRequest("OPTIONS", "http://localhost:8000/", nil)
  134. if err != nil {
  135. t.Error(err)
  136. }
  137. o.ServeHTTP(recorder, req)
  138. expect(t, recorder.Code, http.StatusOK)
  139. refute(t, len(buff.String()), 0)
  140. expect(t, buff.String(), "func(http.ResponseWriter)")
  141. // req
  142. buff.Reset()
  143. req, err = http.NewRequest("DELETE", "http://localhost:8000/", nil)
  144. if err != nil {
  145. t.Error(err)
  146. }
  147. o.ServeHTTP(recorder, req)
  148. expect(t, recorder.Code, http.StatusOK)
  149. refute(t, len(buff.String()), 0)
  150. expect(t, buff.String(), "func(*http.Request)")
  151. }
  152. type Router4Action struct {
  153. Params
  154. }
  155. func (r *Router4Action) Get() string {
  156. return r.Params.Get(":name1") + "-" + r.Params.Get(":name2")
  157. }
  158. func TestRouter4(t *testing.T) {
  159. buff := bytes.NewBufferString("")
  160. recorder := httptest.NewRecorder()
  161. recorder.Body = buff
  162. o := Classic()
  163. o.Get("/:name1-:name2", new(Router4Action))
  164. req, err := http.NewRequest("GET", "http://localhost:8000/foobar-foobar2", nil)
  165. if err != nil {
  166. t.Error(err)
  167. }
  168. o.ServeHTTP(recorder, req)
  169. expect(t, recorder.Code, http.StatusOK)
  170. refute(t, len(buff.String()), 0)
  171. expect(t, buff.String(), "foobar-foobar2")
  172. }
  173. type Router5Action struct {
  174. }
  175. func (r *Router5Action) Get() string {
  176. return "router5"
  177. }
  178. func TestRouter5(t *testing.T) {
  179. buff := bytes.NewBufferString("")
  180. recorder := httptest.NewRecorder()
  181. recorder.Body = buff
  182. o := Classic()
  183. o.Route("GET", "/", new(Router5Action))
  184. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  185. if err != nil {
  186. t.Error(err)
  187. }
  188. o.ServeHTTP(recorder, req)
  189. expect(t, recorder.Code, http.StatusOK)
  190. refute(t, len(buff.String()), 0)
  191. expect(t, buff.String(), "router5")
  192. }
  193. type Router6Action struct {
  194. }
  195. func (r *Router6Action) MyMethod() string {
  196. return "router6"
  197. }
  198. func TestRouter6(t *testing.T) {
  199. buff := bytes.NewBufferString("")
  200. recorder := httptest.NewRecorder()
  201. recorder.Body = buff
  202. o := Classic()
  203. o.Route("GET:MyMethod", "/", new(Router6Action))
  204. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  205. if err != nil {
  206. t.Error(err)
  207. }
  208. o.ServeHTTP(recorder, req)
  209. expect(t, recorder.Code, http.StatusOK)
  210. refute(t, len(buff.String()), 0)
  211. expect(t, buff.String(), "router6")
  212. }
  213. type Router7Action struct {
  214. }
  215. func (r *Router7Action) MyGet() string {
  216. return "router7-get"
  217. }
  218. func (r *Router7Action) Post() string {
  219. return "router7-post"
  220. }
  221. func TestRouter7(t *testing.T) {
  222. buff := bytes.NewBufferString("")
  223. recorder := httptest.NewRecorder()
  224. recorder.Body = buff
  225. o := Classic()
  226. o.Route([]string{"GET:MyGet", "POST"}, "/", new(Router7Action))
  227. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  228. if err != nil {
  229. t.Error(err)
  230. }
  231. o.ServeHTTP(recorder, req)
  232. expect(t, recorder.Code, http.StatusOK)
  233. refute(t, len(buff.String()), 0)
  234. expect(t, buff.String(), "router7-get")
  235. buff.Reset()
  236. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. o.ServeHTTP(recorder, req)
  241. expect(t, recorder.Code, http.StatusOK)
  242. refute(t, len(buff.String()), 0)
  243. expect(t, buff.String(), "router7-post")
  244. }
  245. type Router8Action struct {
  246. }
  247. func (r *Router8Action) MyGet() string {
  248. return "router8-get"
  249. }
  250. func (r *Router8Action) Post() string {
  251. return "router8-post"
  252. }
  253. func TestRouter8(t *testing.T) {
  254. buff := bytes.NewBufferString("")
  255. recorder := httptest.NewRecorder()
  256. recorder.Body = buff
  257. o := Classic()
  258. o.Route(map[string]string{"GET": "MyGet", "POST": "Post"}, "/", new(Router8Action))
  259. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  260. if err != nil {
  261. t.Error(err)
  262. }
  263. o.ServeHTTP(recorder, req)
  264. expect(t, recorder.Code, http.StatusOK)
  265. refute(t, len(buff.String()), 0)
  266. expect(t, buff.String(), "router8-get")
  267. buff.Reset()
  268. req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
  269. if err != nil {
  270. t.Error(err)
  271. }
  272. o.ServeHTTP(recorder, req)
  273. expect(t, recorder.Code, http.StatusOK)
  274. refute(t, len(buff.String()), 0)
  275. expect(t, buff.String(), "router8-post")
  276. }