|
4 | 4 | package httpsfv
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "slices" |
7 | 8 | "strconv"
|
8 | 9 | "strings"
|
9 | 10 | "testing"
|
10 | 11 | )
|
11 | 12 |
|
| 13 | +func TestConsumeBareInnerList(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + in string |
| 17 | + wantBareItems []string |
| 18 | + wantParams []string |
| 19 | + wantListParam string |
| 20 | + wantOk bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "valid inner list without param", |
| 24 | + in: `(a b c)`, |
| 25 | + wantBareItems: []string{"a", "b", "c"}, |
| 26 | + wantParams: []string{"", "", ""}, |
| 27 | + wantOk: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "valid inner list with param", |
| 31 | + in: `(a;d b c;e)`, |
| 32 | + wantBareItems: []string{"a", "b", "c"}, |
| 33 | + wantParams: []string{";d", "", ";e"}, |
| 34 | + wantOk: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "valid inner list with fake ending parenthesis", |
| 38 | + in: `(")";foo=")")`, |
| 39 | + wantBareItems: []string{`")"`}, |
| 40 | + wantParams: []string{`;foo=")"`}, |
| 41 | + wantOk: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "valid inner list with list parameter", |
| 45 | + in: `(a b;c); d`, |
| 46 | + wantBareItems: []string{"a", "b"}, |
| 47 | + wantParams: []string{"", ";c"}, |
| 48 | + wantOk: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "valid inner list with more content after", |
| 52 | + in: `(a b;c); d, a`, |
| 53 | + wantBareItems: []string{"a", "b"}, |
| 54 | + wantParams: []string{"", ";c"}, |
| 55 | + wantOk: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "invalid inner list", |
| 59 | + in: `(a b;c `, |
| 60 | + wantBareItems: []string{"a", "b"}, |
| 61 | + wantParams: []string{"", ";c"}, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + var gotBareItems, gotParams []string |
| 67 | + f := func(bareItem, param string) { |
| 68 | + gotBareItems = append(gotBareItems, bareItem) |
| 69 | + gotParams = append(gotParams, param) |
| 70 | + } |
| 71 | + gotConsumed, gotRest, ok := consumeBareInnerList(tc.in, f) |
| 72 | + if ok != tc.wantOk { |
| 73 | + t.Fatalf("test %q: want ok to be %v, got: %v", tc.name, tc.wantOk, ok) |
| 74 | + } |
| 75 | + if !slices.Equal(tc.wantBareItems, gotBareItems) { |
| 76 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotBareItems, tc.wantBareItems) |
| 77 | + } |
| 78 | + if !slices.Equal(tc.wantParams, gotParams) { |
| 79 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotParams, tc.wantParams) |
| 80 | + } |
| 81 | + if gotConsumed+gotRest != tc.in { |
| 82 | + t.Fatalf("test %q: %#v + %#v != %#v", tc.name, gotConsumed, gotRest, tc.in) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestParseBareInnerList(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + in string |
| 91 | + wantBareItems []string |
| 92 | + wantParams []string |
| 93 | + wantOk bool |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "valid inner list", |
| 97 | + in: `(a b;c)`, |
| 98 | + wantBareItems: []string{"a", "b"}, |
| 99 | + wantParams: []string{"", ";c"}, |
| 100 | + wantOk: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "valid inner list with list parameter", |
| 104 | + in: `(a b;c); d`, |
| 105 | + wantBareItems: []string{"a", "b"}, |
| 106 | + wantParams: []string{"", ";c"}, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "invalid inner list", |
| 110 | + in: `(a b;c `, |
| 111 | + wantBareItems: []string{"a", "b"}, |
| 112 | + wantParams: []string{"", ";c"}, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tc := range tests { |
| 117 | + var gotBareItems, gotParams []string |
| 118 | + f := func(bareItem, param string) { |
| 119 | + gotBareItems = append(gotBareItems, bareItem) |
| 120 | + gotParams = append(gotParams, param) |
| 121 | + } |
| 122 | + ok := ParseBareInnerList(tc.in, f) |
| 123 | + if ok != tc.wantOk { |
| 124 | + t.Fatalf("test %q: want ok to be %v, got: %v", tc.name, tc.wantOk, ok) |
| 125 | + } |
| 126 | + if !slices.Equal(tc.wantBareItems, gotBareItems) { |
| 127 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotBareItems, tc.wantBareItems) |
| 128 | + } |
| 129 | + if !slices.Equal(tc.wantParams, gotParams) { |
| 130 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotParams, tc.wantParams) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestConsumeItem(t *testing.T) { |
| 136 | + tests := []struct { |
| 137 | + name string |
| 138 | + in string |
| 139 | + wantBareItem string |
| 140 | + wantParam string |
| 141 | + wantOk bool |
| 142 | + }{ |
| 143 | + { |
| 144 | + name: "valid bare item", |
| 145 | + in: `fookey`, |
| 146 | + wantBareItem: `fookey`, |
| 147 | + wantOk: true, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "valid bare item and param", |
| 151 | + in: `fookey; a="123"`, |
| 152 | + wantBareItem: `fookey`, |
| 153 | + wantParam: `; a="123"`, |
| 154 | + wantOk: true, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "valid item with content after", |
| 158 | + in: `fookey; a="123", otheritem; otherparam=1`, |
| 159 | + wantBareItem: `fookey`, |
| 160 | + wantParam: `; a="123"`, |
| 161 | + wantOk: true, |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "invalid just param", |
| 165 | + in: `;a="123"`, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + for _, tc := range tests { |
| 170 | + var gotBareItem, gotParam string |
| 171 | + f := func(bareItem, param string) { |
| 172 | + gotBareItem = bareItem |
| 173 | + gotParam = param |
| 174 | + } |
| 175 | + gotConsumed, gotRest, ok := consumeItem(tc.in, f) |
| 176 | + if ok != tc.wantOk { |
| 177 | + t.Fatalf("test %q: want ok to be %v, got: %v", tc.name, tc.wantOk, ok) |
| 178 | + } |
| 179 | + if tc.wantBareItem != gotBareItem { |
| 180 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotBareItem, tc.wantBareItem) |
| 181 | + } |
| 182 | + if tc.wantParam != gotParam { |
| 183 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotParam, tc.wantParam) |
| 184 | + } |
| 185 | + if gotConsumed+gotRest != tc.in { |
| 186 | + t.Fatalf("test %q: %#v + %#v != %#v", tc.name, gotConsumed, gotRest, tc.in) |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +func TestParseItem(t *testing.T) { |
| 192 | + tests := []struct { |
| 193 | + name string |
| 194 | + in string |
| 195 | + wantBareItem string |
| 196 | + wantParam string |
| 197 | + wantOk bool |
| 198 | + }{ |
| 199 | + { |
| 200 | + name: "valid bare item", |
| 201 | + in: `fookey`, |
| 202 | + wantBareItem: `fookey`, |
| 203 | + wantOk: true, |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "valid bare item and param", |
| 207 | + in: `fookey; a="123"`, |
| 208 | + wantBareItem: `fookey`, |
| 209 | + wantParam: `; a="123"`, |
| 210 | + wantOk: true, |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "valid item with content after", |
| 214 | + in: `fookey; a="123", otheritem; otherparam=1`, |
| 215 | + wantBareItem: `fookey`, |
| 216 | + wantParam: `; a="123"`, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "invalid just param", |
| 220 | + in: `;a="123"`, |
| 221 | + }, |
| 222 | + } |
| 223 | + |
| 224 | + for _, tc := range tests { |
| 225 | + var gotBareItem, gotParam string |
| 226 | + f := func(bareItem, param string) { |
| 227 | + gotBareItem = bareItem |
| 228 | + gotParam = param |
| 229 | + } |
| 230 | + ok := ParseItem(tc.in, f) |
| 231 | + if ok != tc.wantOk { |
| 232 | + t.Fatalf("test %q: want ok to be %v, got: %v", tc.name, tc.wantOk, ok) |
| 233 | + } |
| 234 | + if tc.wantBareItem != gotBareItem { |
| 235 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotBareItem, tc.wantBareItem) |
| 236 | + } |
| 237 | + if tc.wantParam != gotParam { |
| 238 | + t.Fatalf("test %q: mismatch.\n got: %#v\nwant: %#v\n", tc.name, gotParam, tc.wantParam) |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
12 | 243 | func TestConsumeParameter(t *testing.T) {
|
13 | 244 | tests := []struct {
|
14 | 245 | name string
|
|
0 commit comments