Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
9.8KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. )
  11. func TestGroup1(t *testing.T) {
  12. buff := bytes.NewBufferString("")
  13. recorder := httptest.NewRecorder()
  14. recorder.Body = buff
  15. o := Classic()
  16. o.Group("/api", func(g *Group) {
  17. g.Get("/1", func() string {
  18. return "/1"
  19. })
  20. g.Post("/2", func() string {
  21. return "/2"
  22. })
  23. })
  24. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. o.ServeHTTP(recorder, req)
  29. expect(t, recorder.Code, http.StatusOK)
  30. refute(t, len(buff.String()), 0)
  31. expect(t, buff.String(), "/1")
  32. buff.Reset()
  33. req, err = http.NewRequest("POST", "http://localhost:8000/api/2", nil)
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. o.ServeHTTP(recorder, req)
  38. expect(t, recorder.Code, http.StatusOK)
  39. refute(t, len(buff.String()), 0)
  40. expect(t, buff.String(), "/2")
  41. }
  42. func TestGroup2(t *testing.T) {
  43. buff := bytes.NewBufferString("")
  44. recorder := httptest.NewRecorder()
  45. recorder.Body = buff
  46. g := NewGroup()
  47. g.Any("/1", func() string {
  48. return "/1"
  49. })
  50. g.Options("/2", func() string {
  51. return "/2"
  52. })
  53. o := Classic()
  54. o.Group("/api", g)
  55. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  56. if err != nil {
  57. t.Error(err)
  58. }
  59. o.ServeHTTP(recorder, req)
  60. expect(t, recorder.Code, http.StatusOK)
  61. refute(t, len(buff.String()), 0)
  62. expect(t, buff.String(), "/1")
  63. buff.Reset()
  64. req, err = http.NewRequest("OPTIONS", "http://localhost:8000/api/2", nil)
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. o.ServeHTTP(recorder, req)
  69. expect(t, recorder.Code, http.StatusOK)
  70. refute(t, len(buff.String()), 0)
  71. expect(t, buff.String(), "/2")
  72. }
  73. func TestGroup3(t *testing.T) {
  74. buff := bytes.NewBufferString("")
  75. recorder := httptest.NewRecorder()
  76. recorder.Body = buff
  77. o := Classic()
  78. o.Group("/api", func(g *Group) {
  79. g.Group("/v1", func(cg *Group) {
  80. cg.Trace("/1", func() string {
  81. return "/1"
  82. })
  83. cg.Patch("/2", func() string {
  84. return "/2"
  85. })
  86. })
  87. })
  88. req, err := http.NewRequest("TRACE", "http://localhost:8000/api/v1/1", nil)
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. o.ServeHTTP(recorder, req)
  93. expect(t, recorder.Code, http.StatusOK)
  94. refute(t, len(buff.String()), 0)
  95. expect(t, buff.String(), "/1")
  96. buff.Reset()
  97. req, err = http.NewRequest("PATCH", "http://localhost:8000/api/v1/2", nil)
  98. if err != nil {
  99. t.Error(err)
  100. }
  101. o.ServeHTTP(recorder, req)
  102. expect(t, recorder.Code, http.StatusOK)
  103. refute(t, len(buff.String()), 0)
  104. expect(t, buff.String(), "/2")
  105. }
  106. func TestGroup4(t *testing.T) {
  107. buff := bytes.NewBufferString("")
  108. recorder := httptest.NewRecorder()
  109. recorder.Body = buff
  110. o := Classic()
  111. o.Group("", func(g *Group) {
  112. g.Delete("/api/1", func() string {
  113. return "/1"
  114. })
  115. g.Head("/api/2", func() string {
  116. return "/2"
  117. })
  118. })
  119. req, err := http.NewRequest("DELETE", "http://localhost:8000/api/1", nil)
  120. if err != nil {
  121. t.Error(err)
  122. }
  123. o.ServeHTTP(recorder, req)
  124. expect(t, recorder.Code, http.StatusOK)
  125. refute(t, len(buff.String()), 0)
  126. expect(t, buff.String(), "/1")
  127. buff.Reset()
  128. req, err = http.NewRequest("HEAD", "http://localhost:8000/api/2", nil)
  129. if err != nil {
  130. t.Error(err)
  131. }
  132. o.ServeHTTP(recorder, req)
  133. expect(t, recorder.Code, http.StatusOK)
  134. refute(t, len(buff.String()), 0)
  135. expect(t, buff.String(), "/2")
  136. }
  137. func TestGroup5(t *testing.T) {
  138. buff := bytes.NewBufferString("")
  139. recorder := httptest.NewRecorder()
  140. recorder.Body = buff
  141. o := Classic()
  142. var handlerGroup bool
  143. o.Group("/api", func(g *Group) {
  144. g.Use(HandlerFunc(func(ctx *Context) {
  145. handlerGroup = true
  146. ctx.Next()
  147. }))
  148. g.Put("/1", func() string {
  149. return "/1"
  150. })
  151. })
  152. o.Post("/2", func() string {
  153. return "/2"
  154. })
  155. req, err := http.NewRequest("PUT", "http://localhost:8000/api/1", nil)
  156. if err != nil {
  157. t.Error(err)
  158. }
  159. o.ServeHTTP(recorder, req)
  160. expect(t, recorder.Code, http.StatusOK)
  161. refute(t, len(buff.String()), 0)
  162. expect(t, buff.String(), "/1")
  163. expect(t, handlerGroup, true)
  164. handlerGroup = false
  165. buff.Reset()
  166. req, err = http.NewRequest("POST", "http://localhost:8000/2", nil)
  167. if err != nil {
  168. t.Error(err)
  169. }
  170. o.ServeHTTP(recorder, req)
  171. expect(t, recorder.Code, http.StatusOK)
  172. refute(t, len(buff.String()), 0)
  173. expect(t, buff.String(), "/2")
  174. expect(t, handlerGroup, false)
  175. }
  176. func TestGroup6(t *testing.T) {
  177. buff := bytes.NewBufferString("")
  178. recorder := httptest.NewRecorder()
  179. recorder.Body = buff
  180. var handlerGroup bool
  181. g := NewGroup()
  182. g.Use(HandlerFunc(func(ctx *Context) {
  183. handlerGroup = true
  184. ctx.Next()
  185. }))
  186. g.Get("/1", func() string {
  187. return "/1"
  188. })
  189. o := Classic()
  190. o.Group("/api", g)
  191. o.Post("/2", func() string {
  192. return "/2"
  193. })
  194. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  195. if err != nil {
  196. t.Error(err)
  197. }
  198. o.ServeHTTP(recorder, req)
  199. expect(t, recorder.Code, http.StatusOK)
  200. refute(t, len(buff.String()), 0)
  201. expect(t, buff.String(), "/1")
  202. expect(t, handlerGroup, true)
  203. handlerGroup = false
  204. buff.Reset()
  205. req, err = http.NewRequest("POST", "http://localhost:8000/2", nil)
  206. if err != nil {
  207. t.Error(err)
  208. }
  209. o.ServeHTTP(recorder, req)
  210. expect(t, recorder.Code, http.StatusOK)
  211. refute(t, len(buff.String()), 0)
  212. expect(t, buff.String(), "/2")
  213. expect(t, handlerGroup, false)
  214. }
  215. func TestGroup7(t *testing.T) {
  216. buff := bytes.NewBufferString("")
  217. recorder := httptest.NewRecorder()
  218. recorder.Body = buff
  219. var isPanic bool
  220. defer func() {
  221. if err := recover(); err != nil {
  222. isPanic = true
  223. }
  224. expect(t, isPanic, true)
  225. }()
  226. o := Classic()
  227. o.Group("/api", func() {
  228. })
  229. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  230. if err != nil {
  231. t.Error(err)
  232. }
  233. o.ServeHTTP(recorder, req)
  234. }
  235. func TestGroup8(t *testing.T) {
  236. buff := bytes.NewBufferString("")
  237. recorder := httptest.NewRecorder()
  238. recorder.Body = buff
  239. var handlerGroup bool
  240. g := NewGroup()
  241. g.Use(HandlerFunc(func(ctx *Context) {
  242. handlerGroup = true
  243. ctx.Next()
  244. }))
  245. g.Get("/1", func(ctx *Context) string {
  246. p := ctx.Params()
  247. return (*p)[0].Value
  248. })
  249. o := Classic()
  250. o.Group("/:user", g)
  251. o.Post("/2", func() string {
  252. return "/2"
  253. })
  254. req, err := http.NewRequest("GET", "http://localhost:8000/myname/1", nil)
  255. if err != nil {
  256. t.Error(err)
  257. }
  258. o.ServeHTTP(recorder, req)
  259. expect(t, recorder.Code, http.StatusOK)
  260. refute(t, len(buff.String()), 0)
  261. expect(t, buff.String(), "myname")
  262. expect(t, handlerGroup, true)
  263. handlerGroup = false
  264. buff.Reset()
  265. req, err = http.NewRequest("POST", "http://localhost:8000/2", nil)
  266. if err != nil {
  267. t.Error(err)
  268. }
  269. o.ServeHTTP(recorder, req)
  270. expect(t, recorder.Code, http.StatusOK)
  271. refute(t, len(buff.String()), 0)
  272. expect(t, buff.String(), "/2")
  273. expect(t, handlerGroup, false)
  274. }
  275. func TestGroup9(t *testing.T) {
  276. buff := bytes.NewBufferString("")
  277. recorder := httptest.NewRecorder()
  278. recorder.Body = buff
  279. o := Classic()
  280. var handlerGroup string
  281. o.Group("/api/v1", func(g *Group) {
  282. g.Group("/case/:case_id", func(tg *Group) {
  283. tg.Use(HandlerFunc(func(ctx *Context) {
  284. handlerGroup = ctx.Param("case_id")
  285. ctx.Next()
  286. }))
  287. tg.Put("/1", func() string {
  288. return "/1"
  289. })
  290. })
  291. })
  292. o.Post("/api/v1/2", func() string {
  293. return "/2"
  294. })
  295. req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/1", nil)
  296. if err != nil {
  297. t.Error(err)
  298. }
  299. o.ServeHTTP(recorder, req)
  300. expect(t, recorder.Code, http.StatusOK)
  301. refute(t, len(buff.String()), 0)
  302. expect(t, buff.String(), "/1")
  303. expect(t, handlerGroup, "1")
  304. handlerGroup = ""
  305. buff.Reset()
  306. req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
  307. if err != nil {
  308. t.Error(err)
  309. }
  310. o.ServeHTTP(recorder, req)
  311. expect(t, recorder.Code, http.StatusOK)
  312. refute(t, len(buff.String()), 0)
  313. expect(t, buff.String(), "/2")
  314. expect(t, handlerGroup, "")
  315. }
  316. func TestGroup10(t *testing.T) {
  317. buff := bytes.NewBufferString("")
  318. recorder := httptest.NewRecorder()
  319. recorder.Body = buff
  320. o := Classic()
  321. var handlerGroup string
  322. o.Group("/api/v1", func(g *Group) {
  323. g.Group("/case/:case_id", func(tg *Group) {
  324. tg.Put("/1", func() string {
  325. return "/1"
  326. })
  327. })
  328. }, HandlerFunc(func(ctx *Context) {
  329. handlerGroup = ctx.Param("case_id")
  330. ctx.Next()
  331. }))
  332. o.Post("/api/v1/2", func() string {
  333. return "/2"
  334. })
  335. req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/1", nil)
  336. if err != nil {
  337. t.Error(err)
  338. }
  339. o.ServeHTTP(recorder, req)
  340. expect(t, recorder.Code, http.StatusOK)
  341. refute(t, len(buff.String()), 0)
  342. expect(t, buff.String(), "/1")
  343. expect(t, handlerGroup, "1")
  344. handlerGroup = ""
  345. buff.Reset()
  346. req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
  347. if err != nil {
  348. t.Error(err)
  349. }
  350. o.ServeHTTP(recorder, req)
  351. expect(t, recorder.Code, http.StatusOK)
  352. refute(t, len(buff.String()), 0)
  353. expect(t, buff.String(), "/2")
  354. expect(t, handlerGroup, "")
  355. }
  356. func TestGroup11(t *testing.T) {
  357. buff := bytes.NewBufferString("")
  358. recorder := httptest.NewRecorder()
  359. recorder.Body = buff
  360. o := Classic()
  361. var handlerGroup string
  362. o.Group("/api/v1", func(g *Group) {
  363. g.Group("/case/:case_id", func(tg *Group) {
  364. tg.Put("/1", func() string {
  365. return "/1"
  366. })
  367. }, HandlerFunc(func(ctx *Context) {
  368. handlerGroup = ctx.Param("case_id")
  369. ctx.Next()
  370. }))
  371. })
  372. o.Post("/api/v1/2", func() string {
  373. return "/2"
  374. })
  375. req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/1", nil)
  376. if err != nil {
  377. t.Error(err)
  378. }
  379. o.ServeHTTP(recorder, req)
  380. expect(t, recorder.Code, http.StatusOK)
  381. refute(t, len(buff.String()), 0)
  382. expect(t, buff.String(), "/1")
  383. expect(t, handlerGroup, "1")
  384. handlerGroup = ""
  385. buff.Reset()
  386. req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
  387. if err != nil {
  388. t.Error(err)
  389. }
  390. o.ServeHTTP(recorder, req)
  391. expect(t, recorder.Code, http.StatusOK)
  392. refute(t, len(buff.String()), 0)
  393. expect(t, buff.String(), "/2")
  394. expect(t, handlerGroup, "")
  395. }