forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
108 lines (92 loc) · 2.56 KB
/
server_test.go
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
105
106
107
108
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package server
import (
"crypto/tls"
"net"
"net/http"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/ortuman/jackal/server/transport"
"github.com/ortuman/jackal/storage"
"github.com/ortuman/jackal/stream/c2s"
"github.com/stretchr/testify/require"
)
func TestSocketServer(t *testing.T) {
storage.Initialize(&storage.Config{Type: storage.Mock})
defer storage.Shutdown()
c2s.Initialize(&c2s.Config{Domains: []string{"jackal.im"}})
defer c2s.Shutdown()
go func() {
time.Sleep(time.Millisecond * 150)
// test XMPP port...
conn, err := net.Dial("tcp", "localhost:5123")
require.Nil(t, err)
require.NotNil(t, conn)
xmlHdr := []byte(`<?xml version="1.0" encoding="UTF-8">`)
n, err := conn.Write(xmlHdr)
require.Nil(t, err)
require.Equal(t, len(xmlHdr), n)
conn.Close()
time.Sleep(time.Millisecond * 150) // wait until disconnected
// test debug port...
req, err := http.NewRequest("GET", "http://localhost:9123/debug/pprof", nil)
require.Nil(t, err)
resp, err := http.DefaultClient.Do(req)
require.Nil(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
Shutdown()
}()
cfg := Config{
ID: "srv-1234",
TLS: TLSConfig{
PrivKeyFile: "../testdata/cert/test.server.key",
CertFile: "../testdata/cert/test.server.crt",
},
Transport: TransportConfig{
Type: transport.Socket,
Port: 5123,
},
}
Initialize([]Config{cfg}, 9123)
}
func TestWebSocketServer(t *testing.T) {
storage.Initialize(&storage.Config{Type: storage.Mock})
defer storage.Shutdown()
c2s.Initialize(&c2s.Config{Domains: []string{"jackal.im"}})
defer c2s.Shutdown()
go func() {
time.Sleep(time.Millisecond * 150)
d := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 15 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
h := http.Header{"Sec-WebSocket-Protocol": []string{"xmpp"}}
conn, _, err := d.Dial("wss://localhost:9876/srv-1234/ws", h)
require.Nil(t, err)
open := []byte(`<?xml version="1.0" encoding="UTF-8">`)
err = conn.WriteMessage(websocket.TextMessage, open)
require.Nil(t, err)
conn.Close()
time.Sleep(time.Millisecond * 150) // wait until disconnected
Shutdown()
}()
cfg := Config{
ID: "srv-1234",
TLS: TLSConfig{
PrivKeyFile: "../testdata/cert/test.server.key",
CertFile: "../testdata/cert/test.server.crt",
},
Transport: TransportConfig{
Type: transport.WebSocket,
Port: 9876,
},
}
Initialize([]Config{cfg}, 0)
}