Skip to content

Commit

Permalink
Oxy with gorilla for websocket(+integration tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens committed Jul 27, 2017
1 parent a09a8b1 commit 888e6dc
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 82 deletions.
4 changes: 2 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import:
- package: github.com/cenk/backoff
- package: github.com/containous/flaeg
- package: github.com/vulcand/oxy
version: 7da864c1d53bd58165435bb78bbf8c01f01c8f4a
version: 49f1894c20d972f5c73ff44b859f87deb83f0076
repo: https://github.com/containous/oxy.git
vcs: git
subpackages:
Expand Down
24 changes: 24 additions & 0 deletions integration/fixtures/websocket/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defaultEntryPoints = ["http"]

logLevel = "DEBUG"

[entryPoints]
[entryPoints.http]
address = ":8000"


[web]
address = ":8080"

[file]

[backends]
[backends.backend1]
[backends.backend1.servers.server1]
url = "{{ .WebsocketServer }}"

[frontends]
[frontends.frontend1]
backend = "backend1"
[frontends.frontend1.routes.test_1]
rule = "Path:/ws"
15 changes: 15 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/containous/traefik/integration/utils"
"github.com/go-check/check"

"bytes"

compose "github.com/libkermit/compose/check"
checker "github.com/vdemeester/shakers"
)
Expand All @@ -38,6 +40,7 @@ func init() {
check.Suite(&EurekaSuite{})
check.Suite(&AcmeSuite{})
check.Suite(&DynamoDBSuite{})
check.Suite(&WebsocketSuite{})
}

var traefikBinary = "../dist/traefik"
Expand Down Expand Up @@ -71,6 +74,18 @@ func (s *BaseSuite) createComposeProject(c *check.C, name string) {
s.composeProject = compose.CreateProject(c, projectName, composeFile)
}

func withConfigFile(file string) string {
return "--configFile=" + file
}

func (s *BaseSuite) cmdTraefik(args ...string) (*exec.Cmd, *bytes.Buffer) {
cmd := exec.Command(traefikBinary, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
return cmd, &out
}

func (s *BaseSuite) traefikCmd(c *check.C, args ...string) (*exec.Cmd, string) {
cmd, out, err := utils.RunCommand(traefikBinary, args...)
c.Assert(err, checker.IsNil, check.Commentf("Fail to run %s with %v", traefikBinary, args))
Expand Down
81 changes: 81 additions & 0 deletions integration/websocket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"net/http"
"net/http/httptest"
"time"

"github.com/go-check/check"

"errors"
"io/ioutil"
"os"
"strings"

"github.com/containous/traefik/integration/utils"
"github.com/gorilla/websocket"
checker "github.com/vdemeester/shakers"
)

// WebsocketSuite
type WebsocketSuite struct{ BaseSuite }

func (suite *WebsocketSuite) TestBase(c *check.C) {
var upgrader = websocket.Upgrader{} // use default options

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
break
}
err = c.WriteMessage(mt, message)
if err != nil {
break
}
}
}))

file := suite.adaptFile(c, "fixtures/websocket/config.toml", struct {
WebsocketServer string
}{
WebsocketServer: srv.URL,
})

defer os.Remove(file)
cmd, _ := suite.cmdTraefik(withConfigFile(file), "--debug")

err := cmd.Start()

c.Assert(err, check.IsNil)
defer cmd.Process.Kill()

// wait for traefik
err = utils.TryRequest("http://127.0.0.1:8080/api/providers", 60*time.Second, func(res *http.Response) error {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if !strings.Contains(string(body), "127.0.0.1") {
return errors.New("Incorrect traefik config")
}
return nil
})
c.Assert(err, checker.IsNil)

conn, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:8000/ws", nil)

c.Assert(err, checker.IsNil)
conn.WriteMessage(websocket.TextMessage, []byte("OK"))

_, msg, err := conn.ReadMessage()
c.Assert(err, checker.IsNil)

c.Assert(string(msg), checker.Equals, "OK")

}
92 changes: 27 additions & 65 deletions vendor/github.com/vulcand/oxy/forward/fwd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 35 additions & 14 deletions vendor/github.com/vulcand/oxy/forward/headers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 888e6dc

Please sign in to comment.