Skip to content
Snippets Groups Projects
Commit c14e0e55 authored by Will McCutchen's avatar Will McCutchen
Browse files

Delay can be given as a golang-style duration

parent b1abd83f
No related branches found
No related tags found
No related merge requests found
...@@ -426,20 +426,24 @@ func (h *HTTPBin) Stream(w http.ResponseWriter, r *http.Request) { ...@@ -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) { func (h *HTTPBin) Delay(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/") parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 { if len(parts) != 3 {
http.Error(w, "Not found", http.StatusNotFound) http.Error(w, "Not found", http.StatusNotFound)
return return
} }
delay, err := time.ParseDuration(parts[2])
if err != nil {
n, err := strconv.ParseFloat(parts[2], 64) n, err := strconv.ParseFloat(parts[2], 64)
if err != nil { if err != nil {
http.Error(w, "Invalid float", http.StatusBadRequest) 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 { if delay > h.options.MaxResponseTime {
delay = h.options.MaxResponseTime delay = h.options.MaxResponseTime
} else if delay < 0 { } else if delay < 0 {
......
...@@ -1233,6 +1233,12 @@ func TestDelay(t *testing.T) { ...@@ -1233,6 +1233,12 @@ func TestDelay(t *testing.T) {
url string url string
expectedDelay time.Duration 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", 0},
{"/delay/0.5", 500 * time.Millisecond}, {"/delay/0.5", 500 * time.Millisecond},
{"/delay/1", maxResponseTime}, {"/delay/1", maxResponseTime},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment