From df64a01ebef2801dbf600176d26c2a69743d7499 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sat, 20 Sep 2025 23:22:30 +0100 Subject: [PATCH] Check for a `force-fail` header and set it as the ...response code Signed-off-by: Oliver Davies --- go-api/main.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/go-api/main.go b/go-api/main.go index 032aa8a..f309b33 100644 --- a/go-api/main.go +++ b/go-api/main.go @@ -4,14 +4,27 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Println("Running handler...") - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) + // If the `force-fail` header is set, get its value and use it as the status + // code. + if failCode := r.Header.Get("force-fail"); failCode != "" { + fmt.Println("`force-fail` header set...") + if code, err := strconv.Atoi(failCode); err == nil { + fmt.Printf("Setting the response code to %d...", code) + + w.WriteHeader(code) + } + } else { + w.WriteHeader(http.StatusOK) + } + + w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]any{}) }