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
b9f4b1f5
Commit
b9f4b1f5
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add /response-headers handler
parent
8af0b578
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
httpbin/handlers.go
+20
-0
20 additions, 0 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+71
-0
71 additions, 0 deletions
httpbin/handlers_test.go
httpbin/httpbin.go
+1
-0
1 addition, 0 deletions
httpbin/httpbin.go
with
92 additions
and
0 deletions
httpbin/handlers.go
+
20
−
0
View file @
b9f4b1f5
...
@@ -176,3 +176,23 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
...
@@ -176,3 +176,23 @@ func (h *HTTPBin) Status(w http.ResponseWriter, r *http.Request) {
w
.
WriteHeader
(
code
)
w
.
WriteHeader
(
code
)
}
}
}
}
// ResponseHeaders responds with a map of header values
func
(
h
*
HTTPBin
)
ResponseHeaders
(
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
}
for
k
,
vs
:=
range
args
{
for
_
,
v
:=
range
vs
{
w
.
Header
()
.
Add
(
http
.
CanonicalHeaderKey
(
k
),
v
)
}
}
body
,
_
:=
json
.
Marshal
(
args
)
if
contentType
:=
w
.
Header
()
.
Get
(
"Content-Type"
);
contentType
==
""
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json; encoding=utf-8"
)
}
w
.
Write
(
body
)
}
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
71
−
0
View file @
b9f4b1f5
...
@@ -109,6 +109,30 @@ func TestGet__Basic(t *testing.T) {
...
@@ -109,6 +109,30 @@ func TestGet__Basic(t *testing.T) {
}
}
}
}
func
TestGet__WithParams
(
t
*
testing
.
T
)
{
params
:=
url
.
Values
{}
params
.
Set
(
"foo"
,
"foo"
)
params
.
Add
(
"bar"
,
"bar1"
)
params
.
Add
(
"bar"
,
"bar2"
)
r
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"/get?%s"
,
params
.
Encode
()),
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
"application/json; encoding=utf-8"
)
var
resp
*
bodyResponse
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
resp
.
Args
.
Encode
()
!=
params
.
Encode
()
{
t
.
Fatalf
(
"args mismatch: %s != %s"
,
resp
.
Args
.
Encode
(),
params
.
Encode
())
}
}
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
()
...
@@ -461,3 +485,50 @@ func TestStatus__Simple(t *testing.T) {
...
@@ -461,3 +485,50 @@ func TestStatus__Simple(t *testing.T) {
}
}
}
}
}
}
func
TestResponseHeaders
(
t
*
testing
.
T
)
{
headers
:=
map
[
string
][]
string
{
"Content-Type"
:
{
"test/test"
},
"Foo"
:
{
"foo"
},
"Bar"
:
{
"bar1, bar2"
},
}
params
:=
url
.
Values
{}
for
k
,
vs
:=
range
headers
{
for
_
,
v
:=
range
vs
{
params
.
Add
(
k
,
v
)
}
}
r
,
_
:=
http
.
NewRequest
(
"GET"
,
fmt
.
Sprintf
(
"/response-headers?%s"
,
params
.
Encode
()),
nil
)
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
for
k
,
expectedValues
:=
range
headers
{
values
,
ok
:=
w
.
HeaderMap
[
k
]
if
!
ok
{
t
.
Fatalf
(
"expected header %s in response headers"
,
k
)
}
if
!
reflect
.
DeepEqual
(
values
,
expectedValues
)
{
t
.
Fatalf
(
"expected key values %#v for header %s, got %#v"
,
expectedValues
,
k
,
values
)
}
}
resp
:=
&
http
.
Header
{}
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
for
k
,
expectedValues
:=
range
headers
{
values
,
ok
:=
(
*
resp
)[
k
]
if
!
ok
{
t
.
Fatalf
(
"expected header %s in response body"
,
k
)
}
if
!
reflect
.
DeepEqual
(
values
,
expectedValues
)
{
t
.
Fatalf
(
"expected key values %#v for header %s, got %#v"
,
expectedValues
,
k
,
values
)
}
}
}
This diff is collapsed.
Click to expand it.
httpbin/httpbin.go
+
1
−
0
View file @
b9f4b1f5
...
@@ -65,6 +65,7 @@ func (h *HTTPBin) Handler() http.Handler {
...
@@ -65,6 +65,7 @@ func (h *HTTPBin) Handler() http.Handler {
mux
.
HandleFunc
(
"/user-agent"
,
h
.
UserAgent
)
mux
.
HandleFunc
(
"/user-agent"
,
h
.
UserAgent
)
mux
.
HandleFunc
(
"/headers"
,
h
.
Headers
)
mux
.
HandleFunc
(
"/headers"
,
h
.
Headers
)
mux
.
HandleFunc
(
"/status/"
,
h
.
Status
)
mux
.
HandleFunc
(
"/status/"
,
h
.
Status
)
mux
.
HandleFunc
(
"/response-headers"
,
h
.
ResponseHeaders
)
return
logger
(
cors
(
mux
))
return
logger
(
cors
(
mux
))
}
}
...
...
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