scope_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package app_test
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "path/filepath"
  9. "reflect"
  10. "runtime"
  11. "testing"
  12. )
  13. // assert fails the test if the condition is false.
  14. func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
  15. if !condition {
  16. _, file, line, _ := runtime.Caller(1)
  17. tb.Fatalf("%s:%d: "+msg, append([]interface{}{filepath.Base(file), line}, v...)...)
  18. }
  19. }
  20. // ok errors the test if an err is not nil.
  21. func ok(tb testing.TB, err error) {
  22. if err != nil {
  23. _, file, line, _ := runtime.Caller(1)
  24. tb.Errorf("%s:%d: unexpected error: %v", filepath.Base(file), line, err)
  25. }
  26. }
  27. // equals errors the test if want is not equal to have.
  28. func equals(tb testing.TB, want, have interface{}) {
  29. if !reflect.DeepEqual(want, have) {
  30. _, file, line, _ := runtime.Caller(1)
  31. tb.Errorf("%s:%d: want %#v, have %#v", filepath.Base(file), line, want, have)
  32. }
  33. }
  34. // checkGet does a GET and returns the response and the body
  35. func checkGet(t *testing.T, ts *httptest.Server, path string) (*http.Response, []byte) {
  36. return checkRequest(t, ts, "GET", path, nil)
  37. }
  38. // checkRequest does a 'method'-request (e.g. 'GET') and returns the response and the body
  39. func checkRequest(t *testing.T, ts *httptest.Server, method, path string, body []byte) (*http.Response, []byte) {
  40. fullPath := ts.URL + path
  41. var bodyReader io.Reader
  42. if len(body) > 0 {
  43. bodyReader = bytes.NewReader(body)
  44. }
  45. req, err := http.NewRequest(method, fullPath, bodyReader)
  46. if err != nil {
  47. t.Fatalf("Error getting %s: %s %s", method, path, err)
  48. }
  49. req.Header.Set("Content-Type", "application/msgpack")
  50. client := &http.Client{}
  51. res, err := client.Do(req)
  52. if err != nil {
  53. t.Fatalf("Error getting %s %s: %s", method, path, err)
  54. }
  55. body, err = ioutil.ReadAll(res.Body)
  56. res.Body.Close()
  57. if err != nil {
  58. t.Fatalf("%s %s body read error: %s", method, path, err)
  59. }
  60. return res, body
  61. }
  62. // getRawJSON GETs a file, checks it is JSON, and returns the non-parsed body
  63. func getRawJSON(t *testing.T, ts *httptest.Server, path string) []byte {
  64. res, body := checkGet(t, ts, path)
  65. _, file, line, _ := runtime.Caller(1)
  66. file = filepath.Base(file)
  67. if res.StatusCode != 200 {
  68. t.Fatalf("%s:%d: Expected status %d, got %d. Path: %s", file, line, 200, res.StatusCode, path)
  69. }
  70. foundCtype := res.Header.Get("content-type")
  71. if foundCtype != "application/json" {
  72. t.Errorf("%s:%d: Wrong Content-type for JSON: %s", file, line, foundCtype)
  73. }
  74. if len(body) == 0 {
  75. t.Errorf("%s:%d: No response body", file, line)
  76. }
  77. // fmt.Printf("Body: %s", body)
  78. return body
  79. }
  80. // is200 GETs path and verifies the status code. Returns the body
  81. func is200(t *testing.T, ts *httptest.Server, path string) []byte {
  82. res, body := checkGet(t, ts, path)
  83. if res.StatusCode != 200 {
  84. t.Fatalf("Expected status %d, got %d. Path: %s", 200, res.StatusCode, path)
  85. }
  86. return body
  87. }
  88. // is404 GETs path and verifies it returns a 404 status code. Returns the body
  89. func is404(t *testing.T, ts *httptest.Server, path string) []byte {
  90. res, body := checkGet(t, ts, path)
  91. if res.StatusCode != 404 {
  92. t.Fatalf("Expected status %d, got %d", 404, res.StatusCode)
  93. }
  94. return body
  95. }
  96. // is400 GETs path and verifies it returns a 400 status code. Returns the body
  97. func is400(t *testing.T, ts *httptest.Server, path string) []byte {
  98. res, body := checkGet(t, ts, path)
  99. if res.StatusCode != 400 {
  100. t.Fatalf("Expected status %d, got %d", 400, res.StatusCode)
  101. }
  102. return body
  103. }