|
1 | 1 | package read
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "errors" |
4 | 6 | "io"
|
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
5 | 10 | "reflect"
|
6 | 11 | "strings"
|
7 | 12 | "testing"
|
| 13 | + "testing/iotest" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "google.golang.org/protobuf/proto" |
| 17 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 18 | + |
| 19 | + "go.step.sm/linkedca" |
8 | 20 |
|
9 | 21 | "github.com/smallstep/certificates/errs"
|
10 | 22 | )
|
@@ -44,3 +56,110 @@ func TestJSON(t *testing.T) {
|
44 | 56 | })
|
45 | 57 | }
|
46 | 58 | }
|
| 59 | + |
| 60 | +func TestProtoJSON(t *testing.T) { |
| 61 | + |
| 62 | + p := new(linkedca.Policy) // TODO(hs): can we use something different, so we don't need the import? |
| 63 | + |
| 64 | + type args struct { |
| 65 | + r io.Reader |
| 66 | + m proto.Message |
| 67 | + } |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + args args |
| 71 | + wantErr bool |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "fail/io.ReadAll", |
| 75 | + args: args{ |
| 76 | + r: iotest.ErrReader(errors.New("read error")), |
| 77 | + m: p, |
| 78 | + }, |
| 79 | + wantErr: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "fail/proto", |
| 83 | + args: args{ |
| 84 | + r: strings.NewReader(`{?}`), |
| 85 | + m: p, |
| 86 | + }, |
| 87 | + wantErr: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "ok", |
| 91 | + args: args{ |
| 92 | + r: strings.NewReader(`{"x509":{}}`), |
| 93 | + m: p, |
| 94 | + }, |
| 95 | + wantErr: false, |
| 96 | + }, |
| 97 | + } |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + err := ProtoJSON(tt.args.r, tt.args.m) |
| 101 | + if (err != nil) != tt.wantErr { |
| 102 | + t.Errorf("ProtoJSON() error = %v, wantErr %v", err, tt.wantErr) |
| 103 | + } |
| 104 | + |
| 105 | + if tt.wantErr { |
| 106 | + switch err.(type) { |
| 107 | + case badProtoJSONError: |
| 108 | + assert.Contains(t, err.Error(), "syntax error") |
| 109 | + case *errs.Error: |
| 110 | + var ee *errs.Error |
| 111 | + if errors.As(err, &ee) { |
| 112 | + assert.Equal(t, http.StatusBadRequest, ee.Status) |
| 113 | + } |
| 114 | + } |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + assert.Equal(t, protoreflect.FullName("linkedca.Policy"), proto.MessageName(tt.args.m)) |
| 119 | + assert.True(t, proto.Equal(&linkedca.Policy{X509: &linkedca.X509Policy{}}, tt.args.m)) |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func Test_badProtoJSONError_Render(t *testing.T) { |
| 125 | + tests := []struct { |
| 126 | + name string |
| 127 | + e badProtoJSONError |
| 128 | + expected string |
| 129 | + }{ |
| 130 | + { |
| 131 | + name: "bad proto normal space", |
| 132 | + e: badProtoJSONError("proto: syntax error (line 1:2): invalid value ?"), |
| 133 | + expected: "syntax error (line 1:2): invalid value ?", |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "bad proto non breaking space", |
| 137 | + e: badProtoJSONError("proto: syntax error (line 1:2): invalid value ?"), |
| 138 | + expected: "syntax error (line 1:2): invalid value ?", |
| 139 | + }, |
| 140 | + } |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + |
| 144 | + w := httptest.NewRecorder() |
| 145 | + tt.e.Render(w) |
| 146 | + res := w.Result() |
| 147 | + defer res.Body.Close() |
| 148 | + |
| 149 | + data, err := ioutil.ReadAll(res.Body) |
| 150 | + assert.NoError(t, err) |
| 151 | + |
| 152 | + v := struct { |
| 153 | + Type string `json:"type"` |
| 154 | + Detail string `json:"detail"` |
| 155 | + Message string `json:"message"` |
| 156 | + }{} |
| 157 | + |
| 158 | + assert.NoError(t, json.Unmarshal(data, &v)) |
| 159 | + assert.Equal(t, "badRequest", v.Type) |
| 160 | + assert.Equal(t, "bad request", v.Detail) |
| 161 | + assert.Equal(t, "syntax error (line 1:2): invalid value ?", v.Message) |
| 162 | + |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments