mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Added test from Echo.WebSocket()
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
2f62f3a599
commit
6f728d428d
10
echo.go
10
echo.go
@ -120,9 +120,9 @@ var (
|
|||||||
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHTTPError(code int, msgs ...string) *HTTPError {
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||||
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
||||||
for _, m := range msgs {
|
for _, m := range msg {
|
||||||
he.Message = m
|
he.Message = m
|
||||||
}
|
}
|
||||||
return he
|
return he
|
||||||
@ -272,16 +272,16 @@ func (e *Echo) Trace(path string, h Handler) {
|
|||||||
|
|
||||||
// WebSocket adds a WebSocket route > handler to the router.
|
// WebSocket adds a WebSocket route > handler to the router.
|
||||||
func (e *Echo) WebSocket(path string, h HandlerFunc) {
|
func (e *Echo) WebSocket(path string, h HandlerFunc) {
|
||||||
e.Get(path, func(c *Context) *HTTPError {
|
e.Get(path, func(c *Context) (err error) {
|
||||||
wss := websocket.Server{
|
wss := websocket.Server{
|
||||||
Handler: func(ws *websocket.Conn) {
|
Handler: func(ws *websocket.Conn) {
|
||||||
c.Socket = ws
|
c.Socket = ws
|
||||||
c.Response.status = http.StatusSwitchingProtocols
|
c.Response.status = http.StatusSwitchingProtocols
|
||||||
h(c)
|
err = h(c)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
wss.ServeHTTP(c.Response.writer, c.Request)
|
wss.ServeHTTP(c.Response.writer, c.Request)
|
||||||
return nil
|
return err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
28
echo_test.go
28
echo_test.go
@ -2,9 +2,12 @@ package echo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -246,6 +249,31 @@ func TestEchoMethod(t *testing.T) {
|
|||||||
e.Trace("/", h)
|
e.Trace("/", h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWebSocket(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
e.WebSocket("/ws", func(c *Context) error {
|
||||||
|
c.Socket.Write([]byte("test"))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
srv := httptest.NewServer(e)
|
||||||
|
defer srv.Close()
|
||||||
|
addr := srv.Listener.Addr().String()
|
||||||
|
origin := "http://localhost"
|
||||||
|
url := fmt.Sprintf("ws://%s/ws", addr)
|
||||||
|
ws, err := websocket.Dial(url, "", origin)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ws.Write([]byte("test"))
|
||||||
|
defer ws.Close()
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(ws)
|
||||||
|
s := buf.String()
|
||||||
|
if s != "test" {
|
||||||
|
t.Errorf("expected `test`, got %s.", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEchoURL(t *testing.T) {
|
func TestEchoURL(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
static := func(*Context) error { return nil }
|
static := func(*Context) error { return nil }
|
||||||
|
@ -2,11 +2,12 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"bytes"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,13 +40,11 @@ func TestGzip(t *testing.T) {
|
|||||||
r, err := gzip.NewReader(w.Body)
|
r, err := gzip.NewReader(w.Body)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
b, err := ioutil.ReadAll(r)
|
buf := new(bytes.Buffer)
|
||||||
if err != nil {
|
buf.ReadFrom(r)
|
||||||
t.Error(err)
|
s = buf.String()
|
||||||
}
|
|
||||||
s = string(b)
|
|
||||||
if s != "test" {
|
if s != "test" {
|
||||||
t.Errorf("expected body `test`, got %s.", s)
|
t.Errorf("expected body `test`, got %s.", s)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user