diff options
author | Matt Holt <[email protected]> | 2015-06-23 10:06:05 -0600 |
---|---|---|
committer | Matt Holt <[email protected]> | 2015-06-23 10:06:05 -0600 |
commit | 7044cbbd677b3fe47565f38425c0cd8e358ee427 (patch) | |
tree | a7e5ef7701c9c55c178de2a4d58cdac89e5cbf07 | |
parent | 06a7f1d3dac161526ac5cde523b9fde04a72b050 (diff) | |
parent | 292c15cd4802f967a1f34e9536c666a4a2be5e71 (diff) | |
download | caddy-7044cbbd677b3fe47565f38425c0cd8e358ee427.tar.gz caddy-7044cbbd677b3fe47565f38425c0cd8e358ee427.zip |
Merge pull request #161 from Karthic-Hackintosh/master
Modularizing config/setup/Websocket.go and test file for the same
-rw-r--r-- | config/setup/websocket.go | 20 | ||||
-rw-r--r-- | config/setup/websocket_test.go | 36 |
2 files changed, 51 insertions, 5 deletions
diff --git a/config/setup/websocket.go b/config/setup/websocket.go index 33f0a27db..7628305b4 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 000000000..07a8e2cf3 --- /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") + } + +} |