Skip to content
Snippets Groups Projects
Commit d0f764cc authored by Will McCutchen's avatar Will McCutchen
Browse files

Add /cookies endpoint

parent 680b6e63
No related branches found
No related tags found
No related merge requests found
...@@ -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)
}
...@@ -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"},
})
})
}
...@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment