From c49dd44972153099f70ebf0d5a68348345ebf55d Mon Sep 17 00:00:00 2001
From: Will McCutchen <will@mccutch.org>
Date: Tue, 30 May 2017 17:29:12 -0700
Subject: [PATCH] Use generic notImplementedHandler for /digest-auth

---
 httpbin/handlers.go      | 16 ++++------------
 httpbin/handlers_test.go |  8 ++++----
 httpbin/httpbin.go       |  4 +++-
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/httpbin/handlers.go b/httpbin/handlers.go
index a212a08..fb27c4b 100644
--- a/httpbin/handlers.go
+++ b/httpbin/handlers.go
@@ -21,6 +21,10 @@ var acceptedMediaTypes = []string{
 	"image/",
 }
 
+func notImplementedHandler(w http.ResponseWriter, r *http.Request) {
+	http.Error(w, "Not implemented", http.StatusNotImplemented)
+}
+
 // Index renders an HTML index page
 func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) {
 	if r.URL.Path != "/" {
@@ -383,18 +387,6 @@ func (h *HTTPBin) HiddenBasicAuth(w http.ResponseWriter, r *http.Request) {
 	writeJSON(w, body, http.StatusOK)
 }
 
-// DigestAuth is not yet implemented, and returns 501 Not Implemented. It
-// appears that stdlib support for working with digest authentication is
-// lacking, and I'm not yet ready to implement it myself.
-func (h *HTTPBin) DigestAuth(w http.ResponseWriter, r *http.Request) {
-	parts := strings.Split(r.URL.Path, "/")
-	if len(parts) != 5 {
-		http.Error(w, "Not Found", http.StatusNotFound)
-		return
-	}
-	http.Error(w, "Not Implemented", http.StatusNotImplemented)
-}
-
 // Stream responds with max(n, 100) lines of JSON-encoded request data.
 func (h *HTTPBin) Stream(w http.ResponseWriter, r *http.Request) {
 	parts := strings.Split(r.URL.Path, "/")
diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go
index 81ed476..d08e382 100644
--- a/httpbin/handlers_test.go
+++ b/httpbin/handlers_test.go
@@ -1075,11 +1075,11 @@ func TestDigestAuth(t *testing.T) {
 		url    string
 		status int
 	}{
-		{"/digest-auth/qop/user/pass", http.StatusNotImplemented},
 		{"/digest-auth", http.StatusNotFound},
-		{"/digest-auth/user", http.StatusNotFound},
-		{"/digest-auth/user/pass", http.StatusNotFound},
-		{"/digest-auth/qop/user/pass/foo", http.StatusNotFound},
+		{"/digest-auth/qop/user/pass", http.StatusNotImplemented},
+		{"/digest-auth/user", http.StatusNotImplemented},
+		{"/digest-auth/user/pass", http.StatusNotImplemented},
+		{"/digest-auth/qop/user/pass/foo", http.StatusNotImplemented},
 	}
 	for _, test := range tests {
 		t.Run("ok"+test.url, func(t *testing.T) {
diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go
index 827c82b..8e835cb 100644
--- a/httpbin/httpbin.go
+++ b/httpbin/httpbin.go
@@ -112,7 +112,6 @@ func (h *HTTPBin) Handler() http.Handler {
 
 	mux.HandleFunc("/basic-auth/", h.BasicAuth)
 	mux.HandleFunc("/hidden-basic-auth/", h.HiddenBasicAuth)
-	mux.HandleFunc("/digest-auth/", h.DigestAuth)
 
 	mux.HandleFunc("/deflate", h.Deflate)
 	mux.HandleFunc("/gzip", h.Gzip)
@@ -139,6 +138,9 @@ func (h *HTTPBin) Handler() http.Handler {
 	mux.HandleFunc("/image/", h.Image)
 	mux.HandleFunc("/xml", h.XML)
 
+	// Not implemented
+	mux.HandleFunc("/digest-auth/", notImplementedHandler)
+
 	// Make sure our ServeMux doesn't "helpfully" redirect these invalid
 	// endpoints by adding a trailing slash. See the ServeMux docs for more
 	// info: https://golang.org/pkg/net/http/#ServeMux
-- 
GitLab