diff --git a/config/setup/websocket.go b/config/setup/websocket.go index 33f0a27d..7628305b 100644 --- a/config/setup/websocket.go +++ b/config/setup/websocket.go @@ -7,6 +7,20 @@ import ( // WebSocket configures a new WebSockets middleware instance. func WebSocket(c *Controller) (middleware.Middleware, error) { + + websocks, err := websocketParse(c) + if err != nil { + return nil, err + } + websockets.GatewayInterface = c.AppName + "-CGI/1.1" + websockets.ServerSoftware = c.AppName + "/" + c.AppVersion + + return func(next middleware.Handler) middleware.Handler { + return websockets.WebSockets{Next: next, Sockets: websocks} + }, nil +} + +func websocketParse(c *Controller) ([]websockets.Config, error) { var websocks []websockets.Config var respawn bool @@ -68,10 +82,6 @@ func WebSocket(c *Controller) (middleware.Middleware, error) { }) } - websockets.GatewayInterface = c.AppName + "-CGI/1.1" - websockets.ServerSoftware = c.AppName + "/" + c.AppVersion + return websocks, nil - return func(next middleware.Handler) middleware.Handler { - return websockets.WebSockets{Next: next, Sockets: websocks} - }, nil } diff --git a/config/setup/websocket_test.go b/config/setup/websocket_test.go new file mode 100644 index 00000000..07a8e2cf --- /dev/null +++ b/config/setup/websocket_test.go @@ -0,0 +1,36 @@ +package setup + +import ( + "github.com/mholt/caddy/middleware/websockets" + "testing" +) + +func TestWebSocket(t *testing.T) { + + c := NewTestController(`websocket cat`) + + mid, err := WebSocket(c) + + if err != nil { + t.Errorf("Expected no errors, got: %v", err) + } + + if mid == nil { + t.Fatal("Expected middleware, was nil instead") + } + + handler := mid(EmptyNext) + myHandler, ok := handler.(websockets.WebSockets) + + if !ok { + t.Fatalf("Expected handler to be type WebSockets, got: %#v", handler) + } + + if myHandler.Sockets[0].Path != "/" { + t.Errorf("Expected / as the default Path") + } + if myHandler.Sockets[0].Command != "cat" { + t.Errorf("Expected %s as the command", "cat") + } + +}