Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
c14e0e55
Commit
c14e0e55
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Delay can be given as a golang-style duration
parent
b1abd83f
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
httpbin/handlers.go
+9
-5
9 additions, 5 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+6
-0
6 additions, 0 deletions
httpbin/handlers_test.go
with
15 additions
and
5 deletions
httpbin/handlers.go
+
9
−
5
View file @
c14e0e55
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
6
−
0
View file @
c14e0e55
...
@@ -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
},
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment