-
Notifications
You must be signed in to change notification settings - Fork 25
/
oauthws_test.go
102 lines (89 loc) · 2.21 KB
/
oauthws_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
package bot
import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"log"
"testing"
"time"
"github.com/gofrs/uuid/v5"
"nhooyr.io/websocket"
)
func TestOauthWS(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
opts := &websocket.DialOptions{
Subprotocols: []string{"Mixin-OAuth-1"},
}
c, _, err := websocket.Dial(ctx, "wss://blaze.mixin.one", opts)
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
go readPumpTest(ctx, c)
blazeMessage, _ := json.Marshal(BlazeMessage{
Id: uuid.Must(uuid.NewV4()).String(),
Action: "REFRESH_OAUTH_CODE",
Params: map[string]interface{}{
"client_id": "67a87828-18f5-46a1-b6cc-c72a97a77c43",
"scope": "PROFILE:READ",
},
})
err = writeGzipToConnTest(ctx, c, blazeMessage)
log.Println(err)
time.Sleep(3 * time.Second)
}
type BlazeMessageTest struct {
Id string `json:"id"`
Action string `json:"action"`
Params map[string]interface{} `json:"params,omitempty"`
Data interface{} `json:"data,omitempty"`
}
func parseMessageTest(ctx context.Context, wsReader io.Reader) error {
var message BlazeMessageTest
gzReader, err := gzip.NewReader(wsReader)
if err != nil {
return err
}
defer gzReader.Close()
if err = json.NewDecoder(gzReader).Decode(&message); err != nil {
return err
}
log.Printf("parseMessage %#v", message)
return nil
}
func readPumpTest(ctx context.Context, conn *websocket.Conn) error {
for {
messageType, wsReader, err := conn.Reader(ctx)
if err != nil {
return err
}
if messageType != websocket.MessageBinary {
return fmt.Errorf("messageType %d", messageType)
}
err = parseMessageTest(ctx, wsReader)
if err != nil {
return err
}
}
}
func writeGzipToConnTest(ctx context.Context, conn *websocket.Conn, msg []byte) error {
wsWriter, err := conn.Writer(ctx, websocket.MessageBinary)
if err != nil {
return err
}
gzWriter, err := gzip.NewWriterLevel(wsWriter, 3)
if err != nil {
return err
}
if _, err := gzWriter.Write(msg); err != nil {
return err
}
if err := gzWriter.Close(); err != nil {
return err
}
return wsWriter.Close()
}