icecandidateinit_test.go 1.3 KB

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