integration: Add WebSocket upgrade test
This commit is contained in:
parent
323fc8c552
commit
59e6272ba4
|
@ -191,6 +191,7 @@ its testing framework. We depend on the following libraries:
|
|||
* https://github.com/bradfitz/http2
|
||||
* https://github.com/tatsuhiro-t/go-nghttp2
|
||||
* https://github.com/tatsuhiro-t/spdy
|
||||
* golang.org/x/net/websocket
|
||||
|
||||
To download the above packages, after settings ``GOPATH``, run the
|
||||
following command under ``integration-tests`` directory::
|
||||
|
|
|
@ -38,6 +38,7 @@ itprep:
|
|||
go get -d -v github.com/bradfitz/http2
|
||||
go get -d -v github.com/tatsuhiro-t/go-nghttp2
|
||||
go get -d -v github.com/tatsuhiro-t/spdy
|
||||
go get -d -v golang.org/x/net/websocket
|
||||
|
||||
it:
|
||||
sh setenv go test -v
|
||||
|
|
|
@ -2,8 +2,10 @@ package nghttp2
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/bradfitz/http2/hpack"
|
||||
"golang.org/x/net/websocket"
|
||||
"io"
|
||||
"net/http"
|
||||
"syscall"
|
||||
|
@ -312,6 +314,26 @@ 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)
|
||||
}))
|
||||
defer st.Close()
|
||||
|
||||
content := []byte("hello world")
|
||||
res, err := st.websocket(requestParam{
|
||||
name: "TestH1H1Websocket",
|
||||
body: content,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Error st.websocket() = %v", err)
|
||||
}
|
||||
if got, want := res.body, content; !bytes.Equal(got, want) {
|
||||
t.Errorf("echo: %q; want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestH1H2ConnectFailure tests that server handles the situation that
|
||||
// connection attempt to HTTP/2 backend failed.
|
||||
func TestH1H2ConnectFailure(t *testing.T) {
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/bradfitz/http2/hpack"
|
||||
"github.com/tatsuhiro-t/go-nghttp2"
|
||||
"github.com/tatsuhiro-t/spdy"
|
||||
"golang.org/x/net/websocket"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
|
@ -66,6 +67,10 @@ func newServerTester(args []string, t *testing.T, handler http.HandlerFunc) *ser
|
|||
return newServerTesterInternal(args, t, handler, false, nil)
|
||||
}
|
||||
|
||||
func newServerTesterHandler(args []string, t *testing.T, handler http.Handler) *serverTester {
|
||||
return newServerTesterInternal(args, t, handler, false, 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, nil)
|
||||
|
@ -79,7 +84,7 @@ func newServerTesterTLSConfig(args []string, t *testing.T, handler http.HandlerF
|
|||
|
||||
// newServerTesterInternal creates test context. If frontendTLS is
|
||||
// true, set up TLS frontend connection.
|
||||
func newServerTesterInternal(args []string, t *testing.T, handler http.HandlerFunc, frontendTLS bool, clientConfig *tls.Config) *serverTester {
|
||||
func newServerTesterInternal(args []string, t *testing.T, handler http.Handler, frontendTLS bool, clientConfig *tls.Config) *serverTester {
|
||||
ts := httptest.NewUnstartedServer(handler)
|
||||
|
||||
backendTLS := false
|
||||
|
@ -279,6 +284,41 @@ func (cbr *chunkedBodyReader) Read(p []byte) (n int, err error) {
|
|||
return cbr.body.Read(p)
|
||||
}
|
||||
|
||||
func (st *serverTester) websocket(rp requestParam) (*serverResponse, error) {
|
||||
urlstring := st.url + "/echo"
|
||||
|
||||
config, err := websocket.NewConfig(urlstring, st.url)
|
||||
if err != nil {
|
||||
st.t.Fatalf("websocket.NewConfig(%q, %q) returned error: %v", urlstring, st.url, err)
|
||||
}
|
||||
|
||||
config.Header.Add("Test-Case", rp.name)
|
||||
for _, h := range rp.header {
|
||||
config.Header.Add(h.Name, h.Value)
|
||||
}
|
||||
|
||||
ws, err := websocket.NewClient(config, st.conn)
|
||||
if err != nil {
|
||||
st.t.Fatalf("Error creating websocket client: %v", err)
|
||||
}
|
||||
|
||||
if _, err := ws.Write(rp.body); err != nil {
|
||||
st.t.Fatalf("ws.Write() returned error: %v", err)
|
||||
}
|
||||
|
||||
msg := make([]byte, 1024)
|
||||
var n int
|
||||
if n, err = ws.Read(msg); err != nil {
|
||||
st.t.Fatalf("ws.Read() returned error: %v", err)
|
||||
}
|
||||
|
||||
res := &serverResponse{
|
||||
body: msg[:n],
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (st *serverTester) http1(rp requestParam) (*serverResponse, error) {
|
||||
method := "GET"
|
||||
if rp.method != "" {
|
||||
|
|
Loading…
Reference in New Issue