From c14e0e555cafc9952fbcbf9b63624d1bc08055f7 Mon Sep 17 00:00:00 2001
From: Will McCutchen <will@mccutch.org>
Date: Sun, 18 Dec 2016 20:02:44 -0800
Subject: [PATCH] Delay can be given as a golang-style duration

---
 httpbin/handlers.go      | 14 +++++++++-----
 httpbin/handlers_test.go |  6 ++++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/httpbin/handlers.go b/httpbin/handlers.go
index 9b5fa37..0a2c55a 100644
--- a/httpbin/handlers.go
+++ b/httpbin/handlers.go
@@ -426,20 +426,24 @@ func (h *HTTPBin) Stream(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-// Delay waits for n seconds before responding
+// Delay waits for a given amount of time before responding, where the time may
+// be specified as a golang-style duration or seconds in floating point.
 func (h *HTTPBin) Delay(w http.ResponseWriter, r *http.Request) {
 	parts := strings.Split(r.URL.Path, "/")
 	if len(parts) != 3 {
 		http.Error(w, "Not found", http.StatusNotFound)
 		return
 	}
-	n, err := strconv.ParseFloat(parts[2], 64)
+
+	delay, err := time.ParseDuration(parts[2])
 	if err != nil {
-		http.Error(w, "Invalid float", http.StatusBadRequest)
+		n, err := strconv.ParseFloat(parts[2], 64)
+		if err != nil {
+			http.Error(w, "Invalid duration", http.StatusBadRequest)
+		}
+		delay = time.Duration(n*1000) * time.Millisecond
 	}
 
-	// n is seconds as a float, and we allow down to millisecond resolution
-	delay := time.Duration(n*1000) * time.Millisecond
 	if delay > h.options.MaxResponseTime {
 		delay = h.options.MaxResponseTime
 	} else if delay < 0 {
diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go
index 88eeb33..66906c3 100644
--- a/httpbin/handlers_test.go
+++ b/httpbin/handlers_test.go
@@ -1233,6 +1233,12 @@ func TestDelay(t *testing.T) {
 		url           string
 		expectedDelay time.Duration
 	}{
+		// go-style durations are supported
+		{"/delay/0ms", 0},
+		{"/delay/500ms", 500 * time.Millisecond},
+		{"/delay/1.5s", maxResponseTime},
+
+		// as are floating point seconds
 		{"/delay/0", 0},
 		{"/delay/0.5", 500 * time.Millisecond},
 		{"/delay/1", maxResponseTime},
-- 
GitLab