Skip to content

Commit bd88501

Browse files
committed
Added new test
1 parent 324ebb2 commit bd88501

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

internal/iot/client_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package iot
2+
3+
import (
4+
"testing"
5+
6+
iotclient "github.com/arduino/iot-client-go/v3"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestJSON_UnknownFields_areAccepted(t *testing.T) {
11+
12+
cert := iotclient.ArduinoDevicev2Cert{}
13+
14+
// Add unknown fields to the JSON and verify that marshalling and unmarshalling works without raising error.
15+
// This is useful when the API is extended with new fields and the client is not updated yet.
16+
certJson := `{
17+
"compressed": {
18+
"not_after": "0001-01-01T00:00:00Z",
19+
"not_before": "0001-01-01T00:00:00Z",
20+
"serial": "",
21+
"signature": "signature",
22+
"signature_asn1_x": "",
23+
"signature_asn1_y": ""
24+
},
25+
"der": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA",
26+
"device_id": "123",
27+
"enabled": true,
28+
"href": "",
29+
"id": "",
30+
"pem": "-----BEGIN CERTIFICATE-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA",
31+
"unknown_field": "value",
32+
"unknown_field2": "value2",
33+
"new_api_field": 2222
34+
}`
35+
36+
err := cert.UnmarshalJSON([]byte(certJson))
37+
if err != nil {
38+
t.Errorf("UnmarshalJSON failed: %s", err)
39+
}
40+
assert.Equal(t, 3, len(cert.AdditionalProperties))
41+
}

0 commit comments

Comments
 (0)