From 67ddb1f5131d0cb63a40f5ccefecabb582f5ab3b Mon Sep 17 00:00:00 2001
From: Alan Wang <gchinaran@gmail.com>
Date: Tue, 17 Jan 2023 21:30:26 +0800
Subject: [PATCH] feat: ensure that 404 responses indicate that they originate
 from go-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.
---
 httpbin/handlers.go      | 3 ++-
 httpbin/handlers_test.go | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/httpbin/handlers.go b/httpbin/handlers.go
index 6409930..1fe6b7e 100644
--- a/httpbin/handlers.go
+++ b/httpbin/handlers.go
@@ -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")
diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go
index 2a346cd..7c3064a 100644
--- a/httpbin/handlers_test.go
+++ b/httpbin/handlers_test.go
@@ -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) {
-- 
GitLab