From 6ea4f361a1c4347da4c3dacd1328ce3236b62b89 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sun, 13 Feb 2022 14:06:00 +0100 Subject: [PATCH] urllib: fixed regression in the set function (#13455) --- vlib/net/urllib/values.v | 12 +++++++++-- vlib/net/urllib/values_test.v | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 vlib/net/urllib/values_test.v 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'] +}