-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgluawebsocket_test.go
More file actions
104 lines (80 loc) · 2.63 KB
/
gluawebsocket_test.go
File metadata and controls
104 lines (80 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gluawebsocket
import (
"strings"
"testing"
"gluawebsocket/tools"
lua "github.com/yuin/gopher-lua"
)
func TestDialFail(t *testing.T) {
if err := evalLua(t, `
local ws = require("websocket")
local conn, _, err = ws.dial("ws://localhost:8080/echo", {})
assert_not_equal(nil, err)
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err.Error())
}
}
func TestDialSuccess(t *testing.T) {
go tools.StartEchoServer("localhost:8080")
if err := evalLua(t, `
local ws = require("websocket")
local c, _, err = ws.dial("ws://localhost:8080/echo", {})
assert_equal(err, nil)
assert_not_equal(c, nil)
local err = c:write_text("hello")
assert_equal(err, nil)
local msg_type, msg, err = c:read()
assert_equal(err, nil)
assert_equal(msg_type, ws.get_msg_type("text"))
assert_equal(msg, "hello")
local err = c:ping("")
assert_equal(err, nil)
local err = c:close(1000, "")
assert_equal(err, nil)
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err.Error())
}
}
func TestGetMsgType(t *testing.T) {
if err := evalLua(t, `
local ws = require("websocket")
assert_equal(1, ws.get_msg_type("text"))
assert_equal(2, ws.get_msg_type("binary"))
assert_equal(8, ws.get_msg_type("close"))
assert_equal(9, ws.get_msg_type("ping"))
assert_equal(10, ws.get_msg_type("pong"))
assert_equal(-1, ws.get_msg_type("invalid_msg_type"))
`); err != nil {
t.Errorf("Failed to evaluate script: %s", err.Error())
}
}
func evalLua(t *testing.T, script string) error {
L := lua.NewState()
defer L.Close()
Preload(L)
L.SetGlobal("assert_equal", L.NewFunction(func(L *lua.LState) int {
expected := L.Get(1)
actual := L.Get(2)
if expected.Type() != actual.Type() || expected.String() != actual.String() {
t.Errorf("Expected %s %q, got %s %q", expected.Type(), expected, actual.Type(), actual)
}
return 0
}))
L.SetGlobal("assert_not_equal", L.NewFunction(func(L *lua.LState) int {
expected := L.Get(1)
actual := L.Get(2)
if expected.Type() == actual.Type() && expected.String() == actual.String() {
t.Errorf("not expected %s %q, got %s %q", expected.Type(), expected, actual.Type(), actual)
}
return 0
}))
L.SetGlobal("assert_contains", L.NewFunction(func(L *lua.LState) int {
contains := L.Get(1)
actual := L.Get(2)
if !strings.Contains(actual.String(), contains.String()) {
t.Errorf("Expected %s %q contains %s %q", actual.Type(), actual, contains.Type(), contains)
}
return 0
}))
return L.DoString(script)
}