1
0
mirror of https://github.com/golang/go synced 2024-11-24 22:27:57 -07:00

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
This commit is contained in:
Brad Fitzpatrick 2011-05-27 11:06:53 -07:00
parent b88be14982
commit 0b204e4625
2 changed files with 17 additions and 1 deletions

View File

@ -92,6 +92,10 @@ func (p *ReverseProxy) ServeHTTP(rw ResponseWriter, req *Request) {
} }
} }
for _, cookie := range res.SetCookie {
SetCookie(rw, cookie)
}
rw.WriteHeader(res.StatusCode) rw.WriteHeader(res.StatusCode)
if res.Body != nil { if res.Body != nil {

View File

@ -20,7 +20,11 @@ func TestReverseProxy(t *testing.T) {
if r.Header.Get("X-Forwarded-For") == "" { if r.Header.Get("X-Forwarded-For") == "" {
t.Errorf("didn't get X-Forwarded-For header") 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") w.Header().Set("X-Foo", "bar")
SetCookie(w, &Cookie{Name: "flavor", Value: "chocolateChip"})
w.WriteHeader(backendStatus) w.WriteHeader(backendStatus)
w.Write([]byte(backendResponse)) w.Write([]byte(backendResponse))
})) }))
@ -33,7 +37,9 @@ func TestReverseProxy(t *testing.T) {
frontend := httptest.NewServer(proxyHandler) frontend := httptest.NewServer(proxyHandler)
defer frontend.Close() 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 { if err != nil {
t.Fatalf("Get: %v", err) 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 { if g, e := res.Header.Get("X-Foo"), "bar"; g != e {
t.Errorf("got X-Foo %q; expected %q", 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) bodyBytes, _ := ioutil.ReadAll(res.Body)
if g, e := string(bodyBytes), backendResponse; g != e { if g, e := string(bodyBytes), backendResponse; g != e {
t.Errorf("got body %q; expected %q", g, e) t.Errorf("got body %q; expected %q", g, e)