From 258749e43dc8c2e842f96f8672823b0fa4e5a147 Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Mon, 8 Mar 2021 15:32:26 -0700 Subject: apiv2 tests: finally fix POST as originally intended When I originally wrote this code I had no idea what POST would look like so I did a sloppy job, deferring making it usable. Now that we have some real-world examples in place, I have a better understanding of what params look like and how to make tests more readable/maintainable. (Deferring isn't always bad: one of my early ideas was to separate params using commas; that would've been a disaster because some JSON values, such as arrays, include commas). This commit implements a better way of dealing with POST: * The main concept is still 'key=value' * When value is a JSON object (dictionary, array), it can be quoted. * Multiple params are simply separated by spaces. The 3-digit HTTP code is a prominent, readable separator between POST params and expected results. The parsing code is a little uglier, but test developers need never see that. The important thing is that writing tests is now easier. * POST params can be empty (this removes the need for a useless '') I snuck in one unrelated change: one of the newly-added tests, .NetworkSettings, was failing when run rootless (which is how I test on my setup). I made it conditional. Signed-off-by: Ed Santiago --- test/apiv2/test-apiv2 | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'test/apiv2/test-apiv2') diff --git a/test/apiv2/test-apiv2 b/test/apiv2/test-apiv2 index e32d6bc62..9f6bf257f 100755 --- a/test/apiv2/test-apiv2 +++ b/test/apiv2/test-apiv2 @@ -156,19 +156,23 @@ function _bump() { # jsonify # convert 'foo=bar,x=y' to json {"foo":"bar","x":"y"} ############# function jsonify() { - # split by comma - local -a settings_in - read -ra settings_in <<<"$1" - # convert each to double-quoted form local -a settings_out - for i in ${settings_in[*]}; do - settings_out+=$(sed -e 's/\(.*\)=\(.*\)/"\1":"\2"/' <<<$i) + for i in "$@"; do + # Each argument is of the form foo=bar. Separate into left and right. + local lhs + local rhs + IFS='=' read lhs rhs <<<"$i" + + # If right-hand side already includes double quotes, do nothing + if [[ ! $rhs =~ \" ]]; then + rhs="\"${rhs}\"" + fi + settings_out+=("\"${lhs}\":${rhs}") done - # ...and wrap inside braces. - # FIXME: handle commas - echo "{${settings_out[*]}}" + # ...and wrap inside braces, with comma separator if multiple fields + (IFS=','; echo "{${settings_out[*]}}") } ####### @@ -180,11 +184,19 @@ function t() { local curl_args local testname="$method $path" - # POST requests require an extra params arg + # POST requests may be followed by one or more key=value pairs. + # Slurp the command line until we see a 3-digit status code. if [[ $method = "POST" ]]; then - curl_args="-d $(jsonify $1)" + local -a post_args + for arg; do + case "$arg" in + *=*) post_args+=("$arg"); shift ;; + [1-9][0-9][0-9]) break;; + *) die "Internal error: invalid POST arg '$arg'" ;; + esac + done + curl_args="-d $(jsonify ${post_args[@]})" testname="$testname [$curl_args]" - shift fi # entrypoint path can include a descriptive comment; strip it off -- cgit v1.2.3-54-g00ecf