| 126 | } |
| 127 | |
| 128 | func TestJSONCodec(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | |
| 131 | codec := &protoJSONCodec{name: codecNameJSON} |
| 132 | |
| 133 | t.Run("success", func(t *testing.T) { |
| 134 | t.Parallel() |
| 135 | err := codec.Unmarshal([]byte("{}"), &emptypb.Empty{}) |
| 136 | assert.Nil(t, err) |
| 137 | }) |
| 138 | |
| 139 | t.Run("unknown fields", func(t *testing.T) { |
| 140 | t.Parallel() |
| 141 | err := codec.Unmarshal([]byte(`{"foo": "bar"}`), &emptypb.Empty{}) |
| 142 | assert.Nil(t, err) |
| 143 | }) |
| 144 | |
| 145 | t.Run("empty string", func(t *testing.T) { |
| 146 | t.Parallel() |
| 147 | err := codec.Unmarshal([]byte{}, &emptypb.Empty{}) |
| 148 | assert.NotNil(t, err) |
| 149 | assert.True( |
| 150 | t, |
| 151 | strings.Contains(err.Error(), "valid JSON"), |
| 152 | assert.Sprintf(`error message should explain that "" is not a valid JSON object`), |
| 153 | ) |
| 154 | }) |
| 155 | } |