-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathws_test.go
151 lines (131 loc) · 3.14 KB
/
ws_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package twelvedata //nolint: testpackage
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"github.com/fasthttp/websocket"
"github.com/rs/zerolog"
"github.com/soulgarden/twelvedata/response"
)
// nolint: gocognit
func TestWS_Subscribe(t *testing.T) {
t.Parallel()
type fields struct {
url *url.URL
eventsCh chan response.PriceEvent
dialer *websocket.Dialer
logger zerolog.Logger
}
type args struct {
ctx context.Context
symbols []string
subscribeStatusMsg string
priceEventMsg string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
expectedPrice float64
}{
{
name: "1",
fields: fields{
url: &url.URL{
Scheme: "ws",
Host: "127.0.0.1",
Path: "/quotes/price",
},
eventsCh: make(chan response.PriceEvent),
dialer: websocket.DefaultDialer,
logger: zerolog.New(os.Stdout),
},
args: args{
ctx: context.Background(),
symbols: []string{"AAPL"},
subscribeStatusMsg: `{
"event":"subscribe-status",
"status":"ok","success":[
{
"symbol":"AAPL",
"exchange":"NASDAQ",
"country":"United States",
"type":"Common Stock"
}
],"fails":null
}`,
priceEventMsg: `{
"event":"price",
"symbol":"AAPL",
"currency":"USD",
"exchange":"NASDAQ",
"type":"Common Stock",
"timestamp":1643972766,
"price":172.8700
}`,
},
wantErr: false,
expectedPrice: 172.8700,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(cw http.ResponseWriter, sr *http.Request) {
upgrader := websocket.Upgrader{
Error: func(w http.ResponseWriter, _ *http.Request, status int, reason error) {
http.Error(w, reason.Error(), status)
},
}
ws, err := upgrader.Upgrade(cw, sr, nil)
if err != nil {
t.Error(err)
_ = ws.Close()
}
go func() {
for {
_, body, err := ws.ReadMessage()
if err != nil {
t.Error(err)
}
tt.fields.logger.Debug().Bytes("body", body).Msg("read message")
}
}()
err = ws.WriteMessage(websocket.TextMessage, []byte(tt.args.subscribeStatusMsg))
if err != nil {
t.Error(err)
}
err = ws.WriteMessage(websocket.TextMessage, []byte(tt.args.priceEventMsg))
if err != nil {
t.Error(err)
}
}))
defer server.Close()
ws := &WS{
url: &url.URL{
Scheme: "ws",
Host: strings.Replace(server.URL, "http://", "", 1),
Path: "/quotes/price",
},
eventsCh: tt.fields.eventsCh,
dialer: tt.fields.dialer,
logger: &tt.fields.logger,
}
go func() {
if err := ws.Subscribe(tt.args.ctx, tt.args.symbols); (err != nil) != tt.wantErr {
t.Errorf("Subscribe() error = %v, wantErr %v", err, tt.wantErr)
}
}()
resp := <-ws.Consume()
if resp.Price != tt.expectedPrice {
t.Error("price not equal")
}
})
}
}