github.com/pion/webrtc/v4@v4.0.1/configuration_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 TestConfiguration_getICEServers(t *testing.T) {
    14  	t.Run("Success", func(t *testing.T) {
    15  		expectedServerStr := "stun:stun.l.google.com:19302"
    16  		cfg := Configuration{
    17  			ICEServers: []ICEServer{
    18  				{
    19  					URLs: []string{expectedServerStr},
    20  				},
    21  			},
    22  		}
    23  
    24  		parsedURLs := cfg.getICEServers()
    25  		assert.Equal(t, expectedServerStr, parsedURLs[0].URLs[0])
    26  	})
    27  
    28  	t.Run("Success", func(t *testing.T) {
    29  		// ignore the fact that stun URLs shouldn't have a query
    30  		serverStr := "stun:global.stun.twilio.com:3478?transport=udp"
    31  		expectedServerStr := "stun:global.stun.twilio.com:3478"
    32  		cfg := Configuration{
    33  			ICEServers: []ICEServer{
    34  				{
    35  					URLs: []string{serverStr},
    36  				},
    37  			},
    38  		}
    39  
    40  		parsedURLs := cfg.getICEServers()
    41  		assert.Equal(t, expectedServerStr, parsedURLs[0].URLs[0])
    42  	})
    43  }
    44  
    45  func TestConfigurationJSON(t *testing.T) {
    46  	j := `{
    47      "iceServers": [{"urls": ["turn:turn.example.org"],
    48                      "username": "jch",
    49                      "credential": "topsecret"
    50                    }],
    51      "iceTransportPolicy": "relay",
    52      "bundlePolicy": "balanced",
    53      "rtcpMuxPolicy": "require"
    54  }`
    55  
    56  	conf := Configuration{
    57  		ICEServers: []ICEServer{
    58  			{
    59  				URLs:       []string{"turn:turn.example.org"},
    60  				Username:   "jch",
    61  				Credential: "topsecret",
    62  			},
    63  		},
    64  		ICETransportPolicy: ICETransportPolicyRelay,
    65  		BundlePolicy:       BundlePolicyBalanced,
    66  		RTCPMuxPolicy:      RTCPMuxPolicyRequire,
    67  	}
    68  
    69  	var conf2 Configuration
    70  	assert.NoError(t, json.Unmarshal([]byte(j), &conf2))
    71  	assert.Equal(t, conf, conf2)
    72  
    73  	j2, err := json.Marshal(conf2)
    74  	assert.NoError(t, err)
    75  
    76  	var conf3 Configuration
    77  	assert.NoError(t, json.Unmarshal(j2, &conf3))
    78  	assert.Equal(t, conf2, conf3)
    79  }