1

I am working on a script which is a test suite that would check all invalid headers case using curl.

I created a bunch of functions which allows me to construct headers tupe (field_name: field_value) with default testing value.

build_foo_header() {
  local FOO_KN=${1:-'X-App-Name'}
  local FOO_KV=${2:-'MyApp'}
  echo $FOO_KN: $FOO_KV
}

I would like to create some arrays in functions with either missing headers or headers with invalid values to test my app behavior.

So i created a sample function as it :

build_headers_invalid_X_App_Name_Value() {
  HEADERS=()

  # this array should contain all other required headers
  HEADERS[0]=$(build_foo_header X-App-Name BadValue)
  # HEADERS[1]= header that is required with default value
  # and son on for all the remaning required headers

  echo "${HEADERS[@]/#/-H}" 
}

However i can't get curl how to pass that array to curl ?

$(curl $(build_headers_invalid_X_App_Name_Value) myURl)

Seems to send a request to the first header in the HEADERS array.

I have approximatively 10 required headers that's why I would like to script it that way.

1 Answer 1

1

The problem is by echoing "${HEADERS[@]/#/-H}", you lose any distinction between whitespace that occurred in an element of HEADERS and whitespace used to separate two elements in the output.

If you want to use a function, you need to just set a global array, then use that directly. No command substitution is needed or useful.

build_foo_header() {
  local FOO_KN=${1:-'X-App-Name'}
  local FOO_KV=${2:-'MyApp'}
  echo "$FOO_KN: $FOO_KV"
}

build_headers_invalid_X_App_Name_Value() {
  HEADERS=()

  # this array should contain all other required headers
  HEADERS[0]=$(build_foo_header X-App-Name BadValue)
  # HEADERS[1]= header that is required with default value
  # and son on for all the remaning required headers    
}

build_headers_invalid_X_App_Name_Value
curl "${HEADERS[@]/#/-H}" myUrl
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for the detailed answer. I will have lot of build_headers_XXX functions to handle each use case of missing or misspelled headers so I would like the array to be local to the function and then figure a way to pass it to the curl command (there will a bunch of curl call in my script). Sorry it's a bit more than the OP but if you won't mind give me hints :)
If you could pass the result as a string (via the output of your function), arrays wouldn't need to have been invented. bash simply doesn't have anything more complicated than a string that you can pass around as a first-class value.
You could run the function and curl in a subshell, as long as there isn't anything else you need to make global: (build_headers; curl "${HEADERS[@]/#/-H}" myUrl). This way, HEADERS is global to the subshell in which the commands run, but disappears after that subshell exits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.