group_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestGroup1(t *testing.T) {
  9. buff := bytes.NewBufferString("")
  10. recorder := httptest.NewRecorder()
  11. recorder.Body = buff
  12. o := Classic()
  13. o.Group("/api", func(g *Group) {
  14. g.Get("/1", func() string {
  15. return "/1"
  16. })
  17. g.Post("/2", func() string {
  18. return "/2"
  19. })
  20. })
  21. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. o.ServeHTTP(recorder, req)
  26. expect(t, recorder.Code, http.StatusOK)
  27. refute(t, len(buff.String()), 0)
  28. expect(t, buff.String(), "/1")
  29. buff.Reset()
  30. req, err = http.NewRequest("POST", "http://localhost:8000/api/2", nil)
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. o.ServeHTTP(recorder, req)
  35. expect(t, recorder.Code, http.StatusOK)
  36. refute(t, len(buff.String()), 0)
  37. expect(t, buff.String(), "/2")
  38. }
  39. func TestGroup2(t *testing.T) {
  40. buff := bytes.NewBufferString("")
  41. recorder := httptest.NewRecorder()
  42. recorder.Body = buff
  43. g := NewGroup()
  44. g.Get("/1", func() string {
  45. return "/1"
  46. })
  47. g.Post("/2", func() string {
  48. return "/2"
  49. })
  50. o := Classic()
  51. o.Group("/api", g)
  52. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. o.ServeHTTP(recorder, req)
  57. expect(t, recorder.Code, http.StatusOK)
  58. refute(t, len(buff.String()), 0)
  59. expect(t, buff.String(), "/1")
  60. buff.Reset()
  61. req, err = http.NewRequest("POST", "http://localhost:8000/api/2", nil)
  62. if err != nil {
  63. t.Error(err)
  64. }
  65. o.ServeHTTP(recorder, req)
  66. expect(t, recorder.Code, http.StatusOK)
  67. refute(t, len(buff.String()), 0)
  68. expect(t, buff.String(), "/2")
  69. }
  70. func TestGroup3(t *testing.T) {
  71. buff := bytes.NewBufferString("")
  72. recorder := httptest.NewRecorder()
  73. recorder.Body = buff
  74. o := Classic()
  75. o.Group("/api", func(g *Group) {
  76. g.Group("/v1", func(cg *Group) {
  77. cg.Get("/1", func() string {
  78. return "/1"
  79. })
  80. cg.Post("/2", func() string {
  81. return "/2"
  82. })
  83. })
  84. })
  85. req, err := http.NewRequest("GET", "http://localhost:8000/api/v1/1", nil)
  86. if err != nil {
  87. t.Error(err)
  88. }
  89. o.ServeHTTP(recorder, req)
  90. expect(t, recorder.Code, http.StatusOK)
  91. refute(t, len(buff.String()), 0)
  92. expect(t, buff.String(), "/1")
  93. buff.Reset()
  94. req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. o.ServeHTTP(recorder, req)
  99. expect(t, recorder.Code, http.StatusOK)
  100. refute(t, len(buff.String()), 0)
  101. expect(t, buff.String(), "/2")
  102. }
  103. func TestGroup4(t *testing.T) {
  104. buff := bytes.NewBufferString("")
  105. recorder := httptest.NewRecorder()
  106. recorder.Body = buff
  107. o := Classic()
  108. o.Group("", func(g *Group) {
  109. g.Get("/api/1", func() string {
  110. return "/1"
  111. })
  112. g.Post("/api/2", func() string {
  113. return "/2"
  114. })
  115. })
  116. req, err := http.NewRequest("GET", "http://localhost:8000/api/1", nil)
  117. if err != nil {
  118. t.Error(err)
  119. }
  120. o.ServeHTTP(recorder, req)
  121. expect(t, recorder.Code, http.StatusOK)
  122. refute(t, len(buff.String()), 0)
  123. expect(t, buff.String(), "/1")
  124. buff.Reset()
  125. req, err = http.NewRequest("POST", "http://localhost:8000/api/2", nil)
  126. if err != nil {
  127. t.Error(err)
  128. }
  129. o.ServeHTTP(recorder, req)
  130. expect(t, recorder.Code, http.StatusOK)
  131. refute(t, len(buff.String()), 0)
  132. expect(t, buff.String(), "/2")
  133. }