tan_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package tango
  2. import (
  3. "reflect"
  4. "testing"
  5. "bytes"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "time"
  10. "io/ioutil"
  11. )
  12. func TestTan1(t *testing.T) {
  13. buff := bytes.NewBufferString("")
  14. recorder := httptest.NewRecorder()
  15. recorder.Body = buff
  16. l := NewLogger(os.Stdout)
  17. o := Classic(l)
  18. o.Get("/", func() string {
  19. return Version()
  20. })
  21. o.Logger().Debug("it's ok")
  22. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. o.ServeHTTP(recorder, req)
  27. expect(t, recorder.Code, http.StatusOK)
  28. refute(t, len(buff.String()), 0)
  29. expect(t, buff.String(), Version())
  30. }
  31. func TestTan2(t *testing.T) {
  32. o := Classic()
  33. o.Get("/", func() string {
  34. return Version()
  35. })
  36. go o.Run()
  37. time.Sleep(100 * time.Millisecond)
  38. resp, err := http.Get("http://localhost:8000/")
  39. if err != nil {
  40. t.Error(err)
  41. }
  42. bs, err := ioutil.ReadAll(resp.Body)
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. expect(t, resp.StatusCode, http.StatusOK)
  47. expect(t, string(bs), Version())
  48. }
  49. func TestTan3(t *testing.T) {
  50. o := Classic()
  51. o.Get("/", func() string {
  52. return Version()
  53. })
  54. go o.Run(":4040")
  55. time.Sleep(100 * time.Millisecond)
  56. resp, err := http.Get("http://localhost:4040/")
  57. if err != nil {
  58. t.Error(err)
  59. }
  60. bs, err := ioutil.ReadAll(resp.Body)
  61. if err != nil {
  62. t.Error(err)
  63. }
  64. expect(t, resp.StatusCode, http.StatusOK)
  65. expect(t, string(bs), Version())
  66. }
  67. /* Test Helpers */
  68. func expect(t *testing.T, a interface{}, b interface{}) {
  69. if a != b {
  70. t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
  71. }
  72. }
  73. func refute(t *testing.T, a interface{}, b interface{}) {
  74. if a == b {
  75. t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
  76. }
  77. }