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