templating_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package render
  2. import (
  3. "html/template"
  4. "net/http"
  5. "net/http/httptest"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "github.com/PuerkitoBio/goquery"
  10. "go.mindeco.de/logging"
  11. "go.mindeco.de/logging/logtest"
  12. )
  13. func TestRender(t *testing.T) {
  14. logging.SetupLogging(logtest.Logger("Render", t))
  15. log := logging.Logger("TestRender")
  16. r, err := New(http.Dir("tests"),
  17. AddTemplates("test1.tmpl"),
  18. SetLogger(log),
  19. )
  20. if err != nil {
  21. t.Fatal("New() failed", err)
  22. }
  23. rw := httptest.NewRecorder()
  24. req, err := http.NewRequest("GET", "/test", nil)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if err := r.Render(rw, req, "test1.tmpl", http.StatusOK, nil); err != nil {
  29. t.Fatal(err)
  30. }
  31. if rw.Code != http.StatusOK {
  32. t.Fatal("wrong status")
  33. }
  34. doc, err := goquery.NewDocumentFromReader(rw.Body)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if title := doc.Find("title").Text(); title != "render - tests" {
  39. t.Fatalf("wrong Title. got: %s", title)
  40. }
  41. if hello := doc.Find("#hello").Text(); hello != "Hello" {
  42. t.Fatalf("wrong hello. got: %s", hello)
  43. }
  44. if testID := doc.Find("#testID").Text(); testID != "Test2" {
  45. t.Fatalf("wrong testID. got: %s", testID)
  46. }
  47. }
  48. func TestFuncMap(t *testing.T) {
  49. logging.SetupLogging(logtest.Logger("Render", t))
  50. log := logging.Logger("TestFuncMap")
  51. r, err := New(http.Dir("tests"),
  52. SetLogger(log),
  53. AddTemplates("testFuncMap.tmpl"),
  54. FuncMap(template.FuncMap{
  55. "itoa": strconv.Itoa,
  56. }),
  57. )
  58. if err != nil {
  59. t.Fatal("New() failed", err)
  60. }
  61. rw := httptest.NewRecorder()
  62. req, err := http.NewRequest("GET", "/test", nil)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if err := r.Render(rw, req, "testFuncMap.tmpl", http.StatusOK, nil); err != nil {
  67. t.Fatal(err)
  68. }
  69. if rw.Code != http.StatusOK {
  70. t.Fatal("wrong status")
  71. }
  72. }
  73. func TestBugOverride(t *testing.T) {
  74. logging.SetupLogging(logtest.Logger("Render", t))
  75. log := logging.Logger("TestBugOverride")
  76. r, err := New(http.Dir("tests"),
  77. SetLogger(log),
  78. AddTemplates("testFuncMap.tmpl", "bug1.tmpl"),
  79. FuncMap(template.FuncMap{"itoa": strconv.Itoa}),
  80. )
  81. if err != nil {
  82. t.Fatal("New() failed", err)
  83. }
  84. rw := httptest.NewRecorder()
  85. req, err := http.NewRequest("GET", "/test", nil)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if err := r.Render(rw, req, "testFuncMap.tmpl", http.StatusOK, nil); err != nil {
  90. t.Fatal(err)
  91. }
  92. if rw.Code != http.StatusOK {
  93. t.Fatal("wrong status")
  94. }
  95. if !strings.Contains(rw.Body.String(), "42") {
  96. t.Fatal("first doesn't contain 42")
  97. }
  98. }
  99. func TestBaseTmpl(t *testing.T) {
  100. logging.SetupLogging(logtest.Logger("Render", t))
  101. log := logging.Logger("TestBugOverride")
  102. r, err := New(http.Dir("tests"),
  103. SetLogger(log),
  104. BaseTemplates("subdir/base2.tmpl"),
  105. AddTemplates("test1.tmpl"),
  106. FuncMap(template.FuncMap{"itoa": strconv.Itoa}),
  107. )
  108. if err != nil {
  109. t.Fatal("New() failed", err)
  110. }
  111. rw := httptest.NewRecorder()
  112. req, err := http.NewRequest("GET", "/test", nil)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if err := r.Render(rw, req, "test1.tmpl", http.StatusOK, nil); err != nil {
  117. t.Fatal(err)
  118. }
  119. if rw.Code != http.StatusOK {
  120. t.Fatal("wrong status")
  121. }
  122. doc, err := goquery.NewDocumentFromReader(rw.Body)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if heading := doc.Find("#baseHead").Text(); heading != "Alternative base in a subdir" {
  127. t.Fatalf("wrong heading. got: %s", heading)
  128. }
  129. }
  130. func TestMultileBaseTmpls(t *testing.T) {
  131. logging.SetupLogging(logtest.Logger("Render", t))
  132. log := logging.Logger("TestMultileBaseTmpls")
  133. r, err := New(http.Dir("tests"),
  134. SetLogger(log),
  135. BaseTemplates("subdir/base2.tmpl", "extra.tmpl"),
  136. AddTemplates("test2.tmpl"),
  137. )
  138. if err != nil {
  139. t.Fatal("New() failed", err)
  140. }
  141. rw := httptest.NewRecorder()
  142. req, err := http.NewRequest("GET", "/test", nil)
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. if err := r.Render(rw, req, "test2.tmpl", http.StatusOK, nil); err != nil {
  147. t.Fatal(err)
  148. }
  149. if rw.Code != http.StatusOK {
  150. t.Fatal("wrong status")
  151. }
  152. doc, err := goquery.NewDocumentFromReader(rw.Body)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. if ex := doc.Find("#extra").Text(); ex != "additional base tpl" {
  157. t.Fatalf("wrong ex. got: %s", ex)
  158. }
  159. }
  160. func TestFuncInjection(t *testing.T) {
  161. logging.SetupLogging(logtest.Logger("Render", t))
  162. log := logging.Logger("TestFuncMap")
  163. r, err := New(http.Dir("tests"),
  164. SetLogger(log),
  165. AddTemplates("testInject.tmpl"),
  166. InjectTemplateFunc("addr", func(r *http.Request) interface{} {
  167. return func() string {
  168. return r.Header.Get("X-Test-Addr")
  169. }
  170. }),
  171. )
  172. if err != nil {
  173. t.Fatal("New() failed", err)
  174. }
  175. rw := httptest.NewRecorder()
  176. req, err := http.NewRequest("GET", "/test", nil)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. req.Header.Set("X-Test-Addr", "localhost:1234")
  181. if err := r.Render(rw, req, "testInject.tmpl", http.StatusOK, nil); err != nil {
  182. t.Fatal(err)
  183. }
  184. if rw.Code != http.StatusOK {
  185. t.Fatal("wrong status")
  186. }
  187. doc, err := goquery.NewDocumentFromReader(rw.Body)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if ex := doc.Find("#addr").Text(); ex != "localhost:1234" {
  192. t.Fatalf("wrong ex. got: %s", ex)
  193. }
  194. }