2015-01-18 15:50:20 +01:00
|
|
|
package nghttp2
|
|
|
|
|
|
|
|
import (
|
2015-01-25 07:36:14 +01:00
|
|
|
"crypto/tls"
|
2015-01-18 15:50:20 +01:00
|
|
|
"fmt"
|
2015-10-08 17:08:54 +02:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/hpack"
|
2015-01-20 14:55:01 +01:00
|
|
|
"io"
|
2015-01-19 14:20:56 +01:00
|
|
|
"io/ioutil"
|
2015-01-18 15:50:20 +01:00
|
|
|
"net/http"
|
2016-01-15 16:59:14 +01:00
|
|
|
"regexp"
|
2015-04-08 09:09:02 +02:00
|
|
|
"strings"
|
2015-01-21 17:43:56 +01:00
|
|
|
"syscall"
|
2015-01-18 15:50:20 +01:00
|
|
|
"testing"
|
2015-09-23 17:11:14 +02:00
|
|
|
"time"
|
2015-01-18 15:50:20 +01:00
|
|
|
)
|
|
|
|
|
2015-01-26 16:19:41 +01:00
|
|
|
// TestH2H1PlainGET tests whether simple HTTP/2 GET request works.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1PlainGET(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-20 14:25:56 +01:00
|
|
|
name: "TestH2H1PlainGET",
|
2015-01-18 15:50:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
want := 200
|
|
|
|
if res.status != want {
|
|
|
|
t.Errorf("status = %v; want %v", res.status, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1AddXff tests that server generates X-Forwarded-For header
|
|
|
|
// field when forwarding request to backend.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1AddXff(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester([]string{"--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
2016-01-16 04:06:41 +01:00
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1AddXff",
|
2015-01-18 15:50:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2016-01-16 04:06:41 +01:00
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1AddXff2 tests that server appends X-Forwarded-For header
|
|
|
|
// field to existing one when forwarding request to backend.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1AddXff2(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester([]string{"--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "host, 127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
2016-01-16 04:06:41 +01:00
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1AddXff2",
|
2015-01-19 13:24:18 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
2015-01-18 15:50:20 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2016-01-16 04:06:41 +01:00
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1StripXff tests that --strip-incoming-x-forwarded-for
|
|
|
|
// option.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1StripXff(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester([]string{"--strip-incoming-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if xff, found := r.Header["X-Forwarded-For"]; found {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want nothing", xff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
2016-01-16 04:06:41 +01:00
|
|
|
res, err := st.http2(requestParam{
|
2016-01-16 04:25:12 +01:00
|
|
|
name: "TestH2H1StripXff",
|
2015-01-19 13:24:18 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
2015-01-18 15:50:20 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2016-01-16 04:06:41 +01:00
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1StripAddXff tests that --strip-incoming-x-forwarded-for and
|
|
|
|
// --add-x-forwarded-for options.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1StripAddXff(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
args := []string{
|
|
|
|
"--strip-incoming-x-forwarded-for",
|
|
|
|
"--add-x-forwarded-for",
|
|
|
|
}
|
|
|
|
st := newServerTester(args, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
2016-01-16 04:06:41 +01:00
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1StripAddXff",
|
2015-01-19 13:24:18 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
2015-01-18 15:50:20 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
2016-01-16 04:06:41 +01:00
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:59:14 +01:00
|
|
|
// TestH2H1AddForwardedObfuscated tests that server generates
|
|
|
|
// Forwarded header field with obfuscated "by" and "for" parameters.
|
|
|
|
func TestH2H1AddForwardedObfuscated(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--add-forwarded=by,for,host,proto"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
pattern := fmt.Sprintf(`by=_[^;]+;for=_[^;]+;host="127.0.0.1:%v";proto=http`, serverPort)
|
2016-01-15 16:59:14 +01:00
|
|
|
validFwd := regexp.MustCompile(pattern)
|
|
|
|
got := r.Header.Get("Forwarded")
|
|
|
|
|
|
|
|
if !validFwd.MatchString(got) {
|
|
|
|
t.Errorf("Forwarded = %v; want pattern %v", got, pattern)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedObfuscated",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1AddForwardedByIP tests that server generates Forwarded header
|
|
|
|
// field with IP address in "by" parameter.
|
|
|
|
func TestH2H1AddForwardedByIP(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--add-forwarded=by,for,host,proto", "--forwarded-by=ip", "--forwarded-for=_bravo"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
want := fmt.Sprintf(`by="127.0.0.1:%v";for=_bravo;host="127.0.0.1:%v";proto=http`, serverPort, serverPort)
|
2016-01-15 16:59:14 +01:00
|
|
|
if got := r.Header.Get("Forwarded"); got != want {
|
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedByIP",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1AddForwardedForIP tests that server generates Forwarded header
|
|
|
|
// field with IP address in "for" parameters.
|
|
|
|
func TestH2H1AddForwardedForIP(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--add-forwarded=by,for,host,proto", "--forwarded-by=_alpha", "--forwarded-for=ip"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
want := fmt.Sprintf(`by=_alpha;for=127.0.0.1;host="127.0.0.1:%v";proto=http`, serverPort)
|
2016-01-15 16:59:14 +01:00
|
|
|
if got := r.Header.Get("Forwarded"); got != want {
|
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedForIP",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1AddForwardedMerge tests that server generates Forwarded
|
|
|
|
// header field with IP address in "by" and "for" parameters. The
|
|
|
|
// generated values must be appended to the existing value.
|
|
|
|
func TestH2H1AddForwardedMerge(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--add-forwarded=proto"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
if got, want := r.Header.Get("Forwarded"), `host=foo, proto=http`; got != want {
|
2016-01-15 16:59:14 +01:00
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedMerge",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1AddForwardedStrip tests that server generates Forwarded
|
|
|
|
// header field with IP address in "by" and "for" parameters. The
|
|
|
|
// generated values must not include the existing value.
|
|
|
|
func TestH2H1AddForwardedStrip(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--strip-incoming-forwarded", "--add-forwarded=proto"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
if got, want := r.Header.Get("Forwarded"), `proto=http`; got != want {
|
2016-01-15 16:59:14 +01:00
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedStrip",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 04:19:19 +01:00
|
|
|
// TestH2H1StripForwarded tests that server strips incoming Forwarded
|
|
|
|
// header field.
|
|
|
|
func TestH2H1StripForwarded(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--strip-incoming-forwarded"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, found := r.Header["Forwarded"]; found {
|
|
|
|
t.Errorf("Forwarded = %v; want nothing", got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1StripForwarded",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:59:14 +01:00
|
|
|
// TestH2H1AddForwardedStatic tests that server generates Forwarded
|
|
|
|
// header field with the given static obfuscated strings for "by" and
|
|
|
|
// "for" parameters.
|
|
|
|
func TestH2H1AddForwardedStatic(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--add-forwarded=by,for", "--forwarded-by=_alpha", "--forwarded-for=_bravo"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
if got, want := r.Header.Get("Forwarded"), `by=_alpha;for=_bravo`; got != want {
|
2016-01-15 16:59:14 +01:00
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AddForwardedStatic",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 16:19:57 +01:00
|
|
|
// TestH2H1GenerateVia tests that server generates Via header field to and
|
|
|
|
// from backend server.
|
|
|
|
func TestH2H1GenerateVia(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-24 06:43:01 +01:00
|
|
|
if got, want := r.Header.Get("Via"), "2 nghttpx"; got != want {
|
2015-01-26 16:19:57 +01:00
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1GenerateVia",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("Via"), "1.1 nghttpx"; got != want {
|
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1AppendVia tests that server adds value to existing Via
|
|
|
|
// header field to and from backend server.
|
|
|
|
func TestH2H1AppendVia(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-24 06:43:01 +01:00
|
|
|
if got, want := r.Header.Get("Via"), "foo, 2 nghttpx"; got != want {
|
2015-01-26 16:19:57 +01:00
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
w.Header().Add("Via", "bar")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AppendVia",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("via", "foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("Via"), "bar, 1.1 nghttpx"; got != want {
|
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1NoVia tests that server does not add value to existing Via
|
|
|
|
// header field to and from backend server.
|
|
|
|
func TestH2H1NoVia(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--no-via"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("Via"), "foo"; got != want {
|
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
w.Header().Add("Via", "bar")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1NoVia",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("via", "foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("Via"), "bar"; got != want {
|
|
|
|
t.Errorf("Via: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 17:15:56 +01:00
|
|
|
// TestH2H1HostRewrite tests that server rewrites host header field
|
|
|
|
func TestH2H1HostRewrite(t *testing.T) {
|
2015-08-08 18:23:58 +02:00
|
|
|
st := newServerTester([]string{"--host-rewrite"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-03 17:15:56 +01:00
|
|
|
w.Header().Add("request-host", r.Host)
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1HostRewrite",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("request-host"), st.backendHost; got != want {
|
|
|
|
t.Errorf("request-host: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 17:41:20 +01:00
|
|
|
// TestH2H1NoHostRewrite tests that server does not rewrite host
|
|
|
|
// header field
|
|
|
|
func TestH2H1NoHostRewrite(t *testing.T) {
|
2015-08-08 18:23:58 +02:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-03 17:41:20 +01:00
|
|
|
w.Header().Add("request-host", r.Host)
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1NoHostRewrite",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("request-host"), st.frontendHost; got != want {
|
|
|
|
t.Errorf("request-host: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1BadRequestCL tests that server rejects request whose
|
|
|
|
// content-length header field value does not match its request body
|
|
|
|
// size.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1BadRequestCL(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
// we set content-length: 1024, but the actual request body is
|
|
|
|
// 3 bytes.
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1BadRequestCL",
|
2015-01-18 15:50:20 +01:00
|
|
|
method: "POST",
|
2015-01-19 13:24:18 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("content-length", "1024"),
|
2015-01-18 15:50:20 +01:00
|
|
|
},
|
|
|
|
body: []byte("foo"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
want := http2.ErrCodeProtocol
|
|
|
|
if res.errCode != want {
|
|
|
|
t.Errorf("res.errCode = %v; want %v", res.errCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1BadResponseCL tests that server returns error when
|
|
|
|
// content-length response header field value does not match its
|
|
|
|
// response body size.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1BadResponseCL(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// we set content-length: 1024, but only send 3 bytes.
|
|
|
|
w.Header().Add("Content-Length", "1024")
|
|
|
|
w.Write([]byte("foo"))
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1BadResponseCL",
|
2015-01-18 15:50:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
want := http2.ErrCodeProtocol
|
|
|
|
if res.errCode != want {
|
|
|
|
t.Errorf("res.errCode = %v; want %v", res.errCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1LocationRewrite tests location header field rewriting
|
|
|
|
// works.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1LocationRewrite(t *testing.T) {
|
2015-01-18 15:50:20 +01:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// TODO we cannot get st.ts's port number here.. 8443
|
|
|
|
// is just a place holder. We ignore it on rewrite.
|
|
|
|
w.Header().Add("Location", "http://127.0.0.1:8443/p/q?a=b#fragment")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1LocationRewrite",
|
2015-01-18 15:50:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-18 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
want := fmt.Sprintf("http://127.0.0.1:%v/p/q?a=b#fragment", serverPort)
|
|
|
|
if got := res.header.Get("Location"); got != want {
|
|
|
|
t.Errorf("Location: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 14:20:56 +01:00
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1ChunkedRequestBody tests that chunked request body works.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1ChunkedRequestBody(t *testing.T) {
|
2015-01-19 14:20:56 +01:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
want := "[chunked]"
|
|
|
|
if got := fmt.Sprint(r.TransferEncoding); got != want {
|
|
|
|
t.Errorf("Transfer-Encoding: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error reading r.body: %v", err)
|
2015-01-19 14:20:56 +01:00
|
|
|
}
|
|
|
|
want = "foo"
|
|
|
|
if got := string(body); got != want {
|
|
|
|
t.Errorf("body: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
2016-01-16 04:06:41 +01:00
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1ChunkedRequestBody",
|
2015-01-19 14:20:56 +01:00
|
|
|
method: "POST",
|
|
|
|
body: []byte("foo"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-19 14:20:56 +01:00
|
|
|
}
|
2016-01-16 04:06:41 +01:00
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
2015-01-19 14:20:56 +01:00
|
|
|
}
|
2015-01-19 14:40:37 +01:00
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1MultipleRequestCL tests that server rejects request with
|
|
|
|
// multiple Content-Length request header fields.
|
2015-01-20 14:56:36 +01:00
|
|
|
func TestH2H1MultipleRequestCL(t *testing.T) {
|
2015-01-20 14:31:21 +01:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward bad request")
|
|
|
|
})
|
2015-01-19 14:40:37 +01:00
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-20 14:56:36 +01:00
|
|
|
name: "TestH2H1MultipleRequestCL",
|
2015-01-19 14:40:37 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("content-length", "1"),
|
2015-01-23 16:07:28 +01:00
|
|
|
pair("content-length", "1"),
|
2015-01-19 14:40:37 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-19 14:40:37 +01:00
|
|
|
}
|
2015-02-19 16:01:15 +01:00
|
|
|
if got, want := res.errCode, http2.ErrCodeProtocol; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
2015-01-19 14:40:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1InvalidRequestCL tests that server rejects request with
|
|
|
|
// Content-Length which cannot be parsed as a number.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1InvalidRequestCL(t *testing.T) {
|
2015-01-20 14:31:21 +01:00
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward bad request")
|
|
|
|
})
|
2015-01-19 14:40:37 +01:00
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1InvalidRequestCL",
|
2015-01-19 14:40:37 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("content-length", ""),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-19 14:40:37 +01:00
|
|
|
}
|
2015-02-19 16:01:15 +01:00
|
|
|
if got, want := res.errCode, http2.ErrCodeProtocol; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
2015-01-20 15:26:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1ConnectFailure tests that server handles the situation that
|
|
|
|
// connection attempt to HTTP/1 backend failed.
|
2015-01-20 15:26:09 +01:00
|
|
|
func TestH2H1ConnectFailure(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
// shutdown backend server to simulate backend connect failure
|
|
|
|
st.ts.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ConnectFailure",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
2015-01-21 15:08:13 +01:00
|
|
|
want := 503
|
2015-01-20 15:26:09 +01:00
|
|
|
if got := res.status; got != want {
|
2015-01-19 14:40:37 +01:00
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 16:45:51 +01:00
|
|
|
|
2015-06-09 16:15:02 +02:00
|
|
|
// TestH2H1InvalidMethod tests that server rejects invalid method with
|
|
|
|
// 501.
|
|
|
|
func TestH2H1InvalidMethod(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward this request")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1InvalidMethod",
|
|
|
|
method: "get",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 501; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 07:59:24 +01:00
|
|
|
// TestH2H1BadAuthority tests that server rejects request including
|
|
|
|
// bad characters in :authority header field.
|
|
|
|
func TestH2H1BadAuthority(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward this request")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2016-01-19 14:27:09 +01:00
|
|
|
name: "TestH2H1BadAuthority",
|
2016-01-16 07:59:24 +01:00
|
|
|
authority: `foo\bar`,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.errCode, http2.ErrCodeProtocol; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1BadScheme tests that server rejects request including
|
|
|
|
// bad characters in :scheme header field.
|
|
|
|
func TestH2H1BadScheme(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward this request")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2016-01-19 14:27:09 +01:00
|
|
|
name: "TestH2H1BadScheme",
|
2016-01-16 07:59:24 +01:00
|
|
|
scheme: "http*",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.errCode, http2.ErrCodeProtocol; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:17:11 +01:00
|
|
|
// TestH2H1AssembleCookies tests that crumbled cookies in HTTP/2
|
|
|
|
// request is assembled into 1 when forwarding to HTTP/1 backend link.
|
|
|
|
func TestH2H1AssembleCookies(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("Cookie"), "alpha; bravo; charlie"; got != want {
|
|
|
|
t.Errorf("Cookie: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1AssembleCookies",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("cookie", "alpha"),
|
|
|
|
pair("cookie", "bravo"),
|
|
|
|
pair("cookie", "charlie"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 14:43:48 +01:00
|
|
|
// TestH2H1TETrailers tests that server accepts TE request header
|
|
|
|
// field if it has trailers only.
|
|
|
|
func TestH2H1TETrailers(t *testing.T) {
|
2015-01-24 08:02:03 +01:00
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-25 14:43:48 +01:00
|
|
|
name: "TestH2H1TETrailers",
|
2015-01-24 08:02:03 +01:00
|
|
|
header: []hpack.HeaderField{
|
2015-01-25 14:43:48 +01:00
|
|
|
pair("te", "trailers"),
|
2015-01-24 08:02:03 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 14:43:48 +01:00
|
|
|
// TestH2H1TEGzip tests that server resets stream if TE request header
|
|
|
|
// field contains gzip.
|
2015-01-24 08:02:03 +01:00
|
|
|
func TestH2H1TEGzip(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Error("server should not forward bad request")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1TEGzip",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("te", "gzip"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.errCode, http2.ErrCodeProtocol; got != want {
|
|
|
|
t.Errorf("res.errCode = %v; want %v", res.errCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 06:27:46 +01:00
|
|
|
// TestH2H1SNI tests server's TLS SNI extension feature. It must
|
|
|
|
// choose appropriate certificate depending on the indicated
|
|
|
|
// server_name from client.
|
2015-01-25 07:36:14 +01:00
|
|
|
func TestH2H1SNI(t *testing.T) {
|
|
|
|
st := newServerTesterTLSConfig([]string{"--subcert=" + testDir + "/alt-server.key:" + testDir + "/alt-server.crt"}, t, noopHandler, &tls.Config{
|
|
|
|
ServerName: "alt-domain",
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
tlsConn := st.conn.(*tls.Conn)
|
|
|
|
connState := tlsConn.ConnectionState()
|
|
|
|
cert := connState.PeerCertificates[0]
|
|
|
|
|
|
|
|
if got, want := cert.Subject.CommonName, "alt-domain"; got != want {
|
|
|
|
t.Errorf("CommonName: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:31:47 +02:00
|
|
|
// TestH2H1TLSXfp tests nghttpx sends x-forwarded-proto header field
|
|
|
|
// with http value since :scheme is http, even if the frontend
|
|
|
|
// connection is encrypted.
|
|
|
|
func TestH2H1TLSXfp(t *testing.T) {
|
|
|
|
st := newServerTesterTLS(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("x-forwarded-proto"), "http"; got != want {
|
|
|
|
t.Errorf("x-forwarded-proto: want %v; got %v", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1TLSXfp",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 07:56:34 +01:00
|
|
|
// TestH2H1ServerPush tests server push using Link header field from
|
|
|
|
// backend server.
|
|
|
|
func TestH2H1ServerPush(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// only resources marked as rel=preload are pushed
|
2015-04-08 09:09:02 +02:00
|
|
|
if !strings.HasPrefix(r.URL.Path, "/css/") {
|
|
|
|
w.Header().Add("Link", "</css/main.css>; rel=preload, </foo>, </css/theme.css>; rel=preload")
|
|
|
|
}
|
2015-02-08 07:56:34 +01:00
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ServerPush",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := len(res.pushResponse), 2; got != want {
|
|
|
|
t.Fatalf("len(res.pushResponse): %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
mainCSS := res.pushResponse[0]
|
|
|
|
if got, want := mainCSS.status, 200; got != want {
|
|
|
|
t.Errorf("mainCSS.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
themeCSS := res.pushResponse[1]
|
|
|
|
if got, want := themeCSS.status, 200; got != want {
|
|
|
|
t.Errorf("themeCSS.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 11:31:43 +01:00
|
|
|
// TestH2H1RequestTrailer tests request trailer part is forwarded to
|
|
|
|
// backend.
|
|
|
|
func TestH2H1RequestTrailer(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
|
|
|
_, err := r.Body.Read(buf)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("r.Body.Read() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if got, want := r.Trailer.Get("foo"), "bar"; got != want {
|
|
|
|
t.Errorf("r.Trailer.Get(foo): %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1RequestTrailer",
|
|
|
|
body: []byte("1"),
|
|
|
|
trailer: []hpack.HeaderField{
|
|
|
|
pair("foo", "bar"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-04-10 14:28:12 +02:00
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:54:25 +02:00
|
|
|
// TestH2H1HeaderFieldBuffer tests that request with header fields
|
|
|
|
// larger than configured buffer size is rejected.
|
|
|
|
func TestH2H1HeaderFieldBuffer(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--header-field-buffer=10"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Fatal("execution path should not be here")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1HeaderFieldBuffer",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 431; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1HeaderFields tests that request with header fields more
|
|
|
|
// than configured number is rejected.
|
|
|
|
func TestH2H1HeaderFields(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--max-header-fields=1"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Fatal("execution path should not be here")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1HeaderFields",
|
|
|
|
// we have at least 4 pseudo-header fields sent, and
|
|
|
|
// that ensures that buffer limit exceeds.
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 431; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 19:52:19 +02:00
|
|
|
// TestH2H1ReqPhaseSetHeader tests mruby request phase hook
|
|
|
|
// modifies request header fields.
|
|
|
|
func TestH2H1ReqPhaseSetHeader(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--mruby-file=" + testDir + "/req-set-header.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-09-03 19:52:19 +02:00
|
|
|
if got, want := r.Header.Get("User-Agent"), "mruby"; got != want {
|
|
|
|
t.Errorf("User-Agent = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ReqPhaseSetHeader",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ReqPhaseReturn tests mruby request phase hook returns
|
|
|
|
// custom response.
|
|
|
|
func TestH2H1ReqPhaseReturn(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--mruby-file=" + testDir + "/req-return.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Fatalf("request should not be forwarded")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ReqPhaseReturn",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 404; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdtests := []struct {
|
|
|
|
k, v string
|
|
|
|
}{
|
2015-10-05 17:10:42 +02:00
|
|
|
{"content-length", "20"},
|
2015-09-03 19:52:19 +02:00
|
|
|
{"from", "mruby"},
|
|
|
|
}
|
|
|
|
for _, tt := range hdtests {
|
|
|
|
if got, want := res.header.Get(tt.k), tt.v; got != want {
|
|
|
|
t.Errorf("%v = %v; want %v", tt.k, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
if got, want := string(res.body), "Hello World from req"; got != want {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Errorf("body = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1RespPhaseSetHeader tests mruby response phase hook modifies
|
|
|
|
// response header fields.
|
|
|
|
func TestH2H1RespPhaseSetHeader(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--mruby-file=" + testDir + "/resp-set-header.rb"}, t, noopHandler)
|
2015-09-03 19:52:19 +02:00
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1RespPhaseSetHeader",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.header.Get("alpha"), "bravo"; got != want {
|
|
|
|
t.Errorf("alpha = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1RespPhaseReturn tests mruby response phase hook returns
|
|
|
|
// custom response.
|
|
|
|
func TestH2H1RespPhaseReturn(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--mruby-file=" + testDir + "/resp-return.rb"}, t, noopHandler)
|
2015-09-03 19:52:19 +02:00
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1RespPhaseReturn",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 404; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdtests := []struct {
|
|
|
|
k, v string
|
|
|
|
}{
|
2015-10-05 17:10:42 +02:00
|
|
|
{"content-length", "21"},
|
2015-09-03 19:52:19 +02:00
|
|
|
{"from", "mruby"},
|
|
|
|
}
|
|
|
|
for _, tt := range hdtests {
|
|
|
|
if got, want := res.header.Get(tt.k), tt.v; got != want {
|
|
|
|
t.Errorf("%v = %v; want %v", tt.k, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
if got, want := string(res.body), "Hello World from resp"; got != want {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Errorf("body = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:28:12 +02:00
|
|
|
// TestH2H1Upgrade tests HTTP Upgrade to HTTP/2
|
|
|
|
func TestH2H1Upgrade(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http1(requestParam{
|
|
|
|
name: "TestH2H1Upgrade",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("Connection", "Upgrade, HTTP2-Settings"),
|
2015-05-08 12:35:09 +02:00
|
|
|
pair("Upgrade", "h2c"),
|
2015-04-10 14:28:12 +02:00
|
|
|
pair("HTTP2-Settings", "AAMAAABkAAQAAP__"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http1() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 101; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = st.http2(requestParam{
|
|
|
|
httpUpgrade: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-03-08 11:31:43 +01:00
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:11:07 +02:00
|
|
|
// TestH2H1ProxyProtocolV1TCP4 tests PROXY protocol version 1
|
|
|
|
// containing TCP4 entry is accepted and X-Forwarded-For contains
|
|
|
|
// advertised src address.
|
|
|
|
func TestH2H1ProxyProtocolV1TCP4(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("X-Forwarded-For"), "192.168.0.2"; got != want {
|
|
|
|
t.Errorf("X-Forwarded-For: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP4 192.168.0.2 192.168.0.100 12345 8080\r\n"))
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1TCP4",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1TCP6 tests PROXY protocol version 1
|
|
|
|
// containing TCP6 entry is accepted and X-Forwarded-For contains
|
|
|
|
// advertised src address.
|
|
|
|
func TestH2H1ProxyProtocolV1TCP6(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("X-Forwarded-For"), "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; got != want {
|
|
|
|
t.Errorf("X-Forwarded-For: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 2001:0db8:85a3:0000:0000:8a2e:0370:7334 ::1 12345 8080\r\n"))
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1TCP6",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1Unknown tests PROXY protocol version 1
|
|
|
|
// containing UNKNOWN entry is accepted.
|
|
|
|
func TestH2H1ProxyProtocolV1Unknown(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, notWant := r.Header.Get("X-Forwarded-For"), "192.168.0.2"; got == notWant {
|
|
|
|
t.Errorf("X-Forwarded-For: %v")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY UNKNOWN 192.168.0.2 192.168.0.100 12345 8080\r\n"))
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1Unknown",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1JustUnknown tests PROXY protocol version 1
|
|
|
|
// containing only "PROXY UNKNOWN" is accepted.
|
|
|
|
func TestH2H1ProxyProtocolV1JustUnknown(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol", "--add-x-forwarded-for"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY UNKNOWN\r\n"))
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1JustUnknown",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 15:35:02 +02:00
|
|
|
// TestH2H1ProxyProtocolV1TooLongLine tests PROXY protocol version 1
|
|
|
|
// line longer than 107 bytes must be rejected
|
|
|
|
func TestH2H1ProxyProtocolV1TooLongLine(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol", "--add-x-forwarded-for"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY UNKNOWN ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535 655350\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1TooLongLine",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:11:07 +02:00
|
|
|
// TestH2H1ProxyProtocolV1BadLineEnd tests that PROXY protocol version
|
|
|
|
// 1 line ending without \r\n should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1BadLineEnd(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080\r \n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1BadLineEnd",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1NoEnd tests that PROXY protocol version 1
|
|
|
|
// line containing no \r\n should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1NoEnd(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 8080"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1NoEnd",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1EmbeddedNULL tests that PROXY protocol
|
|
|
|
// version 1 line containing NULL character should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1EmbeddedNULL(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
b := []byte("PROXY TCP6 ::1*foo ::1 12345 8080\r\n")
|
|
|
|
b[14] = 0
|
|
|
|
st.conn.Write(b)
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1EmbeddedNULL",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1MissingSrcPort tests that PROXY protocol
|
|
|
|
// version 1 line without src port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1MissingSrcPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1MissingSrcPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1MissingDstPort tests that PROXY protocol
|
|
|
|
// version 1 line without dst port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1MissingDstPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 \r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1MissingDstPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidSrcPort tests that PROXY protocol
|
|
|
|
// containing invalid src port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidSrcPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123x 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidSrcPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidDstPort tests that PROXY protocol
|
|
|
|
// containing invalid dst port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidDstPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 123456 80x\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidDstPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 15:35:02 +02:00
|
|
|
// TestH2H1ProxyProtocolV1LeadingZeroPort tests that PROXY protocol
|
|
|
|
// version 1 line with non zero port with leading zero should be
|
|
|
|
// rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1LeadingZeroPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 03000 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1LeadingZeroPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:11:07 +02:00
|
|
|
// TestH2H1ProxyProtocolV1TooLargeSrcPort tests that PROXY protocol
|
|
|
|
// containing too large src port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1TooLargeSrcPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 65536 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1TooLargeSrcPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1TooLargeDstPort tests that PROXY protocol
|
|
|
|
// containing too large dst port should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1TooLargeDstPort(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 ::1 12345 65536\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1TooLargeDstPort",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidSrcAddr tests that PROXY protocol
|
|
|
|
// containing invalid src addr should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidSrcAddr(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 192.168.0.1 ::1 12345 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidSrcAddr",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidDstAddr tests that PROXY protocol
|
|
|
|
// containing invalid dst addr should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidDstAddr(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY TCP6 ::1 192.168.0.1 12345 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidDstAddr",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidProtoFamily tests that PROXY protocol
|
|
|
|
// containing invalid protocol family should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidProtoFamily(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PROXY UNIX ::1 ::1 12345 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidProtoFamily",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H1ProxyProtocolV1InvalidID tests that PROXY protocol
|
|
|
|
// containing invalid PROXY protocol version 1 ID should be rejected.
|
|
|
|
func TestH2H1ProxyProtocolV1InvalidID(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--accept-proxy-protocol"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
st.conn.Write([]byte("PR0XY TCP6 ::1 ::1 12345 8080\r\n"))
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H1ProxyProtocolV1InvalidID",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("connection was not terminated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H1GracefulShutdown tests graceful shutdown.
|
2015-01-21 17:43:56 +01:00
|
|
|
func TestH2H1GracefulShutdown(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
fmt.Fprint(st.conn, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
|
|
|
|
if err := st.fr.WriteSettings(); err != nil {
|
|
|
|
t.Fatalf("st.fr.WriteSettings(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := []hpack.HeaderField{
|
|
|
|
pair(":method", "GET"),
|
|
|
|
pair(":scheme", "http"),
|
|
|
|
pair(":authority", st.authority),
|
|
|
|
pair(":path", "/"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range header {
|
|
|
|
_ = st.enc.WriteField(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.fr.WriteHeaders(http2.HeadersFrameParam{
|
|
|
|
StreamID: 1,
|
|
|
|
EndStream: false,
|
|
|
|
EndHeaders: true,
|
|
|
|
BlockFragment: st.headerBlkBuf.Bytes(),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("st.fr.WriteHeaders(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// send SIGQUIT signal to nghttpx to perform graceful shutdown
|
|
|
|
st.cmd.Process.Signal(syscall.SIGQUIT)
|
2015-09-23 17:11:14 +02:00
|
|
|
time.Sleep(150 * time.Millisecond)
|
2015-01-21 17:43:56 +01:00
|
|
|
|
|
|
|
// after signal, finish request body
|
|
|
|
if err := st.fr.WriteData(1, true, nil); err != nil {
|
|
|
|
t.Fatalf("st.fr.WriteData(): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
numGoAway := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
fr, err := st.readFrame()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
want := 2
|
|
|
|
if got := numGoAway; got != want {
|
|
|
|
t.Fatalf("numGoAway: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("st.readFrame(): %v", err)
|
|
|
|
}
|
|
|
|
switch f := fr.(type) {
|
|
|
|
case *http2.GoAwayFrame:
|
|
|
|
numGoAway += 1
|
|
|
|
want := http2.ErrCodeNo
|
|
|
|
if got := f.ErrCode; got != want {
|
|
|
|
t.Fatalf("f.ErrCode(%v): %v; want %v", numGoAway, got, want)
|
|
|
|
}
|
|
|
|
switch numGoAway {
|
|
|
|
case 1:
|
|
|
|
want := (uint32(1) << 31) - 1
|
|
|
|
if got := f.LastStreamID; got != want {
|
|
|
|
t.Fatalf("f.LastStreamID(%v): %v; want %v", numGoAway, got, want)
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
want := uint32(1)
|
|
|
|
if got := f.LastStreamID; got != want {
|
|
|
|
t.Fatalf("f.LastStreamID(%v): %v; want %v", numGoAway, got, want)
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
t.Fatalf("too many GOAWAYs received")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H2MultipleResponseCL tests that server returns error if
|
|
|
|
// multiple Content-Length response header fields are received.
|
2015-01-20 14:56:36 +01:00
|
|
|
func TestH2H2MultipleResponseCL(t *testing.T) {
|
2015-01-19 16:45:51 +01:00
|
|
|
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("content-length", "1")
|
2015-01-23 16:07:28 +01:00
|
|
|
w.Header().Add("content-length", "1")
|
2015-01-19 16:45:51 +01:00
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-20 14:56:36 +01:00
|
|
|
name: "TestH2H2MultipleResponseCL",
|
2015-01-19 16:45:51 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-19 16:45:51 +01:00
|
|
|
}
|
2015-02-19 16:01:15 +01:00
|
|
|
if got, want := res.errCode, http2.ErrCodeInternal; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
2015-01-19 16:45:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H2InvalidResponseCL tests that server returns error if
|
|
|
|
// Content-Length response header field value cannot be parsed as a
|
|
|
|
// number.
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H2InvalidResponseCL(t *testing.T) {
|
2015-01-19 16:45:51 +01:00
|
|
|
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("content-length", "")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H2InvalidResponseCL",
|
2015-01-19 16:45:51 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-01-20 14:59:09 +01:00
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
2015-01-19 16:45:51 +01:00
|
|
|
}
|
2015-02-19 16:01:15 +01:00
|
|
|
if got, want := res.errCode, http2.ErrCodeInternal; got != want {
|
|
|
|
t.Errorf("res.errCode: %v; want %v", got, want)
|
2015-01-19 16:45:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 17:03:56 +01:00
|
|
|
|
2015-01-23 16:01:14 +01:00
|
|
|
// TestH2H2ConnectFailure tests that server handles the situation that
|
|
|
|
// connection attempt to HTTP/2 backend failed.
|
2015-01-21 15:30:48 +01:00
|
|
|
func TestH2H2ConnectFailure(t *testing.T) {
|
|
|
|
st := newServerTester([]string{"--http2-bridge"}, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
// simulate backend connect attempt failure
|
|
|
|
st.ts.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2ConnectFailure",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
want := 503
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 17:15:56 +01:00
|
|
|
|
|
|
|
// TestH2H2HostRewrite tests that server rewrites host header field
|
|
|
|
func TestH2H2HostRewrite(t *testing.T) {
|
2015-08-08 18:23:58 +02:00
|
|
|
st := newServerTester([]string{"--http2-bridge", "--host-rewrite"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-03 17:15:56 +01:00
|
|
|
w.Header().Add("request-host", r.Host)
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2HostRewrite",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("request-host"), st.backendHost; got != want {
|
|
|
|
t.Errorf("request-host: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 17:41:20 +01:00
|
|
|
|
|
|
|
// TestH2H2NoHostRewrite tests that server does not rewrite host
|
|
|
|
// header field
|
|
|
|
func TestH2H2NoHostRewrite(t *testing.T) {
|
2015-08-08 18:23:58 +02:00
|
|
|
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-02-03 17:41:20 +01:00
|
|
|
w.Header().Add("request-host", r.Host)
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2NoHostRewrite",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := res.header.Get("request-host"), st.frontendHost; got != want {
|
|
|
|
t.Errorf("request-host: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 14:31:47 +02:00
|
|
|
|
|
|
|
// TestH2H2TLSXfp tests nghttpx sends x-forwarded-proto header field
|
|
|
|
// with http value since :scheme is http, even if the frontend
|
|
|
|
// connection is encrypted.
|
|
|
|
func TestH2H2TLSXfp(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, want := r.Header.Get("x-forwarded-proto"), "http"; got != want {
|
|
|
|
t.Errorf("x-forwarded-proto: want %v; got %v", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2TLSXfp",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("res.status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 19:52:19 +02:00
|
|
|
|
2016-01-16 04:25:12 +01:00
|
|
|
// TestH2H2AddXff tests that server generates X-Forwarded-For header
|
|
|
|
// field when forwarding request to backend.
|
|
|
|
func TestH2H2AddXff(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2AddXff",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2AddXff2 tests that server appends X-Forwarded-For header
|
|
|
|
// field to existing one when forwarding request to backend.
|
|
|
|
func TestH2H2AddXff2(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "host, 127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2AddXff2",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2StripXff tests that --strip-incoming-x-forwarded-for
|
|
|
|
// option.
|
|
|
|
func TestH2H2StripXff(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--strip-incoming-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if xff, found := r.Header["X-Forwarded-For"]; found {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want nothing", xff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2StripXff",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2StripAddXff tests that --strip-incoming-x-forwarded-for and
|
|
|
|
// --add-x-forwarded-for options.
|
|
|
|
func TestH2H2StripAddXff(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--strip-incoming-x-forwarded-for", "--add-x-forwarded-for"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
xff := r.Header.Get("X-Forwarded-For")
|
|
|
|
want := "127.0.0.1"
|
|
|
|
if xff != want {
|
|
|
|
t.Errorf("X-Forwarded-For = %v; want %v", xff, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2StripAddXff",
|
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("x-forwarded-for", "host"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:59:14 +01:00
|
|
|
// TestH2H2AddForwarded tests that server generates Forwarded header
|
2016-01-16 04:19:19 +01:00
|
|
|
// field using static obfuscated "by" and "for" parameter.
|
2016-01-15 16:59:14 +01:00
|
|
|
func TestH2H2AddForwarded(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--add-forwarded=by,for,host,proto", "--forwarded-by=_alpha", "--forwarded-for=_bravo"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
want := fmt.Sprintf(`by=_alpha;for=_bravo;host="127.0.0.1:%v";proto=https`, serverPort)
|
2016-01-15 16:59:14 +01:00
|
|
|
if got := r.Header.Get("Forwarded"); got != want {
|
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2AddForwarded",
|
|
|
|
scheme: "https",
|
2016-01-16 04:19:19 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2AddForwardedMerge tests that server generates Forwarded
|
|
|
|
// header field using static obfuscated "by" and "for" parameter, and
|
|
|
|
// existing Forwarded header field.
|
|
|
|
func TestH2H2AddForwardedMerge(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--add-forwarded=by,for,host,proto", "--forwarded-by=_alpha", "--forwarded-for=_bravo"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
want := fmt.Sprintf(`host=foo, by=_alpha;for=_bravo;host="127.0.0.1:%v";proto=https`, serverPort)
|
2016-01-16 04:19:19 +01:00
|
|
|
if got := r.Header.Get("Forwarded"); got != want {
|
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2AddForwardedMerge",
|
|
|
|
scheme: "https",
|
2016-01-15 16:59:14 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2AddForwardedStrip tests that server generates Forwarded
|
|
|
|
// header field using static obfuscated "by" and "for" parameter, and
|
|
|
|
// existing Forwarded header field stripped.
|
|
|
|
func TestH2H2AddForwardedStrip(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--strip-incoming-forwarded", "--add-forwarded=by,for,host,proto", "--forwarded-by=_alpha", "--forwarded-for=_bravo"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2016-01-19 14:27:09 +01:00
|
|
|
want := fmt.Sprintf(`by=_alpha;for=_bravo;host="127.0.0.1:%v";proto=https`, serverPort)
|
2016-01-15 16:59:14 +01:00
|
|
|
if got := r.Header.Get("Forwarded"); got != want {
|
|
|
|
t.Errorf("Forwarded = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2AddForwardedStrip",
|
|
|
|
scheme: "https",
|
2016-01-16 04:19:19 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2StripForwarded tests that server strips incoming Forwarded
|
|
|
|
// header field.
|
|
|
|
func TestH2H2StripForwarded(t *testing.T) {
|
|
|
|
st := newServerTesterTLS([]string{"--http2-bridge", "--strip-incoming-forwarded"}, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if got, found := r.Header["Forwarded"]; found {
|
|
|
|
t.Errorf("Forwarded = %v; want nothing", got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2016-01-16 04:25:12 +01:00
|
|
|
name: "TestH2H2StripForwarded",
|
2016-01-16 04:19:19 +01:00
|
|
|
scheme: "https",
|
2016-01-15 16:59:14 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("forwarded", "host=foo"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
if got, want := res.status, 200; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 19:52:19 +02:00
|
|
|
// TestH2H2ReqPhaseReturn tests mruby request phase hook returns
|
|
|
|
// custom response.
|
|
|
|
func TestH2H2ReqPhaseReturn(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--http2-bridge", "--mruby-file=" + testDir + "/req-return.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Fatalf("request should not be forwarded")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2ReqPhaseReturn",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 404; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdtests := []struct {
|
|
|
|
k, v string
|
|
|
|
}{
|
2015-10-05 17:10:42 +02:00
|
|
|
{"content-length", "20"},
|
2015-09-03 19:52:19 +02:00
|
|
|
{"from", "mruby"},
|
|
|
|
}
|
|
|
|
for _, tt := range hdtests {
|
|
|
|
if got, want := res.header.Get(tt.k), tt.v; got != want {
|
|
|
|
t.Errorf("%v = %v; want %v", tt.k, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
if got, want := string(res.body), "Hello World from req"; got != want {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Errorf("body = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestH2H2RespPhaseReturn tests mruby response phase hook returns
|
|
|
|
// custom response.
|
|
|
|
func TestH2H2RespPhaseReturn(t *testing.T) {
|
2015-10-05 17:10:42 +02:00
|
|
|
st := newServerTester([]string{"--http2-bridge", "--mruby-file=" + testDir + "/resp-return.rb"}, t, noopHandler)
|
2015-09-03 19:52:19 +02:00
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
|
|
|
name: "TestH2H2RespPhaseReturn",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := res.status, 404; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdtests := []struct {
|
|
|
|
k, v string
|
|
|
|
}{
|
2015-10-05 17:10:42 +02:00
|
|
|
{"content-length", "21"},
|
2015-09-03 19:52:19 +02:00
|
|
|
{"from", "mruby"},
|
|
|
|
}
|
|
|
|
for _, tt := range hdtests {
|
|
|
|
if got, want := res.header.Get(tt.k), tt.v; got != want {
|
|
|
|
t.Errorf("%v = %v; want %v", tt.k, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
if got, want := string(res.body), "Hello World from resp"; got != want {
|
2015-09-03 19:52:19 +02:00
|
|
|
t.Errorf("body = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|