#!/usr/bin/env bash # A fake curl for PipelineTests: the registry part of .gitea/scripts/pulizia-docker.sh talks to the packages API of # Gitea 28 through it. No network. It knows only the options the cleanup uses and answers like Gitea: # GET ?page=&limit=50 the page $FAKE_CURL_WORLD/page-.json, or [] after the last one (200) # DELETE / 204, and the version is written to $FAKE_CURL_WORLD/deleted # where is the content of $FAKE_CURL_WORLD/base (…/api/v1/packages//container/febe). Without the header # «Authorization: token » it answers 401; with $FAKE_CURL_WORLD/refuse it answers # 403 to every call; any other address is 404. Every call goes to $FAKE_CURL_WORLD/calls as «METHOD URL», never with # its headers. Any other option is an error (exit 3). set -uo pipefail world="${FAKE_CURL_WORLD:?}" token="$(cat "$world/token" 2> /dev/null)" method=GET output="" write="" url="" authorized=0 while (($#)); do case "$1" in -s | -S | -sS) ;; --max-time) shift ;; -X) method="$2"; shift ;; -H) [[ -n "$token" && "$2" == "Authorization: token $token" ]] && authorized=1; shift ;; -o) output="$2"; shift ;; -w) write="$2"; shift ;; -*) echo "fake curl: unexpected option $1" >&2; exit 3 ;; *) [[ -z "$url" ]] || { echo "fake curl: a second address $1" >&2; exit 3; } url="$1" ;; esac shift done printf '%s %s\n' "$method" "$url" >> "$world/calls" [[ -z "$write" || "$write" == '%{http_code}' ]] || { echo "fake curl: unexpected -w $write" >&2; exit 3; } answer() { local code="$1" body="${2:-}" if [[ -n "$output" ]]; then printf '%s' "$body" > "$output" else printf '%s' "$body" fi [[ -z "$write" ]] || printf '%s' "$code" exit 0 } [[ -f "$world/refuse" ]] && answer 403 '{"message":"token does not have at least one of required scope(s): [write:package]"}' ((authorized)) || answer 401 '{"message":"token is required"}' base="$(cat "$world/base")" case "$method $url" in "GET $base?page="*"&limit=50") page="${url#"$base?page="}" page="${page%%&*}" if [[ -f "$world/page-$page.json" ]]; then answer 200 "$(cat "$world/page-$page.json")" fi answer 200 "[]" ;; "DELETE $base/"*) echo "${url#"$base/"}" >> "$world/deleted" answer 204 ;; *) answer 404 '{"message":"The target couldn'"'"'t be found."}' ;; esac