Commit 292c15cd authored by Karthic Rao's avatar Karthic Rao

Modularizing config/setup/Websocket.go and for the same

parent 06a7f1d3
...@@ -7,6 +7,20 @@ import ( ...@@ -7,6 +7,20 @@ import (
// WebSocket configures a new WebSockets middleware instance. // WebSocket configures a new WebSockets middleware instance.
func WebSocket(c *Controller) (middleware.Middleware, error) { 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 websocks []websockets.Config
var respawn bool var respawn bool
...@@ -68,10 +82,6 @@ func WebSocket(c *Controller) (middleware.Middleware, error) { ...@@ -68,10 +82,6 @@ func WebSocket(c *Controller) (middleware.Middleware, error) {
}) })
} }
websockets.GatewayInterface = c.AppName + "-CGI/1.1" return websocks, nil
websockets.ServerSoftware = c.AppName + "/" + c.AppVersion
return func(next middleware.Handler) middleware.Handler {
return websockets.WebSockets{Next: next, Sockets: websocks}
}, nil
} }
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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment