api_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. const (
  11. ty = "Thank you!"
  12. deny = "That's not true!"
  13. )
  14. func simpleHandle(w http.ResponseWriter, r *http.Request) error {
  15. _, _, err := ProcessRequestOneOf(r, [][]string{
  16. {"compliment"},
  17. {"critique"},
  18. })
  19. if err != nil {
  20. return err
  21. }
  22. return SendResponse(w, ty)
  23. }
  24. func cleverHandle(w http.ResponseWriter, r *http.Request) error {
  25. _, matched, err := ProcessRequestFirstMatchOf(r, [][]string{
  26. {"compliment"},
  27. {"critique"},
  28. })
  29. if err != nil {
  30. return err
  31. }
  32. if matched[0] == "critique" {
  33. return SendResponse(w, deny)
  34. }
  35. return SendResponse(w, ty)
  36. }
  37. func post(t *testing.T, obj map[string]interface{}, ts *httptest.Server) (resp *http.Response, body []byte) {
  38. blob, err := json.Marshal(obj)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. resp, err = http.Post(ts.URL, "application/json", bytes.NewReader(blob))
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. body, err = ioutil.ReadAll(resp.Body)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. return
  51. }
  52. func get(t *testing.T, ts *httptest.Server) (resp *http.Response, body []byte) {
  53. resp, err := http.Get(ts.URL)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. body, err = ioutil.ReadAll(resp.Body)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. return
  62. }
  63. func TestRigidHandle(t *testing.T) {
  64. ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(simpleHandle), Methods: []string{"POST"}})
  65. defer ts.Close()
  66. // Response to compliment
  67. obj := map[string]interface{}{}
  68. obj["compliment"] = "it's good"
  69. resp, body := post(t, obj, ts)
  70. if resp.StatusCode != http.StatusOK {
  71. t.Errorf("Test expected 200, have %d", resp.StatusCode)
  72. }
  73. message := new(Response)
  74. err := json.Unmarshal(body, message)
  75. if err != nil {
  76. t.Errorf("failed to read response body: %v", err)
  77. t.Fatal("returned:", message)
  78. }
  79. if message.Result != ty {
  80. t.Fatal("Wrong response")
  81. }
  82. // Response to critique
  83. obj = map[string]interface{}{}
  84. obj["critique"] = "it's bad"
  85. resp, body = post(t, obj, ts)
  86. if resp.StatusCode != http.StatusOK {
  87. t.Errorf("Test expected 200, have %d", resp.StatusCode)
  88. }
  89. message = new(Response)
  90. err = json.Unmarshal(body, message)
  91. if err != nil {
  92. t.Errorf("failed to read response body: %v", err)
  93. t.Fatal("returned:", message)
  94. }
  95. if message.Result != ty {
  96. t.Fatal("Wrong response")
  97. }
  98. // reject mixed review
  99. obj = map[string]interface{}{}
  100. obj["critique"] = "it's OK"
  101. obj["compliment"] = "it's not bad"
  102. resp, _ = post(t, obj, ts)
  103. if resp.StatusCode != http.StatusBadRequest {
  104. t.Errorf("Test expected 400, have %d", resp.StatusCode)
  105. }
  106. // reject empty review
  107. obj = map[string]interface{}{}
  108. resp, _ = post(t, obj, ts)
  109. if resp.StatusCode != http.StatusBadRequest {
  110. t.Errorf("Test expected 400, have %d", resp.StatusCode)
  111. }
  112. // reject GET
  113. resp, _ = get(t, ts)
  114. if resp.StatusCode != http.StatusMethodNotAllowed {
  115. t.Errorf("Test expected 405, have %d", resp.StatusCode)
  116. }
  117. }
  118. func TestCleverHandle(t *testing.T) {
  119. ts := httptest.NewServer(HTTPHandler{Handler: HandlerFunc(cleverHandle), Methods: []string{"POST"}})
  120. defer ts.Close()
  121. // Response ty to compliment
  122. obj := map[string]interface{}{}
  123. obj["compliment"] = "it's good"
  124. resp, body := post(t, obj, ts)
  125. if resp.StatusCode != http.StatusOK {
  126. t.Errorf("Test expected 200, have %d", resp.StatusCode)
  127. }
  128. message := new(Response)
  129. err := json.Unmarshal(body, message)
  130. if err != nil {
  131. t.Errorf("failed to read response body: %v", err)
  132. t.Fatal("returned:", message)
  133. }
  134. if message.Result != ty {
  135. t.Fatal("Wrong response")
  136. }
  137. // Response deny to critique
  138. obj = map[string]interface{}{}
  139. obj["critique"] = "it's bad"
  140. resp, body = post(t, obj, ts)
  141. if resp.StatusCode != http.StatusOK {
  142. t.Errorf("Test expected 200, have %d", resp.StatusCode)
  143. }
  144. message = new(Response)
  145. err = json.Unmarshal(body, message)
  146. if err != nil {
  147. t.Errorf("failed to read response body: %v", err)
  148. t.Fatal("returned:", message)
  149. }
  150. if message.Result != deny {
  151. t.Fatal("Wrong response")
  152. }
  153. // Be polite to mixed review
  154. obj = map[string]interface{}{}
  155. obj["critique"] = "it's OK"
  156. obj["compliment"] = "it's not bad"
  157. _, body = post(t, obj, ts)
  158. message = new(Response)
  159. err = json.Unmarshal(body, message)
  160. if err != nil {
  161. t.Errorf("failed to read response body: %v", err)
  162. t.Fatal("returned:", message)
  163. }
  164. if message.Result != ty {
  165. t.Fatal("Wrong response")
  166. }
  167. // reject empty review
  168. obj = map[string]interface{}{}
  169. resp, _ = post(t, obj, ts)
  170. if resp.StatusCode != http.StatusBadRequest {
  171. t.Errorf("Test expected 400, have %d", resp.StatusCode)
  172. }
  173. // reject GET
  174. resp, _ = get(t, ts)
  175. if resp.StatusCode != http.StatusMethodNotAllowed {
  176. t.Errorf("Test expected 405, have %d", resp.StatusCode)
  177. }
  178. }