-
Notifications
You must be signed in to change notification settings - Fork 18
/
pure_websocket_subscriber_option_test.go
148 lines (142 loc) · 3.35 KB
/
pure_websocket_subscriber_option_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
package appsync
import (
"strings"
"testing"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/sony/appsync-client-go/graphql"
)
var (
realtimeEndpoint = "wss://example1234567890000.appsync-realtime-api.us-east-1.amazonaws.com/graphql"
request = graphql.PostRequest{}
onReceive = func(*graphql.Response) {}
onConnectionLost = func(error) {}
)
func TestWithAPIKey(t *testing.T) {
type args struct {
host string
apiKey string
}
tests := []struct {
name string
args args
want PureWebSocketSubscriberOption
}{
{
name: "WithAPIKey Success",
args: args{
host: "example1234567890000.appsync-api.us-east-1.amazonaws.com",
apiKey: "apikey",
},
},
{
name: "WithAPIKey with sanitize Success",
args: args{
host: "https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql",
apiKey: "apikey",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewPureWebSocketSubscriber(realtimeEndpoint, request, onReceive, onConnectionLost)
if len(s.header) != 0 {
t.Fatal(s.header)
}
opt := WithAPIKey(tt.args.host, tt.args.apiKey)
opt(s)
if len(s.header) != 2 {
t.Fatal(s.header)
}
if s.header.Get("host") != sanitize(tt.args.host) {
t.Errorf("got: %s, want: %s", s.header.Get("host"), tt.args.host)
}
if s.header.Get("x-api-key") != tt.args.apiKey {
t.Errorf("got: %s, want: %s", s.header.Get("x-api-key"), tt.args.apiKey)
}
})
}
}
func TestWithOIDC(t *testing.T) {
type args struct {
host string
jwt string
}
tests := []struct {
name string
args args
}{
{
name: "WithOIDC Success",
args: args{
host: "example1234567890000.appsync-api.us-east-1.amazonaws.com",
jwt: "jwt",
},
},
{
name: "WithOIDC with sanitize Success",
args: args{
host: "https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql",
jwt: "jwt",
},
},
{
name: "WithOIDC with trim Bearer Success",
args: args{
host: "https://example1234567890000.appsync-api.us-east-1.amazonaws.com/graphql",
jwt: "Bearer jwt",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewPureWebSocketSubscriber(realtimeEndpoint, request, onReceive, onConnectionLost)
if len(s.header) != 0 {
t.Fatal(s.header)
}
opt := WithOIDC(tt.args.host, tt.args.jwt)
opt(s)
if len(s.header) != 2 {
t.Fatal(s.header)
}
if s.header.Get("host") != sanitize(tt.args.host) {
t.Errorf("got: %s, want: %s", s.header.Get("host"), tt.args.host)
}
if s.header.Get("authorization") != strings.TrimPrefix(tt.args.jwt, "Bearer ") {
t.Errorf("got: %s, want: %s", s.header.Get("authorization"), strings.TrimPrefix(tt.args.jwt, "Bearer "))
}
})
}
}
func TestWithIAM(t *testing.T) {
type args struct {
signer *v4.Signer
region string
host string
}
tests := []struct {
name string
args args
}{
{
name: "WithIAM Success",
args: args{
signer: &v4.Signer{},
region: "region",
host: "host",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewPureWebSocketSubscriber(realtimeEndpoint, request, onReceive, onConnectionLost)
if s.sigv4 != nil {
t.Fatal(s.sigv4)
}
opt := WithIAM(tt.args.signer, tt.args.region, tt.args.host)
opt(s)
if s.sigv4 == nil {
t.Fatal(s.sigv4)
}
})
}
}