From e444aae63ba0a47490f5226647ed532d66fbebee Mon Sep 17 00:00:00 2001 From: Will McCutchen <will@mccutch.org> Date: Sat, 27 May 2017 16:59:05 -0700 Subject: [PATCH] Add /robots.txt --- httpbin/handlers.go | 8 ++++++++ httpbin/handlers_test.go | 9 +++++++++ httpbin/httpbin.go | 1 + 3 files changed, 18 insertions(+) diff --git a/httpbin/handlers.go b/httpbin/handlers.go index 5963531..f26ad01 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -554,3 +554,11 @@ func (h *HTTPBin) Range(w http.ResponseWriter, r *http.Request) { func (h *HTTPBin) HTML(w http.ResponseWriter, r *http.Request) { writeHTML(w, MustAsset("moby.html"), http.StatusOK) } + +// Robots renders a basic robots.txt file +func (h *HTTPBin) Robots(w http.ResponseWriter, r *http.Request) { + robotsTxt := []byte(`User-agent: * +Disallow: /deny +`) + writeResponse(w, http.StatusOK, "text/plain", robotsTxt) +} diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index afdd681..dbfab0b 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -1543,3 +1543,12 @@ func TestHTML(t *testing.T) { assertContentType(t, w, htmlContentType) assertBodyContains(t, w, `<h1>Herman Melville - Moby-Dick</h1>`) } + +func TestRobots(t *testing.T) { + r, _ := http.NewRequest("GET", "/robots.txt", nil) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + + assertContentType(t, w, "text/plain") + assertBodyContains(t, w, `Disallow: /deny`) +} diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index 5c1d86c..5824755 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -123,6 +123,7 @@ func (h *HTTPBin) Handler() http.Handler { mux.HandleFunc("/range/", h.Range) mux.HandleFunc("/html", h.HTML) + mux.HandleFunc("/robots.txt", h.Robots) // Make sure our ServeMux doesn't "helpfully" redirect these invalid // endpoints by adding a trailing slash. See the ServeMux docs for more -- GitLab