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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
42347087
Commit
42347087
authored
7 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Simplify configuration
parent
d5ff1777
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
httpbin/handlers.go
+5
-5
5 additions, 5 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+7
-16
7 additions, 16 deletions
httpbin/handlers_test.go
httpbin/httpbin.go
+24
-7
24 additions, 7 deletions
httpbin/httpbin.go
httpbin/httpbin_test.go
+30
-0
30 additions, 0 deletions
httpbin/httpbin_test.go
with
66 additions
and
28 deletions
httpbin/handlers.go
+
5
−
5
View file @
42347087
...
...
@@ -457,7 +457,7 @@ func (h *HTTPBin) Delay(w http.ResponseWriter, r *http.Request) {
return
}
delay
,
err
:=
parseBoundedDuration
(
parts
[
2
],
0
,
h
.
options
.
Max
ResponseTime
)
delay
,
err
:=
parseBoundedDuration
(
parts
[
2
],
0
,
h
.
options
.
Max
Duration
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid duration"
,
http
.
StatusBadRequest
)
return
...
...
@@ -481,7 +481,7 @@ func (h *HTTPBin) Drip(w http.ResponseWriter, r *http.Request) {
userDuration
:=
q
.
Get
(
"duration"
)
if
userDuration
!=
""
{
duration
,
err
=
parseBoundedDuration
(
userDuration
,
0
,
h
.
options
.
Max
ResponseTime
)
duration
,
err
=
parseBoundedDuration
(
userDuration
,
0
,
h
.
options
.
Max
Duration
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid duration"
,
http
.
StatusBadRequest
)
return
...
...
@@ -490,7 +490,7 @@ func (h *HTTPBin) Drip(w http.ResponseWriter, r *http.Request) {
userDelay
:=
q
.
Get
(
"delay"
)
if
userDelay
!=
""
{
delay
,
err
=
parseBoundedDuration
(
userDelay
,
0
,
h
.
options
.
Max
ResponseTime
)
delay
,
err
=
parseBoundedDuration
(
userDelay
,
0
,
h
.
options
.
Max
Duration
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid delay"
,
http
.
StatusBadRequest
)
return
...
...
@@ -500,7 +500,7 @@ func (h *HTTPBin) Drip(w http.ResponseWriter, r *http.Request) {
userNumBytes
:=
q
.
Get
(
"numbytes"
)
if
userNumBytes
!=
""
{
numbytes
,
err
=
strconv
.
ParseInt
(
userNumBytes
,
10
,
64
)
if
err
!=
nil
||
numbytes
<=
0
||
numbytes
>
h
.
options
.
Max
ResponseSize
{
if
err
!=
nil
||
numbytes
<=
0
||
numbytes
>
h
.
options
.
Max
Memory
{
http
.
Error
(
w
,
"Invalid numbytes"
,
http
.
StatusBadRequest
)
return
}
...
...
@@ -515,7 +515,7 @@ func (h *HTTPBin) Drip(w http.ResponseWriter, r *http.Request) {
}
}
if
duration
+
delay
>
h
.
options
.
Max
ResponseTime
{
if
duration
+
delay
>
h
.
options
.
Max
Duration
{
http
.
Error
(
w
,
"Too much time"
,
http
.
StatusBadRequest
)
return
}
...
...
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
7
−
16
View file @
42347087
...
...
@@ -20,13 +20,11 @@ import (
)
const
maxMemory
int64
=
1024
*
1024
const
maxResponseSize
=
1024
const
maxResponseTime
time
.
Duration
=
1
*
time
.
Second
const
maxDuration
time
.
Duration
=
1
*
time
.
Second
var
app
=
NewHTTPBin
(
&
Options
{
MaxMemory
:
maxMemory
,
MaxResponseSize
:
maxResponseSize
,
MaxResponseTime
:
maxResponseTime
,
var
app
=
NewHTTPBinWithOptions
(
&
Options
{
MaxMemory
:
maxMemory
,
MaxDuration
:
maxDuration
,
})
var
handler
=
app
.
Handler
()
...
...
@@ -60,13 +58,6 @@ func assertBodyEquals(t *testing.T, w *httptest.ResponseRecorder, want string) {
}
}
func
TestNewHTTPBin__NilOptions
(
t
*
testing
.
T
)
{
h
:=
NewHTTPBin
(
nil
)
if
h
.
options
.
MaxMemory
!=
0
{
t
.
Fatalf
(
"expected default MaxMemory == 0, got %#v"
,
h
.
options
.
MaxMemory
)
}
}
func
TestIndex
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
...
...
@@ -1341,7 +1332,7 @@ func TestDelay(t *testing.T) {
// as are floating point seconds
{
"/delay/0"
,
0
},
{
"/delay/0.5"
,
500
*
time
.
Millisecond
},
{
"/delay/1"
,
max
ResponseTime
},
{
"/delay/1"
,
max
Duration
},
}
for
_
,
test
:=
range
okTests
{
t
.
Run
(
"ok"
+
test
.
url
,
func
(
t
*
testing
.
T
)
{
...
...
@@ -1418,7 +1409,7 @@ func TestDrip(t *testing.T) {
{
&
url
.
Values
{
"numbytes"
:
{
"1"
}},
0
,
1
,
http
.
StatusOK
},
{
&
url
.
Values
{
"numbytes"
:
{
"101"
}},
0
,
101
,
http
.
StatusOK
},
{
&
url
.
Values
{
"numbytes"
:
{
fmt
.
Sprintf
(
"%d"
,
max
ResponseSize
)}},
0
,
maxResponseSize
,
http
.
StatusOK
},
{
&
url
.
Values
{
"numbytes"
:
{
fmt
.
Sprintf
(
"%d"
,
max
Memory
)}},
0
,
int
(
maxMemory
)
,
http
.
StatusOK
},
{
&
url
.
Values
{
"code"
:
{
"100"
}},
0
,
10
,
100
},
{
&
url
.
Values
{
"code"
:
{
"404"
}},
0
,
10
,
404
},
...
...
@@ -1472,7 +1463,7 @@ func TestDrip(t *testing.T) {
{
&
url
.
Values
{
"numbytes"
:
{
"0"
}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"numbytes"
:
{
"-1"
}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"numbytes"
:
{
"0xff"
}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"numbytes"
:
{
fmt
.
Sprintf
(
"%d"
,
max
ResponseSize
+
1
)}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"numbytes"
:
{
fmt
.
Sprintf
(
"%d"
,
max
Memory
+
1
)}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"code"
:
{
"foo"
}},
http
.
StatusBadRequest
},
{
&
url
.
Values
{
"code"
:
{
"-1"
}},
http
.
StatusBadRequest
},
...
...
This diff is collapsed.
Click to expand it.
httpbin/httpbin.go
+
24
−
7
View file @
42347087
...
...
@@ -6,6 +6,12 @@ import (
"time"
)
// Default configuration values
const
(
DefaultMaxMemory
int64
=
1024
*
1024
DefaultMaxDuration
=
10
*
time
.
Second
)
const
jsonContentType
=
"application/json; encoding=utf-8"
const
htmlContentType
=
"text/html; charset=utf-8"
...
...
@@ -72,9 +78,13 @@ type streamResponse struct {
// Options are used to configure HTTPBin
type
Options
struct
{
MaxMemory
int64
MaxResponseSize
int64
MaxResponseTime
time
.
Duration
// How much memory a request is allowed to consume in bytes, as a limit on
// the size of incoming request bodies and on responses generated
MaxMemory
int64
// Maximum duration of a request, for those requests that allow user
// control over timing (e.g. /delay)
MaxDuration
time
.
Duration
}
// HTTPBin contains the business logic
...
...
@@ -166,11 +176,18 @@ func (h *HTTPBin) Handler() http.Handler {
return
handler
}
// NewHTTPBin creates a new HTTPBin
func
NewHTTPBin
(
options
*
Options
)
*
HTTPBin
{
if
options
==
nil
{
options
=
&
Options
{}
// NewHTTPBin creates a new HTTPBin instance with default options
func
NewHTTPBin
()
*
HTTPBin
{
return
&
HTTPBin
{
options
:
&
Options
{
MaxMemory
:
DefaultMaxMemory
,
MaxDuration
:
DefaultMaxDuration
,
},
}
}
// NewHTTPBinWithOptions creates a new HTTPBin instance with the given options
func
NewHTTPBinWithOptions
(
options
*
Options
)
*
HTTPBin
{
return
&
HTTPBin
{
options
:
options
,
}
...
...
This diff is collapsed.
Click to expand it.
httpbin/httpbin_test.go
0 → 100644
+
30
−
0
View file @
42347087
package
httpbin
import
(
"testing"
"time"
)
func
TestNewHTTPBin__Defaults
(
t
*
testing
.
T
)
{
h
:=
NewHTTPBin
()
if
h
.
options
.
MaxMemory
!=
DefaultMaxMemory
{
t
.
Fatalf
(
"expected default MaxMemory == %d, got %#v"
,
DefaultMaxMemory
,
h
.
options
.
MaxMemory
)
}
if
h
.
options
.
MaxDuration
!=
DefaultMaxDuration
{
t
.
Fatalf
(
"expected default MaxDuration == %s, got %#v"
,
DefaultMaxDuration
,
h
.
options
.
MaxDuration
)
}
}
func
TestNewHTTPBinWithOptions__Defaults
(
t
*
testing
.
T
)
{
o
:=
&
Options
{
MaxDuration
:
1
*
time
.
Second
,
MaxMemory
:
1024
,
}
h
:=
NewHTTPBinWithOptions
(
o
)
if
h
.
options
.
MaxMemory
!=
o
.
MaxMemory
{
t
.
Fatalf
(
"expected MaxMemory == %d, got %#v"
,
o
.
MaxMemory
,
h
.
options
.
MaxMemory
)
}
if
h
.
options
.
MaxDuration
!=
o
.
MaxDuration
{
t
.
Fatalf
(
"expected MaxDuration == %s, got %#v"
,
o
.
MaxDuration
,
h
.
options
.
MaxDuration
)
}
}
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