Check for a force-fail
header and set it as the
...response code Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
fd6bac6da7
commit
df64a01ebe
1 changed files with 15 additions and 2 deletions
|
@ -4,14 +4,27 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("Running handler...")
|
fmt.Println("Running handler...")
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
// If the `force-fail` header is set, get its value and use it as the status
|
||||||
w.WriteHeader(http.StatusOK)
|
// 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{})
|
json.NewEncoder(w).Encode(map[string]any{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue