static_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package tango
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestStatic(t *testing.T) {
  9. buff := bytes.NewBufferString("")
  10. recorder := httptest.NewRecorder()
  11. recorder.Body = buff
  12. tg := Static()
  13. req, err := http.NewRequest("GET", "http://localhost:8000/test.html", nil)
  14. if err != nil {
  15. t.Error(err)
  16. }
  17. tg.ServeHTTP(recorder, req)
  18. expect(t, recorder.Code, http.StatusOK)
  19. refute(t, len(buff.String()), 0)
  20. expect(t, buff.String(), "hello tango")
  21. buff.Reset()
  22. req, err = http.NewRequest("GET", "http://localhost:8000/", nil)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. tg.ServeHTTP(recorder, req)
  27. expect(t, recorder.Code, http.StatusOK)
  28. refute(t, len(buff.String()), 0)
  29. expect(t, buff.String(), "this is index.html")
  30. }
  31. func TestStatic2(t *testing.T) {
  32. buff := bytes.NewBufferString("")
  33. recorder := httptest.NewRecorder()
  34. recorder.Body = buff
  35. tg := Static()
  36. req, err := http.NewRequest("GET", "http://localhost:8000/test.png", nil)
  37. if err != nil {
  38. t.Error(err)
  39. }
  40. tg.ServeHTTP(recorder, req)
  41. expect(t, recorder.Code, http.StatusNotFound)
  42. }