Skip to content

Commit

Permalink
feat: ensure that 404 responses indicate that they originate from go-…
Browse files Browse the repository at this point in the history
…httpbin (#107)

This can help debug misconfigurations, where it's useful to know whether a 404 response is due to a configuration issue in some upstream proxy (load balancer, gateway, etc) or due to an actual bad request to go-httpbin.
  • Loading branch information
chinaran authored Jan 17, 2023
1 parent 1229cc6 commit 67ddb1f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion httpbin/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func notImplementedHandler(w http.ResponseWriter, r *http.Request) {
// Index renders an HTML index page
func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not Found", http.StatusNotFound)
msg := fmt.Sprintf("Not Found (go-httpbin does not handle the path %s)", r.URL.Path)
http.Error(w, msg, http.StatusNotFound)
return
}
w.Header().Set("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' camo.githubusercontent.com")
Expand Down
1 change: 1 addition & 0 deletions httpbin/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestIndex__NotFound(t *testing.T) {
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
assertStatusCode(t, w, http.StatusNotFound)
assertBodyContains(t, w, "/foo")
}

func TestFormsPost(t *testing.T) {
Expand Down

0 comments on commit 67ddb1f

Please sign in to comment.