|
| 1 | +package webrtc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestICECandidateInit_Serialization(t *testing.T) { |
| 11 | + tt := []struct { |
| 12 | + candidate ICECandidateInit |
| 13 | + serialized string |
| 14 | + }{ |
| 15 | + {ICECandidateInit{ |
| 16 | + Candidate: "candidate:abc123", |
| 17 | + SDPMid: refString("0"), |
| 18 | + SDPMLineIndex: refUint16(0), |
| 19 | + UsernameFragment: "def", |
| 20 | + }, `{"candidate":"candidate:abc123","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"def"}`}, |
| 21 | + {ICECandidateInit{ |
| 22 | + Candidate: "candidate:abc123", |
| 23 | + UsernameFragment: "def", |
| 24 | + }, `{"candidate":"candidate:abc123","usernameFragment":"def"}`}, |
| 25 | + } |
| 26 | + |
| 27 | + for i, tc := range tt { |
| 28 | + b, err := json.Marshal(tc.candidate) |
| 29 | + if err != nil { |
| 30 | + t.Errorf("Failed to marshal %d: %v", i, err) |
| 31 | + } |
| 32 | + actualSerialized := string(b) |
| 33 | + if actualSerialized != tc.serialized { |
| 34 | + t.Errorf("%d expected %s got %s", i, tc.serialized, actualSerialized) |
| 35 | + } |
| 36 | + |
| 37 | + var actual ICECandidateInit |
| 38 | + err = json.Unmarshal(b, &actual) |
| 39 | + if err != nil { |
| 40 | + t.Errorf("Failed to unmarshal %d: %v", i, err) |
| 41 | + } |
| 42 | + |
| 43 | + assert.Equal(t, tc.candidate, actual, "should match") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func refString(s string) *string { |
| 48 | + return &s |
| 49 | +} |
| 50 | + |
| 51 | +func refUint16(i uint16) *uint16 { |
| 52 | + return &i |
| 53 | +} |
0 commit comments