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.

109 lines
2.4KB

  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 TestDir1(t *testing.T) {
  12. buff := bytes.NewBufferString("")
  13. recorder := httptest.NewRecorder()
  14. recorder.Body = buff
  15. tg := New()
  16. tg.Get("/:name", Dir("./public"))
  17. req, err := http.NewRequest("GET", "http://localhost:8000/test.html", nil)
  18. if err != nil {
  19. t.Error(err)
  20. }
  21. tg.ServeHTTP(recorder, req)
  22. expect(t, recorder.Code, http.StatusOK)
  23. refute(t, len(buff.String()), 0)
  24. expect(t, buff.String(), "hello tango")
  25. }
  26. func TestDir2(t *testing.T) {
  27. buff := bytes.NewBufferString("")
  28. recorder := httptest.NewRecorder()
  29. recorder.Body = buff
  30. tg := New()
  31. tg.Get("/", Dir("./public"))
  32. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. tg.ServeHTTP(recorder, req)
  37. expect(t, recorder.Code, http.StatusNotFound)
  38. refute(t, len(buff.String()), 0)
  39. expect(t, buff.String(), http.StatusText(http.StatusNotFound))
  40. }
  41. func TestDir3(t *testing.T) {
  42. buff := bytes.NewBufferString("")
  43. recorder := httptest.NewRecorder()
  44. recorder.Body = buff
  45. tg := New()
  46. tg.Get("/*name", Dir("./public"))
  47. req, err := http.NewRequest("GET", "http://localhost:8000/js/my.js", nil)
  48. if err != nil {
  49. t.Error(err)
  50. }
  51. var res = "var test"
  52. tg.ServeHTTP(recorder, req)
  53. expect(t, recorder.Code, http.StatusOK)
  54. expect(t, len(buff.String()), len(res))
  55. expect(t, buff.String(), res)
  56. }
  57. func TestFile1(t *testing.T) {
  58. buff := bytes.NewBufferString("")
  59. recorder := httptest.NewRecorder()
  60. recorder.Body = buff
  61. tg := New()
  62. tg.Get("/test.html", File("./public/test.html"))
  63. req, err := http.NewRequest("GET", "http://localhost:8000/test.html", nil)
  64. if err != nil {
  65. t.Error(err)
  66. }
  67. tg.ServeHTTP(recorder, req)
  68. expect(t, recorder.Code, http.StatusOK)
  69. refute(t, len(buff.String()), 0)
  70. expect(t, buff.String(), "hello tango")
  71. }
  72. func TestContent1(t *testing.T) {
  73. buff := bytes.NewBufferString("")
  74. recorder := httptest.NewRecorder()
  75. recorder.Body = buff
  76. tg := New()
  77. tg.Get("/test.html", Content("test.html", http.Dir("./public")))
  78. req, err := http.NewRequest("GET", "http://localhost:8000/test.html", nil)
  79. if err != nil {
  80. t.Error(err)
  81. }
  82. tg.ServeHTTP(recorder, req)
  83. expect(t, recorder.Code, http.StatusOK)
  84. refute(t, len(buff.String()), 0)
  85. expect(t, buff.String(), "hello tango")
  86. }