2015-01-18 15:50:20 +01:00
|
|
|
package nghttp2
|
|
|
|
|
|
|
|
import (
|
2015-01-20 14:55:01 +01:00
|
|
|
"bufio"
|
2015-01-18 15:50:20 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/bradfitz/http2"
|
2015-01-19 13:24:18 +01:00
|
|
|
"github.com/bradfitz/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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-20 14:19:28 +01:00
|
|
|
func TestH1H1PlainGET(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http1(requestParam{
|
|
|
|
name: "TestH1H1PlainGET",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error st.http1() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := 200
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestH1H1PlainGETClose(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, noopHandler)
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http1(requestParam{
|
2015-01-20 14:25:56 +01:00
|
|
|
name: "TestH1H1PlainGETClose",
|
2015-01-20 14:19:28 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("Connection", "close"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error st.http1() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := 200
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status = %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 14:55:01 +01:00
|
|
|
func TestH1H1DuplicateRequestCL(t *testing.T) {
|
|
|
|
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t.Errorf("server should not forward bad request")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
if _, err := io.WriteString(st.conn, fmt.Sprintf("GET / HTTP/1.1\r\nHost: %v\r\nTest-Case: TestH1H1DuplicateRequestCL\r\nContent-Length: 0\r\nContent-Length: 1\r\n\r\n", st.authority)); err != nil {
|
|
|
|
t.Fatalf("Error io.WriteString() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(st.conn), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error http.ReadResponse() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := 400
|
|
|
|
if got := resp.StatusCode; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := 200
|
|
|
|
if res.status != want {
|
|
|
|
t.Errorf("status = %v; want %v", res.status, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
_, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H1StripXff1",
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := http2.ErrCodeProtocol
|
|
|
|
if res.errCode != want {
|
|
|
|
t.Errorf("res.errCode = %v; want %v", res.errCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := http2.ErrCodeProtocol
|
|
|
|
if res.errCode != want {
|
|
|
|
t.Errorf("res.errCode = %v; want %v", res.errCode, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 {
|
|
|
|
t.Errorf("Error reading r.body: %v", err)
|
|
|
|
}
|
|
|
|
want = "foo"
|
|
|
|
if got := string(body); got != want {
|
|
|
|
t.Errorf("body: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
_, 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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 14:40:37 +01:00
|
|
|
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H1DuplicateRequestCL(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: "TestH2H1DuplicateRequestCL",
|
2015-01-19 14:40:37 +01:00
|
|
|
header: []hpack.HeaderField{
|
|
|
|
pair("content-length", "1"),
|
|
|
|
pair("content-length", "2"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
want := 400
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
want := 400
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 16:45:51 +01:00
|
|
|
|
2015-01-19 16:59:09 +01:00
|
|
|
func TestH2H2DuplicateResponseCL(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")
|
|
|
|
w.Header().Add("content-length", "2")
|
|
|
|
})
|
|
|
|
defer st.Close()
|
|
|
|
|
|
|
|
res, err := st.http2(requestParam{
|
2015-01-19 16:59:09 +01:00
|
|
|
name: "TestH2H2DuplicateResponseCL",
|
2015-01-19 16:45:51 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
want := 502
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Errorf("Error st.http2() = %v", err)
|
|
|
|
}
|
|
|
|
want := 502
|
|
|
|
if got := res.status; got != want {
|
|
|
|
t.Errorf("status: %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|