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.

126 lines
2.6 KiB

  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 TestStatic(t *testing.T) {
  12. buff := bytes.NewBufferString("")
  13. recorder := httptest.NewRecorder()
  14. recorder.Body = buff
  15. tg := New()
  16. tg.Use(Static())
  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. buff.Reset()
  26. req, err = http.NewRequest("GET", "http://localhost:8000/", nil)
  27. if err != nil {
  28. t.Error(err)
  29. }
  30. tg.ServeHTTP(recorder, req)
  31. expect(t, recorder.Code, http.StatusOK)
  32. refute(t, len(buff.String()), 0)
  33. expect(t, buff.String(), "this is index.html")
  34. }
  35. func TestStatic2(t *testing.T) {
  36. buff := bytes.NewBufferString("")
  37. recorder := httptest.NewRecorder()
  38. recorder.Body = buff
  39. tg := New()
  40. tg.Use(Static())
  41. req, err := http.NewRequest("GET", "http://localhost:8000/test.png", nil)
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. tg.ServeHTTP(recorder, req)
  46. expect(t, recorder.Code, http.StatusNotFound)
  47. }
  48. func TestStatic3(t *testing.T) {
  49. buff := bytes.NewBufferString("")
  50. recorder := httptest.NewRecorder()
  51. recorder.Body = buff
  52. tg := New()
  53. tg.Use(Static(StaticOptions{
  54. Prefix: "/public",
  55. RootPath: "./public",
  56. }))
  57. req, err := http.NewRequest("GET", "http://localhost:8000/public/test.html", nil)
  58. if err != nil {
  59. t.Error(err)
  60. }
  61. tg.ServeHTTP(recorder, req)
  62. expect(t, recorder.Code, http.StatusOK)
  63. expect(t, buff.String(), "hello tango")
  64. }
  65. func TestStatic4(t *testing.T) {
  66. buff := bytes.NewBufferString("")
  67. recorder := httptest.NewRecorder()
  68. recorder.Body = buff
  69. tg := New()
  70. tg.Use(Static(StaticOptions{
  71. Prefix: "/public",
  72. RootPath: "./public",
  73. }))
  74. req, err := http.NewRequest("GET", "http://localhost:8000/public/t.html", nil)
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. tg.ServeHTTP(recorder, req)
  79. expect(t, recorder.Code, http.StatusNotFound)
  80. expect(t, buff.String(), NotFound().Error())
  81. }
  82. func TestStatic5(t *testing.T) {
  83. buff := bytes.NewBufferString("")
  84. recorder := httptest.NewRecorder()
  85. recorder.Body = buff
  86. tg := New()
  87. tg.Use(Static(StaticOptions{
  88. Prefix: "/public",
  89. RootPath: "./public",
  90. ListDir: true,
  91. IndexFiles: []string{"a.html"},
  92. }))
  93. req, err := http.NewRequest("GET", "http://localhost:8000/public/", nil)
  94. if err != nil {
  95. t.Error(err)
  96. }
  97. tg.ServeHTTP(recorder, req)
  98. expect(t, recorder.Code, http.StatusOK)
  99. //expect(t, buff.String(), NotFound().Error())
  100. }