utils_test.go 3.0 KB

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