2011-04-15 08:13:52 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Reverse proxy tests.
|
|
|
|
|
|
2011-11-03 15:54:08 -07:00
|
|
|
package httputil
|
2011-04-15 08:13:52 -07:00
|
|
|
|
|
|
|
|
import (
|
2012-04-18 11:33:02 -07:00
|
|
|
"io"
|
2011-04-15 08:13:52 -07:00
|
|
|
"io/ioutil"
|
2011-11-08 15:41:54 -08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
2012-04-18 11:33:02 -07:00
|
|
|
"runtime"
|
2011-04-15 08:13:52 -07:00
|
|
|
"testing"
|
2012-04-18 11:33:02 -07:00
|
|
|
"time"
|
2011-04-15 08:13:52 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestReverseProxy(t *testing.T) {
|
|
|
|
|
const backendResponse = "I am the backend"
|
|
|
|
|
const backendStatus = 404
|
2011-11-03 15:54:08 -07:00
|
|
|
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2011-06-24 16:46:14 -07:00
|
|
|
if len(r.TransferEncoding) > 0 {
|
|
|
|
|
t.Errorf("backend got unexpected TransferEncoding: %v", r.TransferEncoding)
|
|
|
|
|
}
|
2011-04-15 08:13:52 -07:00
|
|
|
if r.Header.Get("X-Forwarded-For") == "" {
|
|
|
|
|
t.Errorf("didn't get X-Forwarded-For header")
|
|
|
|
|
}
|
2011-10-26 15:27:29 +09:00
|
|
|
if c := r.Header.Get("Connection"); c != "" {
|
|
|
|
|
t.Errorf("handler got Connection header value %q", c)
|
|
|
|
|
}
|
2011-05-27 11:06:53 -07:00
|
|
|
if g, e := r.Host, "some-name"; g != e {
|
|
|
|
|
t.Errorf("backend got Host header %q, want %q", g, e)
|
|
|
|
|
}
|
2011-04-15 08:13:52 -07:00
|
|
|
w.Header().Set("X-Foo", "bar")
|
2011-11-03 15:54:08 -07:00
|
|
|
http.SetCookie(w, &http.Cookie{Name: "flavor", Value: "chocolateChip"})
|
2011-04-15 08:13:52 -07:00
|
|
|
w.WriteHeader(backendStatus)
|
|
|
|
|
w.Write([]byte(backendResponse))
|
|
|
|
|
}))
|
|
|
|
|
defer backend.Close()
|
2011-08-17 13:36:02 +10:00
|
|
|
backendURL, err := url.Parse(backend.URL)
|
2011-04-15 08:13:52 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
proxyHandler := NewSingleHostReverseProxy(backendURL)
|
|
|
|
|
frontend := httptest.NewServer(proxyHandler)
|
|
|
|
|
defer frontend.Close()
|
|
|
|
|
|
2011-11-03 15:54:08 -07:00
|
|
|
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
|
2011-05-27 11:06:53 -07:00
|
|
|
getReq.Host = "some-name"
|
2011-10-26 15:27:29 +09:00
|
|
|
getReq.Header.Set("Connection", "close")
|
|
|
|
|
getReq.Close = true
|
2011-11-03 15:54:08 -07:00
|
|
|
res, err := http.DefaultClient.Do(getReq)
|
2011-04-15 08:13:52 -07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Get: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if g, e := res.StatusCode, backendStatus; g != e {
|
|
|
|
|
t.Errorf("got res.StatusCode %d; expected %d", g, e)
|
|
|
|
|
}
|
|
|
|
|
if g, e := res.Header.Get("X-Foo"), "bar"; g != e {
|
|
|
|
|
t.Errorf("got X-Foo %q; expected %q", g, e)
|
|
|
|
|
}
|
2011-06-16 13:02:28 -07:00
|
|
|
if g, e := len(res.Header["Set-Cookie"]), 1; g != e {
|
2011-05-27 11:06:53 -07:00
|
|
|
t.Fatalf("got %d SetCookies, want %d", g, e)
|
|
|
|
|
}
|
2011-06-16 13:02:28 -07:00
|
|
|
if cookie := res.Cookies()[0]; cookie.Name != "flavor" {
|
2011-05-27 11:06:53 -07:00
|
|
|
t.Errorf("unexpected cookie %q", cookie.Name)
|
|
|
|
|
}
|
2011-04-15 08:13:52 -07:00
|
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
if g, e := string(bodyBytes), backendResponse; g != e {
|
|
|
|
|
t.Errorf("got body %q; expected %q", g, e)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-07 18:00:30 -08:00
|
|
|
|
|
|
|
|
var proxyQueryTests = []struct {
|
|
|
|
|
baseSuffix string // suffix to add to backend URL
|
|
|
|
|
reqSuffix string // suffix to add to frontend's request URL
|
|
|
|
|
want string // what backend should see for final request URL (without ?)
|
|
|
|
|
}{
|
|
|
|
|
{"", "", ""},
|
|
|
|
|
{"?sta=tic", "?us=er", "sta=tic&us=er"},
|
|
|
|
|
{"", "?us=er", "us=er"},
|
|
|
|
|
{"?sta=tic", "", "sta=tic"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReverseProxyQuery(t *testing.T) {
|
|
|
|
|
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("X-Got-Query", r.URL.RawQuery)
|
|
|
|
|
w.Write([]byte("hi"))
|
|
|
|
|
}))
|
|
|
|
|
defer backend.Close()
|
|
|
|
|
|
|
|
|
|
for i, tt := range proxyQueryTests {
|
|
|
|
|
backendURL, err := url.Parse(backend.URL + tt.baseSuffix)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
frontend := httptest.NewServer(NewSingleHostReverseProxy(backendURL))
|
|
|
|
|
req, _ := http.NewRequest("GET", frontend.URL+tt.reqSuffix, nil)
|
|
|
|
|
req.Close = true
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("%d. Get: %v", i, err)
|
|
|
|
|
}
|
|
|
|
|
if g, e := res.Header.Get("X-Got-Query"), tt.want; g != e {
|
|
|
|
|
t.Errorf("%d. got query %q; expected %q", i, g, e)
|
|
|
|
|
}
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
frontend.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-18 11:33:02 -07:00
|
|
|
|
|
|
|
|
func TestReverseProxyFlushInterval(t *testing.T) {
|
|
|
|
|
if testing.Short() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const expected = "hi"
|
|
|
|
|
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Write([]byte(expected))
|
|
|
|
|
}))
|
|
|
|
|
defer backend.Close()
|
|
|
|
|
|
|
|
|
|
backendURL, err := url.Parse(backend.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
proxyHandler := NewSingleHostReverseProxy(backendURL)
|
|
|
|
|
proxyHandler.FlushInterval = time.Microsecond
|
|
|
|
|
|
|
|
|
|
dstChan := make(chan io.Writer, 1)
|
|
|
|
|
beforeCopyResponse = func(dst io.Writer) { dstChan <- dst }
|
|
|
|
|
defer func() { beforeCopyResponse = nil }()
|
|
|
|
|
|
|
|
|
|
frontend := httptest.NewServer(proxyHandler)
|
|
|
|
|
defer frontend.Close()
|
|
|
|
|
|
|
|
|
|
initGoroutines := runtime.NumGoroutine()
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
|
req, _ := http.NewRequest("GET", frontend.URL, nil)
|
|
|
|
|
req.Close = true
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Get: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if bodyBytes, _ := ioutil.ReadAll(res.Body); string(bodyBytes) != expected {
|
|
|
|
|
t.Errorf("got body %q; expected %q", bodyBytes, expected)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case dst := <-dstChan:
|
|
|
|
|
if _, ok := dst.(*maxLatencyWriter); !ok {
|
|
|
|
|
t.Errorf("got writer %T; expected %T", dst, &maxLatencyWriter{})
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
t.Error("maxLatencyWriter Write() was never called")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
// Allow up to 50 additional goroutines over 100 requests.
|
|
|
|
|
if delta := runtime.NumGoroutine() - initGoroutines; delta > 50 {
|
|
|
|
|
t.Errorf("grew %d goroutines; leak?", delta)
|
|
|
|
|
}
|
|
|
|
|
}
|