4
0

utils_test.go 3.1 KB

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