rest_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package rest
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/go-test/deep"
  9. )
  10. type Something struct {
  11. First int
  12. Second string
  13. }
  14. func TestJSONDecoder(t *testing.T) {
  15. const json = `{ "First": 15, "Second": "Some string" }`
  16. buffer := bytes.NewBuffer([]byte(json))
  17. expected := Something{First: 15, Second: "Some string"}
  18. var result Something
  19. err := JSONDecoder(buffer.Bytes(), &result)
  20. if err != nil {
  21. t.Error(err)
  22. }
  23. if diff := deep.Equal(expected, result); diff != nil {
  24. t.Errorf("Wrong result: %v", diff)
  25. }
  26. }
  27. func TestJSONDecoderBadJSONSyntax(t *testing.T) {
  28. const badJSON = `{ First: "15", "Second": "Some string" }`
  29. buffer := bytes.NewBuffer([]byte(badJSON))
  30. expected := Something{First: 15, Second: "Some string"}
  31. var result Something
  32. err := JSONDecoder(buffer.Bytes(), &result)
  33. if err == nil {
  34. t.Error("There should be an error")
  35. }
  36. if diff := deep.Equal(expected, result); diff == nil {
  37. t.Error("Structures shouldn't match")
  38. }
  39. t.Log(err)
  40. }
  41. func TestJSONDecoderBadJSONType(t *testing.T) {
  42. const badJSON = `{ "First": "15", "Second": "Some string" }`
  43. buffer := bytes.NewBuffer([]byte(badJSON))
  44. expected := Something{First: 15, Second: "Some string"}
  45. var result Something
  46. err := JSONDecoder(buffer.Bytes(), &result)
  47. if err == nil {
  48. t.Error("There should be an error")
  49. }
  50. if diff := deep.Equal(expected, result); diff == nil {
  51. t.Error("Structures shouldn't match")
  52. }
  53. t.Log(err)
  54. }
  55. func fakeServer(statusCode int, data string) *httptest.Server {
  56. function := func(w http.ResponseWriter, r *http.Request) {
  57. w.Header().Set("Content-Type", "text")
  58. w.WriteHeader(statusCode)
  59. fmt.Fprint(w, data)
  60. }
  61. return httptest.NewServer(http.HandlerFunc(function))
  62. }
  63. func TestGET(t *testing.T) {
  64. expected := "Some text"
  65. server := fakeServer(http.StatusOK, expected)
  66. defer server.Close()
  67. data, err := GET(server.URL)
  68. if data == nil {
  69. t.Error("Data shouldn't be nil")
  70. }
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. result := data.String()
  75. if expected != result {
  76. t.Errorf("Wrong result, %v", result)
  77. }
  78. }
  79. func TestGET206(t *testing.T) {
  80. expected := "Some text"
  81. server := fakeServer(http.StatusPartialContent, expected)
  82. defer server.Close()
  83. data, err := GET(server.URL)
  84. if data == nil {
  85. t.Error("Data shouldn't be nil")
  86. }
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. result := data.String()
  91. if expected != result {
  92. t.Errorf("Wrong result, %v", result)
  93. }
  94. }
  95. func TestGET404(t *testing.T) {
  96. expected := "Some text"
  97. server := fakeServer(http.StatusNotFound, expected)
  98. defer server.Close()
  99. data, err := GET(server.URL)
  100. if data == nil {
  101. t.Error("Data shouldn't be nil")
  102. }
  103. if err == nil {
  104. t.Error("There should be an error!")
  105. }
  106. result := data.String()
  107. if expected != result {
  108. t.Errorf("Wrong result, %v", result)
  109. }
  110. }
  111. func TestGETNoServer(t *testing.T) {
  112. data, err := GET("/")
  113. if data != nil {
  114. t.Error("Data should be nil!")
  115. }
  116. if err == nil {
  117. t.Error("There should be an error!")
  118. }
  119. }
  120. func TestHEAD(t *testing.T) {
  121. server := fakeServer(http.StatusOK, "")
  122. defer server.Close()
  123. data, err := HEAD(server.URL)
  124. if err != nil {
  125. t.Error(err)
  126. }
  127. if data == nil {
  128. t.Error("Data shouldn't be nil")
  129. }
  130. content, ok := data["Content-Type"]
  131. if !ok {
  132. t.Error("There's no Content-Type in header!")
  133. }
  134. if len(content) == 0 {
  135. t.Error("There's no value set for Content-Type!")
  136. }
  137. if content[0] != "text" {
  138. t.Errorf("Content-Type should be set to 'text', but is '%s'!", content[0])
  139. }
  140. }