utils_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "io"
  4. "net/http"
  5. "net/http/httptest"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/api/read"
  11. "github.com/smallstep/certificates/errs"
  12. "github.com/smallstep/certificates/logging"
  13. )
  14. func TestLogError(t *testing.T) {
  15. theError := errors.New("the error")
  16. type args struct {
  17. rw http.ResponseWriter
  18. err error
  19. }
  20. tests := []struct {
  21. name string
  22. args args
  23. withFields bool
  24. }{
  25. {"normalLogger", args{httptest.NewRecorder(), theError}, false},
  26. {"responseLogger", args{logging.NewResponseLogger(httptest.NewRecorder()), theError}, true},
  27. }
  28. for _, tt := range tests {
  29. t.Run(tt.name, func(t *testing.T) {
  30. LogError(tt.args.rw, tt.args.err)
  31. if tt.withFields {
  32. if rl, ok := tt.args.rw.(logging.ResponseLogger); ok {
  33. fields := rl.Fields()
  34. if !reflect.DeepEqual(fields["error"], theError) {
  35. t.Errorf("ResponseLogger[\"error\"] = %s, wants %s", fields["error"], theError)
  36. }
  37. } else {
  38. t.Error("ResponseWriter does not implement logging.ResponseLogger")
  39. }
  40. }
  41. })
  42. }
  43. }
  44. func TestJSON(t *testing.T) {
  45. type args struct {
  46. rw http.ResponseWriter
  47. v interface{}
  48. }
  49. tests := []struct {
  50. name string
  51. args args
  52. ok bool
  53. }{
  54. {"ok", args{httptest.NewRecorder(), map[string]interface{}{"foo": "bar"}}, true},
  55. {"fail", args{httptest.NewRecorder(), make(chan int)}, false},
  56. }
  57. for _, tt := range tests {
  58. t.Run(tt.name, func(t *testing.T) {
  59. rw := logging.NewResponseLogger(tt.args.rw)
  60. JSON(rw, tt.args.v)
  61. rr, ok := tt.args.rw.(*httptest.ResponseRecorder)
  62. if !ok {
  63. t.Error("ResponseWriter does not implement *httptest.ResponseRecorder")
  64. return
  65. }
  66. fields := rw.Fields()
  67. if tt.ok {
  68. if body := rr.Body.String(); body != "{\"foo\":\"bar\"}\n" {
  69. t.Errorf(`Unexpected body = %v, want {"foo":"bar"}`, body)
  70. }
  71. if len(fields) != 0 {
  72. t.Errorf("ResponseLogger fields = %v, wants 0 elements", fields)
  73. }
  74. } else {
  75. if body := rr.Body.String(); body != "" {
  76. t.Errorf("Unexpected body = %s, want empty string", body)
  77. }
  78. if len(fields) != 1 {
  79. t.Errorf("ResponseLogger fields = %v, wants 1 element", fields)
  80. }
  81. }
  82. })
  83. }
  84. }
  85. func TestReadJSON(t *testing.T) {
  86. type args struct {
  87. r io.Reader
  88. v interface{}
  89. }
  90. tests := []struct {
  91. name string
  92. args args
  93. wantErr bool
  94. }{
  95. {"ok", args{strings.NewReader(`{"foo":"bar"}`), make(map[string]interface{})}, false},
  96. {"fail", args{strings.NewReader(`{"foo"}`), make(map[string]interface{})}, true},
  97. }
  98. for _, tt := range tests {
  99. t.Run(tt.name, func(t *testing.T) {
  100. err := read.JSON(tt.args.r, &tt.args.v)
  101. if (err != nil) != tt.wantErr {
  102. t.Errorf("ReadJSON() error = %v, wantErr %v", err, tt.wantErr)
  103. }
  104. if tt.wantErr {
  105. e, ok := err.(*errs.Error)
  106. if ok {
  107. if code := e.StatusCode(); code != 400 {
  108. t.Errorf("error.StatusCode() = %v, wants 400", code)
  109. }
  110. } else {
  111. t.Errorf("error type = %T, wants *Error", err)
  112. }
  113. } else if !reflect.DeepEqual(tt.args.v, map[string]interface{}{"foo": "bar"}) {
  114. t.Errorf("ReadJSON value = %v, wants %v", tt.args.v, map[string]interface{}{"foo": "bar"})
  115. }
  116. })
  117. }
  118. }