0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Merge pull request #161 from Karthic-Hackintosh/master

Modularizing config/setup/Websocket.go and test file for the same
This commit is contained in:
Matt Holt 2015-06-23 10:06:05 -06:00
commit 7044cbbd67
2 changed files with 51 additions and 5 deletions

View file

@ -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) {
}) })
} }
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
} }

View file

@ -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")
}
}