diff --git a/vlib/net/urllib/values.v b/vlib/net/urllib/values.v index 19336bd947..c420e2cbbb 100644 --- a/vlib/net/urllib/values.v +++ b/vlib/net/urllib/values.v @@ -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) } } diff --git a/vlib/net/urllib/values_test.v b/vlib/net/urllib/values_test.v new file mode 100644 index 0000000000..335dc987cd --- /dev/null +++ b/vlib/net/urllib/values_test.v @@ -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'] +}