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.

120 lines
2.3KB

  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. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "reflect"
  12. "testing"
  13. "time"
  14. )
  15. func TestTan1(t *testing.T) {
  16. buff := bytes.NewBufferString("")
  17. recorder := httptest.NewRecorder()
  18. recorder.Body = buff
  19. l := NewLogger(os.Stdout)
  20. o := Classic(l)
  21. o.Get("/", func() string {
  22. return Version()
  23. })
  24. o.Logger().Debug("it's ok")
  25. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. o.ServeHTTP(recorder, req)
  30. expect(t, recorder.Code, http.StatusOK)
  31. refute(t, len(buff.String()), 0)
  32. expect(t, buff.String(), Version())
  33. }
  34. func TestTan2(t *testing.T) {
  35. o := Classic()
  36. o.Get("/", func() string {
  37. return Version()
  38. })
  39. go o.Run()
  40. time.Sleep(100 * time.Millisecond)
  41. resp, err := http.Get("http://localhost:8000/")
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. bs, err := ioutil.ReadAll(resp.Body)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. expect(t, resp.StatusCode, http.StatusOK)
  50. expect(t, string(bs), Version())
  51. }
  52. func TestTan3(t *testing.T) {
  53. o := Classic()
  54. o.Get("/", func() string {
  55. return Version()
  56. })
  57. go o.Run(":4040")
  58. time.Sleep(100 * time.Millisecond)
  59. resp, err := http.Get("http://localhost:4040/")
  60. if err != nil {
  61. t.Error(err)
  62. }
  63. bs, err := ioutil.ReadAll(resp.Body)
  64. if err != nil {
  65. t.Error(err)
  66. }
  67. expect(t, resp.StatusCode, http.StatusOK)
  68. expect(t, string(bs), Version())
  69. }
  70. /*
  71. func TestTan4(t *testing.T) {
  72. o := Classic()
  73. o.Get("/", func() string {
  74. return Version()
  75. })
  76. go o.RunTLS("./public/cert.pem", "./public/key.pem", ":5050")
  77. time.Sleep(100 * time.Millisecond)
  78. resp, err := http.Get("https://localhost:5050/")
  79. if err != nil {
  80. t.Error(err)
  81. }
  82. bs, err := ioutil.ReadAll(resp.Body)
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. expect(t, resp.StatusCode, http.StatusOK)
  87. expect(t, string(bs), Version())
  88. }*/
  89. /* Test Helpers */
  90. func expect(t *testing.T, a interface{}, b interface{}) {
  91. if a != b {
  92. t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
  93. }
  94. }
  95. func refute(t *testing.T, a interface{}, b interface{}) {
  96. if a == b {
  97. t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
  98. }
  99. }