configuration_test.go 1.8 KB

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