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
d0f764cc
Commit
d0f764cc
authored
8 years ago
by
Will McCutchen
Browse files
Options
Downloads
Patches
Plain Diff
Add /cookies endpoint
parent
680b6e63
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
+13
-0
13 additions, 0 deletions
httpbin/handlers.go
httpbin/handlers_test.go
+47
-0
47 additions, 0 deletions
httpbin/handlers_test.go
httpbin/httpbin.go
+4
-0
4 additions, 0 deletions
httpbin/httpbin.go
with
64 additions
and
0 deletions
httpbin/handlers.go
+
13
−
0
View file @
d0f764cc
...
@@ -241,3 +241,16 @@ func (h *HTTPBin) RelativeRedirect(w http.ResponseWriter, r *http.Request) {
...
@@ -241,3 +241,16 @@ func (h *HTTPBin) RelativeRedirect(w http.ResponseWriter, r *http.Request) {
func
(
h
*
HTTPBin
)
AbsoluteRedirect
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
h
*
HTTPBin
)
AbsoluteRedirect
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
doRedirect
(
w
,
r
,
false
)
doRedirect
(
w
,
r
,
false
)
}
}
// Cookies responds with the cookies in the incoming request
func
(
h
*
HTTPBin
)
Cookies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
resp
:=
cookiesResponse
{}
for
_
,
c
:=
range
r
.
Cookies
()
{
if
_
,
found
:=
resp
[
c
.
Name
];
!
found
{
resp
[
c
.
Name
]
=
[]
string
{}
}
resp
[
c
.
Name
]
=
append
(
resp
[
c
.
Name
],
c
.
Value
)
}
body
,
_
:=
json
.
Marshal
(
resp
)
writeJSON
(
w
,
body
,
http
.
StatusOK
)
}
This diff is collapsed.
Click to expand it.
httpbin/handlers_test.go
+
47
−
0
View file @
d0f764cc
...
@@ -808,3 +808,50 @@ func TestRedirects(t *testing.T) {
...
@@ -808,3 +808,50 @@ func TestRedirects(t *testing.T) {
})
})
}
}
}
}
func
TestCookies
(
t
*
testing
.
T
)
{
testCookies
:=
func
(
t
*
testing
.
T
,
cookies
cookiesResponse
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/cookies"
,
nil
)
for
k
,
vs
:=
range
cookies
{
for
_
,
v
:=
range
vs
{
r
.
AddCookie
(
&
http
.
Cookie
{
Name
:
k
,
Value
:
v
,
})
}
}
w
:=
httptest
.
NewRecorder
()
handler
.
ServeHTTP
(
w
,
r
)
assertStatusCode
(
t
,
w
,
http
.
StatusOK
)
assertContentType
(
t
,
w
,
jsonContentType
)
resp
:=
cookiesResponse
{}
err
:=
json
.
Unmarshal
(
w
.
Body
.
Bytes
(),
&
resp
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to unmarshal body %s from JSON: %s"
,
w
.
Body
,
err
)
}
if
!
reflect
.
DeepEqual
(
cookies
,
resp
)
{
t
.
Fatalf
(
"expected cookies %#v, got %#v"
,
cookies
,
resp
)
}
}
t
.
Run
(
"no cookies"
,
func
(
t
*
testing
.
T
)
{
testCookies
(
t
,
cookiesResponse
{})
})
t
.
Run
(
"single cookies"
,
func
(
t
*
testing
.
T
)
{
testCookies
(
t
,
cookiesResponse
{
"k1"
:
{
"v1"
},
"k2"
:
{
"v2"
},
})
})
t
.
Run
(
"duplicate cookies"
,
func
(
t
*
testing
.
T
)
{
testCookies
(
t
,
cookiesResponse
{
"k1"
:
{
"v1"
},
"k2"
:
{
"v2a"
,
"v2b"
},
})
})
}
This diff is collapsed.
Click to expand it.
httpbin/httpbin.go
+
4
−
0
View file @
d0f764cc
...
@@ -39,6 +39,8 @@ type bodyResponse struct {
...
@@ -39,6 +39,8 @@ type bodyResponse struct {
JSON
interface
{}
`json:"json"`
JSON
interface
{}
`json:"json"`
}
}
type
cookiesResponse
map
[
string
][]
string
// Options are used to configure HTTPBin
// Options are used to configure HTTPBin
type
Options
struct
{
type
Options
struct
{
MaxMemory
int64
MaxMemory
int64
...
@@ -73,6 +75,8 @@ func (h *HTTPBin) Handler() http.Handler {
...
@@ -73,6 +75,8 @@ func (h *HTTPBin) Handler() http.Handler {
mux
.
HandleFunc
(
"/relative-redirect/"
,
h
.
RelativeRedirect
)
mux
.
HandleFunc
(
"/relative-redirect/"
,
h
.
RelativeRedirect
)
mux
.
HandleFunc
(
"/absolute-redirect/"
,
h
.
AbsoluteRedirect
)
mux
.
HandleFunc
(
"/absolute-redirect/"
,
h
.
AbsoluteRedirect
)
mux
.
HandleFunc
(
"/cookies"
,
h
.
Cookies
)
// Make sure our ServeMux doesn't "helpfully" redirect these invalid
// Make sure our ServeMux doesn't "helpfully" redirect these invalid
// endpoints by adding a trailing slash. See the ServeMux docs for more
// endpoints by adding a trailing slash. See the ServeMux docs for more
// info: https://golang.org/pkg/net/http/#ServeMux
// info: https://golang.org/pkg/net/http/#ServeMux
...
...
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