Commit 0b204e46 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http: propagate Set-Cookie in reverse proxy

Also adds Host header tests.

Fixes #1886

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/4556063
parent b88be149
......@@ -92,6 +92,10 @@ func (p *ReverseProxy) ServeHTTP(rw ResponseWriter, req *Request) {
}
}
for _, cookie := range res.SetCookie {
SetCookie(rw, cookie)
}
rw.WriteHeader(res.StatusCode)
if res.Body != nil {
......
......@@ -20,7 +20,11 @@ func TestReverseProxy(t *testing.T) {
if r.Header.Get("X-Forwarded-For") == "" {
t.Errorf("didn't get X-Forwarded-For header")
}
if g, e := r.Host, "some-name"; g != e {
t.Errorf("backend got Host header %q, want %q", g, e)
}
w.Header().Set("X-Foo", "bar")
SetCookie(w, &Cookie{Name: "flavor", Value: "chocolateChip"})
w.WriteHeader(backendStatus)
w.Write([]byte(backendResponse))
}))
......@@ -33,7 +37,9 @@ func TestReverseProxy(t *testing.T) {
frontend := httptest.NewServer(proxyHandler)
defer frontend.Close()
res, err := Get(frontend.URL)
getReq, _ := NewRequest("GET", frontend.URL, nil)
getReq.Host = "some-name"
res, err := DefaultClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
......@@ -43,6 +49,12 @@ func TestReverseProxy(t *testing.T) {
if g, e := res.Header.Get("X-Foo"), "bar"; g != e {
t.Errorf("got X-Foo %q; expected %q", g, e)
}
if g, e := len(res.SetCookie), 1; g != e {
t.Fatalf("got %d SetCookies, want %d", g, e)
}
if cookie := res.SetCookie[0]; cookie.Name != "flavor" {
t.Errorf("unexpected cookie %q", cookie.Name)
}
bodyBytes, _ := ioutil.ReadAll(res.Body)
if g, e := string(bodyBytes), backendResponse; g != e {
t.Errorf("got body %q; expected %q", g, e)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment