urllib: fixed regression in the set function (#13455)

pull/13460/head
Vincenzo Palazzo 2022-02-13 14:06:00 +01:00 committed by GitHub
parent 9ff0c51e61
commit 6ea4f361a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 2 deletions

View File

@ -57,13 +57,21 @@ pub fn (v &Values) get_all(key string) []string {
}
// set sets the key to value. It replaces any existing
// values.
// values, or create a new bucket with the new key if it is missed.
pub fn (mut v Values) set(key string, value string) {
// A query string can contains several
// duplicate, so we need to make sure that we
// cover all the edge case.
mut found := false
for mut qvalue in v.data {
qvalue.value = value
if qvalue.key == key {
found = true
qvalue.value = value
}
}
if !found {
v.add(key, value)
}
}

View File

@ -0,0 +1,38 @@
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module urllib
fn test_add_key_val() {
mut values := Values{}
values.add('key', 'value')
val := values.get('key')
assert val == 'value'
}
fn test_get_all_with_key() {
mut values := Values{}
values.add('key', 'value1')
values.add('key', 'value2')
key_values := values.get_all('key')
assert key_values == ['value1', 'value2']
}
fn test_set_with_key() {
mut values := Values{}
values.add('key', 'value1')
values.add('key', 'value2')
values.add('key1', 'value2')
values.set('key1', 'vlang')
key_values := values.get_all('key')
key2_values := values.get_all('key1')
assert key_values == ['value1', 'value2']
assert key2_values == ['vlang']
}
fn test_set_missing_key() {
mut values := Values{}
values.set('err', 'err1')
vals := values.get_all('err')
assert vals == ['err1']
}