Refactor serverTester constructor

This commit is contained in:
Tatsuhiro Tsujikawa 2022-05-24 21:47:24 +09:00
parent 0287024e85
commit c67dc7ff1d
3 changed files with 1355 additions and 689 deletions

View File

@ -18,7 +18,7 @@ import (
// TestH1H1PlainGET tests whether simple HTTP/1 GET request works.
func TestH1H1PlainGET(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
st := newServerTester(t, options{})
defer st.Close()
res, err := st.http1(requestParam{
@ -37,7 +37,7 @@ func TestH1H1PlainGET(t *testing.T) {
// TestH1H1PlainGETClose tests whether simple HTTP/1 GET request with
// Connection: close request header field works.
func TestH1H1PlainGETClose(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
st := newServerTester(t, options{})
defer st.Close()
res, err := st.http1(requestParam{
@ -59,9 +59,12 @@ func TestH1H1PlainGETClose(t *testing.T) {
// TestH1H1InvalidMethod tests that server rejects invalid method with
// 501 status code
func TestH1H1InvalidMethod(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -80,9 +83,12 @@ func TestH1H1InvalidMethod(t *testing.T) {
// TestH1H1MultipleRequestCL tests that server rejects request which
// contains multiple Content-Length header fields.
func TestH1H1MultipleRequestCL(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward bad request")
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward bad request")
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, fmt.Sprintf(`GET / HTTP/1.1
@ -109,7 +115,7 @@ Content-Length: 0
// // TestH1H1ConnectFailure tests that server handles the situation that
// // connection attempt to HTTP/1 backend failed.
// func TestH1H1ConnectFailure(t *testing.T) {
// st := newServerTester(nil, t, noopHandler)
// st := newServerTester(t, options{})
// defer st.Close()
// // shutdown backend server to simulate backend connect failure
@ -130,7 +136,10 @@ Content-Length: 0
// TestH1H1AffinityCookie tests that affinity cookie is sent back in
// cleartext http.
func TestH1H1AffinityCookie(t *testing.T) {
st := newServerTester([]string{"--affinity-cookie"}, t, noopHandler)
opts := options{
args: []string{"--affinity-cookie"},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -154,7 +163,11 @@ func TestH1H1AffinityCookie(t *testing.T) {
// TestH1H1AffinityCookieTLS tests that affinity cookie is sent back
// in https.
func TestH1H1AffinityCookieTLS(t *testing.T) {
st := newServerTesterTLS([]string{"--alpn-h1", "--affinity-cookie"}, t, noopHandler)
opts := options{
args: []string{"--alpn-h1", "--affinity-cookie"},
tls: true,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -177,7 +190,7 @@ func TestH1H1AffinityCookieTLS(t *testing.T) {
// TestH1H1GracefulShutdown tests graceful shutdown.
func TestH1H1GracefulShutdown(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
st := newServerTester(t, options{})
defer st.Close()
res, err := st.http1(requestParam{
@ -218,9 +231,13 @@ func TestH1H1GracefulShutdown(t *testing.T) {
// TestH1H1HostRewrite tests that server rewrites Host header field
func TestH1H1HostRewrite(t *testing.T) {
st := newServerTester([]string{"--host-rewrite"}, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
})
opts := options{
args: []string{"--host-rewrite"},
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -240,9 +257,12 @@ func TestH1H1HostRewrite(t *testing.T) {
// TestH1H1BadHost tests that server rejects request including bad
// characters in host header field.
func TestH1H1BadHost(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET / HTTP/1.1\r\nTest-Case: TestH1H1HBadHost\r\nHost: foo\"bar\r\n\r\n"); err != nil {
@ -260,9 +280,12 @@ func TestH1H1BadHost(t *testing.T) {
// TestH1H1BadAuthority tests that server rejects request including
// bad characters in authority component of requset URI.
func TestH1H1BadAuthority(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET http://foo\"bar/ HTTP/1.1\r\nTest-Case: TestH1H1HBadAuthority\r\nHost: foobar\r\n\r\n"); err != nil {
@ -280,9 +303,12 @@ func TestH1H1BadAuthority(t *testing.T) {
// TestH1H1BadScheme tests that server rejects request including
// bad characters in scheme component of requset URI.
func TestH1H1BadScheme(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward this request")
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET http*://example.com/ HTTP/1.1\r\nTest-Case: TestH1H1HBadScheme\r\nHost: example.com\r\n\r\n"); err != nil {
@ -300,9 +326,12 @@ func TestH1H1BadScheme(t *testing.T) {
// TestH1H1HTTP10 tests that server can accept HTTP/1.0 request
// without Host header field
func TestH1H1HTTP10(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET / HTTP/1.0\r\nTest-Case: TestH1H1HTTP10\r\n\r\n"); err != nil {
@ -326,9 +355,12 @@ func TestH1H1HTTP10(t *testing.T) {
// field using actual backend server even if --no-http-rewrite is
// used.
func TestH1H1HTTP10NoHostRewrite(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET / HTTP/1.0\r\nTest-Case: TestH1H1HTTP10NoHostRewrite\r\n\r\n"); err != nil {
@ -351,21 +383,24 @@ func TestH1H1HTTP10NoHostRewrite(t *testing.T) {
// TestH1H1RequestTrailer tests request trailer part is forwarded to
// backend.
func TestH1H1RequestTrailer(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
opts := options{
handler: 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 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)
}
}
if got, want := r.Trailer.Get("foo"), "bar"; got != want {
t.Errorf("r.Trailer.Get(foo): %v; want %v", got, want)
}
})
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -389,9 +424,13 @@ func TestH1H1HeaderFieldBufferPath(t *testing.T) {
// The value 100 is chosen so that sum of header fields bytes
// does not exceed it. We use > 100 bytes URI to exceed this
// limit.
st := newServerTester([]string{"--request-header-field-buffer=100"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
})
opts := options{
args: []string{"--request-header-field-buffer=100"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -409,9 +448,13 @@ func TestH1H1HeaderFieldBufferPath(t *testing.T) {
// TestH1H1HeaderFieldBuffer tests that request with header fields
// larger than configured buffer size is rejected.
func TestH1H1HeaderFieldBuffer(t *testing.T) {
st := newServerTester([]string{"--request-header-field-buffer=10"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
})
opts := options{
args: []string{"--request-header-field-buffer=10"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -428,9 +471,13 @@ func TestH1H1HeaderFieldBuffer(t *testing.T) {
// TestH1H1HeaderFields tests that request with header fields more
// than configured number is rejected.
func TestH1H1HeaderFields(t *testing.T) {
st := newServerTester([]string{"--max-request-header-fields=1"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
})
opts := options{
args: []string{"--max-request-header-fields=1"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatal("execution path should not be here")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -451,9 +498,12 @@ func TestH1H1HeaderFields(t *testing.T) {
// TestH1H1Websocket tests that HTTP Upgrade to WebSocket works.
func TestH1H1Websocket(t *testing.T) {
st := newServerTesterHandler(nil, t, websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws)
}))
opts := options{
handler: websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws)
}).ServeHTTP,
}
st := newServerTester(t, opts)
defer st.Close()
content := []byte("hello world")
@ -472,11 +522,15 @@ func TestH1H1Websocket(t *testing.T) {
// TestH1H1ReqPhaseSetHeader tests mruby request phase hook
// modifies request header fields.
func TestH1H1ReqPhaseSetHeader(t *testing.T) {
st := newServerTester([]string{"--mruby-file=" + testDir + "/req-set-header.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("User-Agent"), "mruby"; got != want {
t.Errorf("User-Agent = %v; want %v", got, want)
}
})
opts := options{
args: []string{"--mruby-file=" + testDir + "/req-set-header.rb"},
handler: func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("User-Agent"), "mruby"; got != want {
t.Errorf("User-Agent = %v; want %v", got, want)
}
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -494,9 +548,13 @@ func TestH1H1ReqPhaseSetHeader(t *testing.T) {
// TestH1H1ReqPhaseReturn tests mruby request phase hook returns
// custom response.
func TestH1H1ReqPhaseReturn(t *testing.T) {
st := newServerTester([]string{"--mruby-file=" + testDir + "/req-return.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
})
opts := options{
args: []string{"--mruby-file=" + testDir + "/req-return.rb"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -530,7 +588,10 @@ func TestH1H1ReqPhaseReturn(t *testing.T) {
// TestH1H1RespPhaseSetHeader tests mruby response phase hook modifies
// response header fields.
func TestH1H1RespPhaseSetHeader(t *testing.T) {
st := newServerTester([]string{"--mruby-file=" + testDir + "/resp-set-header.rb"}, t, noopHandler)
opts := options{
args: []string{"--mruby-file=" + testDir + "/resp-set-header.rb"},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -552,7 +613,10 @@ func TestH1H1RespPhaseSetHeader(t *testing.T) {
// TestH1H1RespPhaseReturn tests mruby response phase hook returns
// custom response.
func TestH1H1RespPhaseReturn(t *testing.T) {
st := newServerTester([]string{"--mruby-file=" + testDir + "/resp-return.rb"}, t, noopHandler)
opts := options{
args: []string{"--mruby-file=" + testDir + "/resp-return.rb"},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -586,7 +650,10 @@ func TestH1H1RespPhaseReturn(t *testing.T) {
// TestH1H1HTTPSRedirect tests that the request to the backend which
// requires TLS is redirected to https URI.
func TestH1H1HTTPSRedirect(t *testing.T) {
st := newServerTester([]string{"--redirect-if-not-tls"}, t, noopHandler)
opts := options{
args: []string{"--redirect-if-not-tls"},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -607,7 +674,13 @@ func TestH1H1HTTPSRedirect(t *testing.T) {
// TestH1H1HTTPSRedirectPort tests that the request to the backend
// which requires TLS is redirected to https URI with given port.
func TestH1H1HTTPSRedirectPort(t *testing.T) {
st := newServerTester([]string{"--redirect-if-not-tls", "--redirect-https-port=8443"}, t, noopHandler)
opts := options{
args: []string{
"--redirect-if-not-tls",
"--redirect-https-port=8443",
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -629,7 +702,7 @@ func TestH1H1HTTPSRedirectPort(t *testing.T) {
// TestH1H1POSTRequests tests that server can handle 2 requests with
// request body.
func TestH1H1POSTRequests(t *testing.T) {
st := newServerTester(nil, t, noopHandler)
st := newServerTester(t, options{})
defer st.Close()
res, err := st.http1(requestParam{
@ -658,7 +731,10 @@ func TestH1H1POSTRequests(t *testing.T) {
// // TestH1H2ConnectFailure tests that server handles the situation that
// // connection attempt to HTTP/2 backend failed.
// func TestH1H2ConnectFailure(t *testing.T) {
// st := newServerTester([]string{"--http2-bridge"}, t, noopHandler)
// opts := options{
// args: []string{"--http2-bridge"},
// }
// st := newServerTester(t, opts)
// defer st.Close()
// // simulate backend connect attempt failure
@ -679,9 +755,13 @@ func TestH1H1POSTRequests(t *testing.T) {
// TestH1H2NoHost tests that server rejects request without Host
// header field for HTTP/2 backend.
func TestH1H2NoHost(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward bad request")
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Errorf("server should not forward bad request")
},
}
st := newServerTester(t, opts)
defer st.Close()
// without Host header field, we expect 400 response
@ -703,9 +783,13 @@ func TestH1H2NoHost(t *testing.T) {
// TestH1H2HTTP10 tests that server can accept HTTP/1.0 request
// without Host header field
func TestH1H2HTTP10(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET / HTTP/1.0\r\nTest-Case: TestH1H2HTTP10\r\n\r\n"); err != nil {
@ -729,9 +813,13 @@ func TestH1H2HTTP10(t *testing.T) {
// field using actual backend server even if --no-http-rewrite is
// used.
func TestH1H2HTTP10NoHostRewrite(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("request-host", r.Host)
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, "GET / HTTP/1.0\r\nTest-Case: TestH1H2HTTP10NoHostRewrite\r\n\r\n"); err != nil {
@ -756,11 +844,15 @@ func TestH1H2HTTP10NoHostRewrite(t *testing.T) {
// concatenates crumbled Cookies automatically, so this test is not
// much effective now.
func TestH1H2CrumbleCookie(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, 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)
}
})
opts := options{
args: []string{"--http2-bridge"},
handler: 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)
}
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -780,11 +872,15 @@ func TestH1H2CrumbleCookie(t *testing.T) {
// TestH1H2GenerateVia tests that server generates Via header field to and
// from backend server.
func TestH1H2GenerateVia(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("Via"), "1.1 nghttpx"; got != want {
t.Errorf("Via: %v; want %v", got, want)
}
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("Via"), "1.1 nghttpx"; got != want {
t.Errorf("Via: %v; want %v", got, want)
}
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -801,12 +897,16 @@ func TestH1H2GenerateVia(t *testing.T) {
// TestH1H2AppendVia tests that server adds value to existing Via
// header field to and from backend server.
func TestH1H2AppendVia(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("Via"), "foo, 1.1 nghttpx"; got != want {
t.Errorf("Via: %v; want %v", got, want)
}
w.Header().Add("Via", "bar")
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("Via"), "foo, 1.1 nghttpx"; got != want {
t.Errorf("Via: %v; want %v", got, want)
}
w.Header().Add("Via", "bar")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -826,12 +926,16 @@ func TestH1H2AppendVia(t *testing.T) {
// TestH1H2NoVia tests that server does not add value to existing Via
// header field to and from backend server.
func TestH1H2NoVia(t *testing.T) {
st := newServerTester([]string{"--http2-bridge", "--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")
})
opts := options{
args: []string{"--http2-bridge", "--no-via"},
handler: 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")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -851,9 +955,16 @@ func TestH1H2NoVia(t *testing.T) {
// TestH1H2ReqPhaseReturn tests mruby request phase hook returns
// custom response.
func TestH1H2ReqPhaseReturn(t *testing.T) {
st := newServerTester([]string{"--http2-bridge", "--mruby-file=" + testDir + "/req-return.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
})
opts := options{
args: []string{
"--http2-bridge",
"--mruby-file=" + testDir + "/req-return.rb",
},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -887,7 +998,13 @@ func TestH1H2ReqPhaseReturn(t *testing.T) {
// TestH1H2RespPhaseReturn tests mruby response phase hook returns
// custom response.
func TestH1H2RespPhaseReturn(t *testing.T) {
st := newServerTester([]string{"--http2-bridge", "--mruby-file=" + testDir + "/resp-return.rb"}, t, noopHandler)
opts := options{
args: []string{
"--http2-bridge",
"--mruby-file=" + testDir + "/resp-return.rb",
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -921,11 +1038,15 @@ func TestH1H2RespPhaseReturn(t *testing.T) {
// TestH1H2TE tests that "te: trailers" header is forwarded to HTTP/2
// backend server by stripping other encodings.
func TestH1H2TE(t *testing.T) {
st := newServerTester([]string{"--http2-bridge"}, t, func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("te"), "trailers"; got != want {
t.Errorf("te: %v; want %v", got, want)
}
})
opts := options{
args: []string{"--http2-bridge"},
handler: func(w http.ResponseWriter, r *http.Request) {
if got, want := r.Header.Get("te"), "trailers"; got != want {
t.Errorf("te: %v; want %v", got, want)
}
},
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -945,9 +1066,14 @@ func TestH1H2TE(t *testing.T) {
// TestH1APIBackendconfig exercise backendconfig API endpoint routine
// for successful case.
func TestH1APIBackendconfig(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3010)
opts := options{
args: []string{"-f127.0.0.1,3010;api;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3010,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -982,9 +1108,14 @@ backend=127.0.0.1,3011
// TestH1APIBackendconfigQuery exercise backendconfig API endpoint
// routine with query.
func TestH1APIBackendconfigQuery(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3010)
opts := options{
args: []string{"-f127.0.0.1,3010;api;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3010,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -1019,9 +1150,14 @@ backend=127.0.0.1,3011
// TestH1APIBackendconfigBadMethod exercise backendconfig API endpoint
// routine with bad method.
func TestH1APIBackendconfigBadMethod(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3010)
opts := options{
args: []string{"-f127.0.0.1,3010;api;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3010,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -1055,9 +1191,14 @@ backend=127.0.0.1,3011
// TestH1APIConfigrevision tests configrevision API.
func TestH1APIConfigrevision(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3010)
opts := options{
args: []string{"-f127.0.0.1,3010;api;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3010,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -1093,9 +1234,14 @@ func TestH1APIConfigrevision(t *testing.T) {
// TestH1APINotFound exercise backendconfig API endpoint routine when
// API endpoint is not found.
func TestH1APINotFound(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3010;api;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3010)
opts := options{
args: []string{"-f127.0.0.1,3010;api;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3010,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -1129,9 +1275,14 @@ backend=127.0.0.1,3011
// TestH1Healthmon tests health monitor endpoint.
func TestH1Healthmon(t *testing.T) {
st := newServerTesterConnectPort([]string{"-f127.0.0.1,3011;healthmon;no-tls"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
}, 3011)
opts := options{
args: []string{"-f127.0.0.1,3011;healthmon;no-tls"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatalf("request should not be forwarded")
},
connectPort: 3011,
}
st := newServerTester(t, opts)
defer st.Close()
res, err := st.http1(requestParam{
@ -1149,9 +1300,13 @@ func TestH1Healthmon(t *testing.T) {
// TestH1ResponseBeforeRequestEnd tests the situation where response
// ends before request body finishes.
func TestH1ResponseBeforeRequestEnd(t *testing.T) {
st := newServerTester([]string{"--mruby-file=" + testDir + "/req-return.rb"}, t, func(w http.ResponseWriter, r *http.Request) {
t.Fatal("request should not be forwarded")
})
opts := options{
args: []string{"--mruby-file=" + testDir + "/req-return.rb"},
handler: func(w http.ResponseWriter, r *http.Request) {
t.Fatal("request should not be forwarded")
},
}
st := newServerTester(t, opts)
defer st.Close()
if _, err := io.WriteString(st.conn, fmt.Sprintf(`POST / HTTP/1.1
@ -1176,21 +1331,24 @@ Content-Length: 1000000
// TestH1H1ChunkedEndsPrematurely tests that an HTTP/1.1 request fails
// if the backend chunked encoded response ends prematurely.
func TestH1H1ChunkedEndsPrematurely(t *testing.T) {
st := newServerTester(nil, t, func(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Could not hijack the connection", http.StatusInternalServerError)
return
}
conn, bufrw, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n")
bufrw.Flush()
})
opts := options{
handler: func(w http.ResponseWriter, r *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Could not hijack the connection", http.StatusInternalServerError)
return
}
conn, bufrw, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
bufrw.WriteString("HTTP/1.1 200\r\nTransfer-Encoding: chunked\r\n\r\n")
bufrw.Flush()
},
}
st := newServerTester(t, opts)
defer st.Close()
_, err := st.http1(requestParam{

File diff suppressed because it is too large Load Diff

View File

@ -61,46 +61,37 @@ type serverTester struct {
errCh chan error
}
// newServerTester creates test context for plain TCP frontend
// connection.
func newServerTester(args []string, t *testing.T, handler http.HandlerFunc) *serverTester {
return newServerTesterInternal(args, t, handler, false, serverPort, nil)
type options struct {
// args is the additional arguments to nghttpx.
args []string
// handler is the handler to handle the request. It defaults
// to noopHandler.
handler http.HandlerFunc
// connectPort is the server side port where client connection
// is made. It defaults to serverPort.
connectPort int
// tls, if set to true, sets up TLS frontend connection.
tls bool
// tlsConfig is the client side TLS configuration that is used
// when tls is true.
tlsConfig *tls.Config
}
func newServerTesterConnectPort(args []string, t *testing.T, handler http.HandlerFunc, port int) *serverTester {
return newServerTesterInternal(args, t, handler, false, port, nil)
}
// newServerTester creates test context.
func newServerTester(t *testing.T, opts options) *serverTester {
if opts.handler == nil {
opts.handler = noopHandler
}
if opts.connectPort == 0 {
opts.connectPort = serverPort
}
func newServerTesterHandler(args []string, t *testing.T, handler http.Handler) *serverTester {
return newServerTesterInternal(args, t, handler, false, serverPort, nil)
}
// newServerTester creates test context for TLS frontend connection.
func newServerTesterTLS(args []string, t *testing.T, handler http.HandlerFunc) *serverTester {
return newServerTesterInternal(args, t, handler, true, serverPort, nil)
}
func newServerTesterTLSConnectPort(args []string, t *testing.T, handler http.HandlerFunc, port int) *serverTester {
return newServerTesterInternal(args, t, handler, true, port, nil)
}
// newServerTester creates test context for TLS frontend connection
// with given clientConfig
func newServerTesterTLSConfig(args []string, t *testing.T, handler http.HandlerFunc, clientConfig *tls.Config) *serverTester {
return newServerTesterInternal(args, t, handler, true, serverPort, clientConfig)
}
// newServerTesterInternal creates test context. If frontendTLS is
// true, set up TLS frontend connection. connectPort is the server
// side port where client connection is made.
func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handler, frontendTLS bool, connectPort int, clientConfig *tls.Config) *serverTester {
ts := httptest.NewUnstartedServer(handler)
args := []string{}
ts := httptest.NewUnstartedServer(opts.handler)
var args []string
var backendTLS, dns, externalDNS, acceptProxyProtocol, redirectIfNotTLS, affinityCookie, alpnH1 bool
for _, k := range src_args {
for _, k := range opts.args {
switch k {
case "--http2-bridge":
backendTLS = true
@ -134,7 +125,7 @@ func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handl
ts.Start()
}
scheme := "http"
if frontendTLS {
if opts.tls {
scheme = "https"
args = append(args, testDir+"/server.key", testDir+"/server.crt")
}
@ -174,7 +165,7 @@ func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handl
}
noTLS := ";no-tls"
if frontendTLS {
if opts.tls {
noTLS = ""
}
@ -186,7 +177,7 @@ func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handl
args = append(args, fmt.Sprintf("-f127.0.0.1,%v%v%v", serverPort, noTLS, proxyProto), b,
"--errorlog-file="+logDir+"/log.txt", "-LINFO")
authority := fmt.Sprintf("127.0.0.1:%v", connectPort)
authority := fmt.Sprintf("127.0.0.1:%v", opts.connectPort)
st := &serverTester{
cmd: exec.Command(serverBin, args...),
@ -214,12 +205,12 @@ func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handl
var conn net.Conn
var err error
if frontendTLS {
if opts.tls {
var tlsConfig *tls.Config
if clientConfig == nil {
if opts.tlsConfig == nil {
tlsConfig = new(tls.Config)
} else {
tlsConfig = clientConfig
tlsConfig = opts.tlsConfig.Clone()
}
tlsConfig.InsecureSkipVerify = true
if alpnH1 {
@ -239,7 +230,7 @@ func newServerTesterInternal(src_args []string, t *testing.T, handler http.Handl
}
continue
}
if frontendTLS {
if opts.tls {
tlsConn := conn.(*tls.Conn)
cs := tlsConn.ConnectionState()
if !cs.NegotiatedProtocolIsMutual {