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
19ea19b7
Commit
19ea19b7
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Better handling of empty request bodies
parent
7c7422c1
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_test.go
+35
-15
35 additions, 15 deletions
httpbin/handlers_test.go
httpbin/helpers.go
+2
-1
2 additions, 1 deletion
httpbin/helpers.go
with
37 additions
and
16 deletions
httpbin/handlers_test.go
+
35
−
15
View file @
19ea19b7
...
...
@@ -323,7 +323,18 @@ func TestHeaders(t *testing.T) {
}
func
TestPost__EmptyBody
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
contentType
string
}{
{
""
},
{
"application/json; charset=utf-8"
},
{
"application/x-www-form-urlencoded"
},
{
"multipart/form-data; foo"
},
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
"content type/"
+
test
.
contentType
,
func
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"POST"
,
"/post"
,
nil
)
r
.
Header
.
Set
(
"Content-Type"
,
test
.
contentType
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
...
...
@@ -336,12 +347,21 @@ func TestPost__EmptyBody(t *testing.T) {
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
resp
.
Data
!=
""
{
t
.
Fatalf
(
"expected empty response data, got %#v"
,
resp
.
Data
)
}
if
resp
.
JSON
!=
nil
{
t
.
Fatalf
(
"expected nil response json, got %#v"
,
resp
.
JSON
)
}
if
len
(
resp
.
Args
)
>
0
{
t
.
Fatalf
(
"expected no query params, got %#v"
,
resp
.
Args
)
}
if
len
(
resp
.
Form
)
>
0
{
t
.
Fatalf
(
"expected no form data, got %#v"
,
resp
.
Form
)
}
})
}
}
func
TestPost__FormEncodedBody
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
httpbin/helpers.go
+
2
−
1
View file @
19ea19b7
...
...
@@ -121,7 +121,8 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse) error
}
resp
.
Form
=
r
.
PostForm
case
strings
.
HasPrefix
(
ct
,
"application/json"
)
:
if
err
:=
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
resp
.
JSON
);
err
!=
nil
{
err
:=
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
resp
.
JSON
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
return
err
}
}
...
...
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