diff --git a/README.md b/README.md index 8304bf6..635bdf1 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,24 @@ smoke_header "X-Forwarded-Proto: https" Existing custom headers can be unset with `remove_smoke_headers`. +### Basic Auth + +Basic Auth credentials can be passed using: `smoke_basic_auth ` +To unset credentials pass no arguments or empty ones. + +Full usage example: + +```bash +smoke_url "${API}/health" + smoke_assert_code 401 + +smoke_basic_auth user1 pass1 # set credentials +smoke_url_ok "${API}/health" # call with credentials + smoke_assert_code 200 + +smoke_basic_auth # unset +``` + ### CSRF tokens Web applications that are protected with CSRF tokens will need to extract a @@ -248,3 +266,4 @@ API |`smoke_host ` | set the host header to use | |`smoke_header
` | set additional request header | |`smoke_tcp_ok ` | open a tcp connection and check for a `Connected` response | +|`smoke_basic_auth ` | set Basic Auth credentials | diff --git a/smoke.sh b/smoke.sh index 4932637..b38a7f9 100644 --- a/smoke.sh +++ b/smoke.sh @@ -18,6 +18,11 @@ SMOKE_HEADERS=() ## "Public API" +smoke_basic_auth() { + SMOKE_BASIC_AUTH_USER="$1" + SMOKE_BASIC_AUTH_PASSWORD="$2" +} + smoke_csrf() { SMOKE_CSRF_TOKEN="$1" } @@ -189,6 +194,10 @@ _curl() { opt+=(-H "$header") done fi + if [[ -n "$SMOKE_BASIC_AUTH_USER" ]] + then + opt+=(-u "$SMOKE_BASIC_AUTH_USER:$SMOKE_BASIC_AUTH_PASSWORD") + fi curl "${opt[@]}" "$@" > $SMOKE_CURL_BODY } @@ -257,6 +266,13 @@ _smoke_print_success() { } _smoke_print_url() { - TEXT="$1" + TEXT="$@" echo "> $TEXT" + + if [[ -n "$SMOKE_BASIC_AUTH_USER" ]] + then + echo "> $${bold}${TEXT}${normal} with BasicAuth ${bold}${SMOKE_BASIC_AUTH_USER}:****${normal}" + else + echo "> ${bold}${TEXT}${normal}" + fi }