Skip to content

Commit

Permalink
updating curl to support header and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
narfdre committed Jun 26, 2024
1 parent cc02c1d commit 579496f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
21 changes: 10 additions & 11 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,17 @@ nvm_get_latest() {
}

nvm_download() {
local CURL_COMPRESSED_FLAG
local AUTH_HEADER_FLAG
if nvm_has "curl"; then
local CURL_OPTIONS=()

if [ -n "$NVM_AUTH_HEADER" ]; then
AUTH_HEADER_FLAG="--header $NVM_AUTH_HEADER"
fi
if [ -n "$NVM_AUTH_HEADER" ]; then
CURL_OPTIONS+=(--header "$NVM_AUTH_HEADER")
fi

if nvm_has "curl"; then
if nvm_curl_use_compression; then
CURL_COMPRESSED_FLAG="--compressed"
CURL_OPTIONS+=(--compressed)
fi
curl --fail ${CURL_COMPRESSED_FLAG:-} ${AUTH_HEADER_FLAG:-} -q "$@"
curl -q --fail "${CURL_OPTIONS[@]}" "$@"
elif nvm_has "wget"; then
# Emulate curl with wget
ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \
Expand All @@ -139,9 +138,9 @@ nvm_download() {
-e 's/-sS /-nv /' \
-e 's/-o /-O /' \
-e 's/-C - /-c /')
if [ -n "$AUTH_HEADER_FLAG" ]; then
ARGS="$AUTH_HEADER_FLAG $ARGS"

if [ -n "$NVM_AUTH_HEADER" ]; then
ARGS="$ARGS --header \"$NVM_AUTH_HEADER\""
fi
# shellcheck disable=SC2086
eval wget $ARGS
Expand Down
5 changes: 5 additions & 0 deletions test/fast/Unit tests/nvm_download
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/de
# nvm_download should fail to download wrong_install.sh
! nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/wrong_install.sh" >/dev/null || die "nvm_download should fail to download no existing file"

# nvm_download should pass when calling with auth header
NVM_AUTH_HEADER="Authorization: Bearer test-token"
nvm_download "https://httpbin.org/bearer" >/dev/null || die "nvm_download unable to send auth header"
unset NVM_AUTH_HEADER

cleanup

0 comments on commit 579496f

Please sign in to comment.