read_test.go 3.7 KB

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