read_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package read
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. "testing/iotest"
  12. "github.com/stretchr/testify/assert"
  13. "google.golang.org/protobuf/proto"
  14. "google.golang.org/protobuf/reflect/protoreflect"
  15. "github.com/smallstep/linkedca"
  16. "github.com/smallstep/certificates/errs"
  17. )
  18. func TestJSON(t *testing.T) {
  19. type args struct {
  20. r io.Reader
  21. v any
  22. }
  23. tests := []struct {
  24. name string
  25. args args
  26. wantErr bool
  27. }{
  28. {"ok", args{strings.NewReader(`{"foo":"bar"}`), make(map[string]any)}, false},
  29. {"fail", args{strings.NewReader(`{"foo"}`), make(map[string]any)}, true},
  30. }
  31. for _, tt := range tests {
  32. t.Run(tt.name, func(t *testing.T) {
  33. err := JSON(tt.args.r, &tt.args.v)
  34. if (err != nil) != tt.wantErr {
  35. t.Errorf("JSON() error = %v, wantErr %v", err, tt.wantErr)
  36. }
  37. if tt.wantErr {
  38. if e, ok := errors.AsType[*errs.Error](err); ok {
  39. if code := e.StatusCode(); code != 400 {
  40. t.Errorf("error.StatusCode() = %v, wants 400", code)
  41. }
  42. } else {
  43. t.Errorf("error type = %T, wants *Error", err)
  44. }
  45. } else if !reflect.DeepEqual(tt.args.v, map[string]any{"foo": "bar"}) {
  46. t.Errorf("JSON value = %v, wants %v", tt.args.v, map[string]any{"foo": "bar"})
  47. }
  48. })
  49. }
  50. }
  51. func TestProtoJSON(t *testing.T) {
  52. p := new(linkedca.Policy) // TODO(hs): can we use something different, so we don't need the import?
  53. type args struct {
  54. r io.Reader
  55. m proto.Message
  56. }
  57. tests := []struct {
  58. name string
  59. args args
  60. wantErr bool
  61. }{
  62. {
  63. name: "fail/io.ReadAll",
  64. args: args{
  65. r: iotest.ErrReader(errors.New("read error")),
  66. m: p,
  67. },
  68. wantErr: true,
  69. },
  70. {
  71. name: "fail/proto",
  72. args: args{
  73. r: strings.NewReader(`{?}`),
  74. m: p,
  75. },
  76. wantErr: true,
  77. },
  78. {
  79. name: "ok",
  80. args: args{
  81. r: strings.NewReader(`{"x509":{}}`),
  82. m: p,
  83. },
  84. wantErr: false,
  85. },
  86. }
  87. for _, tt := range tests {
  88. t.Run(tt.name, func(t *testing.T) {
  89. err := ProtoJSON(tt.args.r, tt.args.m)
  90. if (err != nil) != tt.wantErr {
  91. t.Errorf("ProtoJSON() error = %v, wantErr %v", err, tt.wantErr)
  92. }
  93. if tt.wantErr {
  94. var (
  95. ee *errs.Error
  96. bpe badProtoJSONError
  97. )
  98. switch {
  99. case errors.As(err, &bpe):
  100. assert.Contains(t, err.Error(), "syntax error")
  101. case errors.As(err, &ee):
  102. assert.Equal(t, http.StatusBadRequest, ee.Status)
  103. }
  104. return
  105. }
  106. assert.Equal(t, protoreflect.FullName("linkedca.Policy"), proto.MessageName(tt.args.m))
  107. assert.True(t, proto.Equal(&linkedca.Policy{X509: &linkedca.X509Policy{}}, tt.args.m))
  108. })
  109. }
  110. }
  111. func Test_badProtoJSONError_Render(t *testing.T) {
  112. tests := []struct {
  113. name string
  114. e badProtoJSONError
  115. expected string
  116. }{
  117. {
  118. name: "bad proto normal space",
  119. e: badProtoJSONError("proto: syntax error (line 1:2): invalid value ?"),
  120. expected: "syntax error (line 1:2): invalid value ?",
  121. },
  122. {
  123. name: "bad proto non breaking space",
  124. e: badProtoJSONError("proto: syntax error (line 1:2): invalid value ?"),
  125. expected: "syntax error (line 1:2): invalid value ?",
  126. },
  127. }
  128. for _, tt := range tests {
  129. t.Run(tt.name, func(t *testing.T) {
  130. w := httptest.NewRecorder()
  131. r := httptest.NewRequest("POST", "/test", http.NoBody)
  132. tt.e.Render(w, r)
  133. res := w.Result()
  134. defer res.Body.Close()
  135. data, err := io.ReadAll(res.Body)
  136. assert.NoError(t, err)
  137. v := struct {
  138. Type string `json:"type"`
  139. Detail string `json:"detail"`
  140. Message string `json:"message"`
  141. }{}
  142. assert.NoError(t, json.Unmarshal(data, &v))
  143. assert.Equal(t, "badRequest", v.Type)
  144. assert.Equal(t, "bad request", v.Detail)
  145. assert.Equal(t, "syntax error (line 1:2): invalid value ?", v.Message)
  146. })
  147. }
  148. }