fmtp_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package fmtp
  4. import (
  5. "reflect"
  6. "testing"
  7. )
  8. func TestGenericParseFmtp(t *testing.T) {
  9. testCases := map[string]struct {
  10. input string
  11. expected FMTP
  12. }{
  13. "OneParam": {
  14. input: "key-name=value",
  15. expected: &genericFMTP{
  16. mimeType: "generic",
  17. parameters: map[string]string{
  18. "key-name": "value",
  19. },
  20. },
  21. },
  22. "OneParamWithWhiteSpeces": {
  23. input: "\tkey-name=value ",
  24. expected: &genericFMTP{
  25. mimeType: "generic",
  26. parameters: map[string]string{
  27. "key-name": "value",
  28. },
  29. },
  30. },
  31. "TwoParams": {
  32. input: "key-name=value;key2=value2",
  33. expected: &genericFMTP{
  34. mimeType: "generic",
  35. parameters: map[string]string{
  36. "key-name": "value",
  37. "key2": "value2",
  38. },
  39. },
  40. },
  41. "TwoParamsWithWhiteSpeces": {
  42. input: "key-name=value; \n\tkey2=value2 ",
  43. expected: &genericFMTP{
  44. mimeType: "generic",
  45. parameters: map[string]string{
  46. "key-name": "value",
  47. "key2": "value2",
  48. },
  49. },
  50. },
  51. }
  52. for name, testCase := range testCases {
  53. testCase := testCase
  54. t.Run(name, func(t *testing.T) {
  55. f := Parse("generic", testCase.input)
  56. if !reflect.DeepEqual(testCase.expected, f) {
  57. t.Errorf("Expected Fmtp params: %v, got: %v", testCase.expected, f)
  58. }
  59. if f.MimeType() != "generic" {
  60. t.Errorf("Expected MimeType of generic, got: %s", f.MimeType())
  61. }
  62. })
  63. }
  64. }
  65. func TestGenericFmtpCompare(t *testing.T) {
  66. consistString := map[bool]string{true: "consist", false: "inconsist"}
  67. testCases := map[string]struct {
  68. a, b string
  69. consist bool
  70. }{
  71. "Equal": {
  72. a: "key1=value1;key2=value2;key3=value3",
  73. b: "key1=value1;key2=value2;key3=value3",
  74. consist: true,
  75. },
  76. "EqualWithWhitespaceVariants": {
  77. a: "key1=value1;key2=value2;key3=value3",
  78. b: " key1=value1; \nkey2=value2;\t\nkey3=value3",
  79. consist: true,
  80. },
  81. "EqualWithCase": {
  82. a: "key1=value1;key2=value2;key3=value3",
  83. b: "key1=value1;key2=Value2;Key3=value3",
  84. consist: true,
  85. },
  86. "OneHasExtraParam": {
  87. a: "key1=value1;key2=value2;key3=value3",
  88. b: "key1=value1;key2=value2;key3=value3;key4=value4",
  89. consist: true,
  90. },
  91. "Inconsistent": {
  92. a: "key1=value1;key2=value2;key3=value3",
  93. b: "key1=value1;key2=different_value;key3=value3",
  94. consist: false,
  95. },
  96. "Inconsistent_OneHasExtraParam": {
  97. a: "key1=value1;key2=value2;key3=value3;key4=value4",
  98. b: "key1=value1;key2=different_value;key3=value3",
  99. consist: false,
  100. },
  101. }
  102. for name, testCase := range testCases {
  103. testCase := testCase
  104. check := func(t *testing.T, a, b string) {
  105. aa := Parse("", a)
  106. bb := Parse("", b)
  107. c := aa.Match(bb)
  108. if c != testCase.consist {
  109. t.Errorf(
  110. "'%s' and '%s' are expected to be %s, but treated as %s",
  111. a, b, consistString[testCase.consist], consistString[c],
  112. )
  113. }
  114. // test reverse case here
  115. c = bb.Match(aa)
  116. if c != testCase.consist {
  117. t.Errorf(
  118. "'%s' and '%s' are expected to be %s, but treated as %s",
  119. a, b, consistString[testCase.consist], consistString[c],
  120. )
  121. }
  122. }
  123. t.Run(name, func(t *testing.T) {
  124. check(t, testCase.a, testCase.b)
  125. })
  126. }
  127. }