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
818cdefe
Commit
818cdefe
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Stop manually parsing query params like a caveman
parent
5b7dab93
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
+4
-21
4 additions, 21 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+2
-24
2 additions, 24 deletions
httpbin/handlers_test.go
with
6 additions
and
45 deletions
httpbin/handlers.go
+
4
−
21
View file @
818cdefe
...
@@ -4,7 +4,6 @@ import (
...
@@ -4,7 +4,6 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"net/http"
"net/http"
"net/url"
"strconv"
"strconv"
"strings"
"strings"
)
)
...
@@ -35,13 +34,8 @@ func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) {
...
@@ -35,13 +34,8 @@ func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) {
// Get handles HTTP GET requests
// Get handles HTTP GET requests
func
(
h
*
HTTPBin
)
Get
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
h
*
HTTPBin
)
Get
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
getResponse
{
resp
:=
&
getResponse
{
Args
:
args
,
Args
:
r
.
URL
.
Query
()
,
Headers
:
r
.
Header
,
Headers
:
r
.
Header
,
Origin
:
getOrigin
(
r
),
Origin
:
getOrigin
(
r
),
URL
:
getURL
(
r
)
.
String
(),
URL
:
getURL
(
r
)
.
String
(),
...
@@ -52,20 +46,14 @@ func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
...
@@ -52,20 +46,14 @@ func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
// RequestWithBody handles POST, PUT, and PATCH requests
// RequestWithBody handles POST, PUT, and PATCH requests
func
(
h
*
HTTPBin
)
RequestWithBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
h
*
HTTPBin
)
RequestWithBody
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
resp
:=
&
bodyResponse
{
resp
:=
&
bodyResponse
{
Args
:
args
,
Args
:
r
.
URL
.
Query
()
,
Headers
:
r
.
Header
,
Headers
:
r
.
Header
,
Origin
:
getOrigin
(
r
),
Origin
:
getOrigin
(
r
),
URL
:
getURL
(
r
)
.
String
(),
URL
:
getURL
(
r
)
.
String
(),
}
}
err
=
parseBody
(
w
,
r
,
resp
,
h
.
options
.
MaxMemory
)
err
:
=
parseBody
(
w
,
r
,
resp
,
h
.
options
.
MaxMemory
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing request body: %s"
,
err
),
http
.
StatusBadRequest
)
}
}
...
@@ -179,12 +167,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
...
@@ -179,12 +167,7 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
// ResponseHeaders responds with a map of header values
// ResponseHeaders responds with a map of header values
func
(
h
*
HTTPBin
)
ResponseHeaders
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
h
*
HTTPBin
)
ResponseHeaders
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
args
,
err
:=
url
.
ParseQuery
(
r
.
URL
.
RawQuery
)
args
:=
r
.
URL
.
Query
()
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"error parsing query params: %s"
,
err
),
http
.
StatusBadRequest
)
return
}
for
k
,
vs
:=
range
args
{
for
k
,
vs
:=
range
args
{
for
_
,
v
:=
range
vs
{
for
_
,
v
:=
range
vs
{
w
.
Header
()
.
Add
(
http
.
CanonicalHeaderKey
(
k
),
v
)
w
.
Header
()
.
Add
(
http
.
CanonicalHeaderKey
(
k
),
v
)
...
...
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
2
−
24
View file @
818cdefe
...
@@ -141,14 +141,6 @@ func TestGet__WithParams(t *testing.T) {
...
@@ -141,14 +141,6 @@ func TestGet__WithParams(t *testing.T) {
}
}
}
}
func
TestGet__InvalidQuery
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/get?foo=%ZZ"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusBadRequest
)
}
func
TestGet__OnlyAllowsGets
(
t
*
testing
.
T
)
{
func
TestGet__OnlyAllowsGets
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"POST"
,
"/get"
,
nil
)
r
,
_
:=
http
.
NewRequest
(
"POST"
,
"/get"
,
nil
)
w
:=
httptest
.
NewRecorder
()
w
:=
httptest
.
NewRecorder
()
...
@@ -540,13 +532,6 @@ func TestPost__BodyTooBig(t *testing.T) {
...
@@ -540,13 +532,6 @@ func TestPost__BodyTooBig(t *testing.T) {
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
}
}
func
TestPost__InvalidQueryParams
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"POST"
,
"/post?foo=%ZZ"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusBadRequest
)
}
func
TestPost__QueryParams
(
t
*
testing
.
T
)
{
func
TestPost__QueryParams
(
t
*
testing
.
T
)
{
params
:=
url
.
Values
{}
params
:=
url
.
Values
{}
params
.
Set
(
"foo"
,
"foo"
)
params
.
Set
(
"foo"
,
"foo"
)
...
@@ -743,14 +728,7 @@ func TestResponseHeaders__OverrideContentType(t *testing.T) {
...
@@ -743,14 +728,7 @@ func TestResponseHeaders__OverrideContentType(t *testing.T) {
assertContentType
(
t
,
w
,
contentType
)
assertContentType
(
t
,
w
,
contentType
)
}
}
func
TestResponseHeaders__InvalidQuery
(
t
*
testing
.
T
)
{
func
TestAbsoluteAndRelativeRedirects__OK
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/response-headers?foo=%ZZ"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusBadRequest
)
}
func
TestRedirects__OK
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
var
tests
=
[]
struct
{
relative
bool
relative
bool
n
int
n
int
...
@@ -782,7 +760,7 @@ func TestRedirects__OK(t *testing.T) {
...
@@ -782,7 +760,7 @@ func TestRedirects__OK(t *testing.T) {
}
}
}
}
func
TestRedirects__Errors
(
t
*
testing
.
T
)
{
func
Test
AbsoluteAndRelative
Redirects__Errors
(
t
*
testing
.
T
)
{
var
tests
=
[]
struct
{
var
tests
=
[]
struct
{
relative
bool
relative
bool
given
interface
{}
given
interface
{}
...
...
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