forked from speakeasy-api/speakeasy-auth-test-service
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds a new HTTP middleware to that intercepts requests with an `x-teapot` header and responds with a 418 status code and a message in json or text format depending on the value of the header. This middleware provides a simple mechanism to trigger error responses.
- Loading branch information
1 parent
ad5cf9c
commit ea86c3a
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package middleware | ||
|
||
import "net/http" | ||
|
||
func Teapot(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
format := r.Header.Get("x-teapot") | ||
if format == "" { | ||
h.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
if format == "json" { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusTeapot) | ||
w.Write([]byte(`{"message": "I'm a teapot"}`)) | ||
return | ||
} else { | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.WriteHeader(http.StatusTeapot) | ||
w.Write([]byte("I'm a teapot\n")) | ||
return | ||
} | ||
}) | ||
} |