github.com/pion/webrtc/v3@v3.2.24/icecandidateinit_test.go (about)

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