|
4 | 4 | # demands its own credentials. This is a slow test because it installs node: |
5 | 5 | # the latest LTS is installed, and the node binary of that installation is |
6 | 6 | # then used to run the dependency-free mock proxy. The proxy-header checks |
7 | | -# themselves follow the nvm_download fast test, using a local httpbin |
8 | | -# container as the origin. |
| 7 | +# use a local mock server (test/mocks/mock-server.js) as the origin, which |
| 8 | +# provides the endpoints a local httpbin container used to provide. |
9 | 9 |
|
10 | 10 | cleanup () { |
11 | 11 | unset -f die cleanup |
12 | 12 | if [ -n "${mock_proxy_pid:-}" ]; then |
13 | 13 | kill "${mock_proxy_pid}" >/dev/null 2>&1 || true |
14 | 14 | fi |
15 | 15 | rm -f "${mock_proxy_port_file:-}" 2>/dev/null || true |
16 | | - docker rm -f httpbin >/dev/null 2>&1 || true |
| 16 | + if [ -n "${mock_server_pid:-}" ]; then |
| 17 | + kill "${mock_server_pid}" >/dev/null 2>&1 || true |
| 18 | + fi |
| 19 | + rm -f "${mock_server_port_file:-}" 2>/dev/null || true |
17 | 20 | if [ -n "${nvm_alias_bak:-}" ]; then |
18 | 21 | rm -rf "${nvm_alias_dir}" 2>/dev/null || true |
19 | 22 | mv "${nvm_alias_bak}" "${nvm_alias_dir}" 2>/dev/null || true |
|
63 | 66 | [ -n "${mock_proxy_port}" ] || die 'the mock proxy did not report a port' |
64 | 67 | proxy_url="http://127.0.0.1:${mock_proxy_port}" |
65 | 68 |
|
| 69 | +# start the mock server (which provides the origin endpoints) with the |
| 70 | +# installed node, on an ephemeral port |
| 71 | +mock_server_port_file="$(mktemp)" |
| 72 | +"${NODE_BIN}" "../mocks/mock-server.js" "${mock_server_port_file}" >/dev/null 2>&1 & |
| 73 | +mock_server_pid="$!" |
| 74 | +mock_server_port='' |
| 75 | +i=0 |
| 76 | +while [ "${i}" -lt 10 ]; do |
| 77 | + if [ -s "${mock_server_port_file}" ]; then |
| 78 | + mock_server_port="$(cat "${mock_server_port_file}")" |
| 79 | + break |
| 80 | + fi |
| 81 | + i=$((i + 1)) |
| 82 | + sleep 1 |
| 83 | +done |
| 84 | +[ -n "${mock_server_port}" ] || die 'the mock server did not report a port' |
| 85 | + |
66 | 86 | # make sure no_proxy does not bypass the proxy for these tests |
67 | 87 | unset no_proxy NO_PROXY |
68 | 88 |
|
69 | | -# the proxy header checks need a local httpbin container (as the nvm_download |
70 | | -# fast test); retry the pull, and skip (rather than fail) if the image cannot |
71 | | -# be pulled or run, so a transient Docker registry outage does not fail the |
72 | | -# suite |
73 | | -httpbin_pulled=0 |
74 | | -for i in 1 2 3 4 5; do |
75 | | - if docker pull kennethreitz/httpbin; then httpbin_pulled=1; break; fi |
76 | | - echo "docker pull httpbin failed, attempt $i/5" |
77 | | - sleep $((i * 5)) |
78 | | -done |
79 | | -if [ "${httpbin_pulled}" = 1 ] && SHELL=bash docker run -d --name httpbin -p 80:80 kennethreitz/httpbin; then |
80 | | - sleep 1 # wait for httpbin to start |
81 | | - # no Authorization and no Proxy-Authorization: expected fail |
82 | | - http_proxy="${proxy_url}" nvm_download "http://127.0.0.1/get" > /dev/null && die 'nvm_download with no Authorization and no Proxy-Authorization header should fail' |
83 | | - # Authorization but no Proxy-Authorization: expected fail |
84 | | - NVM_AUTH_HEADER="Bearer test-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1/bearer" > /dev/null && die 'nvm_download with Authorization but no Proxy-Authorization header should fail' |
85 | | - # no Authorization but Proxy-Authorization: expected fail |
86 | | - NVM_PROXY_AUTH_HEADER="Bearer proxy-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1/bearer" > /dev/null && die 'nvm_download with Proxy-Authorization but no Authorization header should fail' |
87 | | - # Authorization and Proxy-Authorization: expected success |
88 | | - NVM_AUTH_HEADER="Bearer test-token" NVM_PROXY_AUTH_HEADER="Bearer proxy-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1/bearer" > /dev/null || die 'nvm_download with Authorization and Proxy-Authorization headers should succeed' |
89 | | -else |
90 | | - echo 'skipping proxy header checks: unable to pull or run httpbin' |
91 | | -fi |
| 89 | +# no Authorization and no Proxy-Authorization: expected fail |
| 90 | +http_proxy="${proxy_url}" nvm_download "http://127.0.0.1:${mock_server_port}/get" > /dev/null && die 'nvm_download with no Authorization and no Proxy-Authorization header should fail' |
| 91 | +# Authorization but no Proxy-Authorization: expected fail |
| 92 | +NVM_AUTH_HEADER="Bearer test-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1:${mock_server_port}/bearer" > /dev/null && die 'nvm_download with Authorization but no Proxy-Authorization header should fail' |
| 93 | +# no Authorization but Proxy-Authorization: expected fail |
| 94 | +NVM_PROXY_AUTH_HEADER="Bearer proxy-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1:${mock_server_port}/bearer" > /dev/null && die 'nvm_download with Proxy-Authorization but no Authorization header should fail' |
| 95 | +# Authorization and Proxy-Authorization: expected success |
| 96 | +NVM_AUTH_HEADER="Bearer test-token" NVM_PROXY_AUTH_HEADER="Bearer proxy-token" http_proxy="${proxy_url}" nvm_download "http://127.0.0.1:${mock_server_port}/bearer" > /dev/null || die 'nvm_download with Authorization and Proxy-Authorization headers should succeed' |
92 | 97 |
|
93 | 98 | cleanup |
0 commit comments