aws-appsync-subscription-link 3.1.3 → 4.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,525 +1,595 @@
1
1
  import { AUTH_TYPE } from "aws-appsync-auth-link";
2
- import { execute } from "@apollo/client/core";
3
- import gql from 'graphql-tag';
4
- import { AppSyncRealTimeSubscriptionHandshakeLink } from '../../src/realtime-subscription-handshake-link';
2
+ import { execute, ApolloClient } from "@apollo/client/core";
3
+ import gql from "graphql-tag";
4
+ import { AppSyncRealTimeSubscriptionHandshakeLink } from "../../src/realtime-subscription-handshake-link";
5
5
  import { MESSAGE_TYPES } from "../../src/types";
6
6
  import { v4 as uuid } from "uuid";
7
- jest.mock('uuid', () => ({ v4: jest.fn() }));
7
+ jest.mock("uuid", () => ({ v4: jest.fn() }));
8
8
 
9
- const query = gql`subscription { someSubscription { aField } }`
9
+ const query = gql`
10
+ subscription {
11
+ someSubscription {
12
+ aField
13
+ }
14
+ }
15
+ `;
16
+
17
+ // Mock ApolloClient for execute context
18
+ const mockClient = {} as ApolloClient;
10
19
 
11
20
  class myWebSocket implements WebSocket {
12
- binaryType: BinaryType;
13
- bufferedAmount: number;
14
- extensions: string;
15
- onclose: (this: WebSocket, ev: CloseEvent) => any;
16
- onerror: (this: WebSocket, ev: Event) => any;
17
- onmessage: (this: WebSocket, ev: MessageEvent) => any;
18
- onopen: (this: WebSocket, ev: Event) => any;
19
- protocol: string;
20
- readyState: number;
21
- url: string;
22
- close(code?: number, reason?: string): void {
23
- throw new Error("Method not implemented.");
24
- }
25
- send(data: string | ArrayBuffer | Blob | ArrayBufferView): void {
26
- throw new Error("Method not implemented.");
27
- }
28
- CLOSED: number;
29
- CLOSING: number;
30
- CONNECTING: number;
31
- OPEN: number;
32
- addEventListener<K extends "close" | "error" | "message" | "open">(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => void, options?: boolean | AddEventListenerOptions): void;
33
- addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
34
- addEventListener(type: any, listener: any, options?: any) {
35
- throw new Error("Method not implemented.");
36
- }
37
- removeEventListener<K extends "close" | "error" | "message" | "open">(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => void, options?: boolean | EventListenerOptions): void;
38
- removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
39
- removeEventListener(type: any, listener: any, options?: any) {
40
- throw new Error("Method not implemented.");
41
- }
42
- dispatchEvent(event: Event): boolean {
43
- throw new Error("Method not implemented.");
44
- }
21
+ binaryType: BinaryType = "arraybuffer";
22
+ bufferedAmount: number;
23
+ extensions: string;
24
+ onclose: (this: WebSocket, ev: CloseEvent) => any;
25
+ onerror: (this: WebSocket, ev: Event) => any;
26
+ onmessage: (this: WebSocket, ev: MessageEvent) => any;
27
+ onopen: (this: WebSocket, ev: Event) => any;
28
+ protocol: string;
29
+ readyState: number;
30
+ url: string;
31
+ close(code?: number, reason?: string): void {
32
+ throw new Error("Method not implemented.");
33
+ }
34
+ send(data: string | ArrayBuffer | Blob | ArrayBufferView): void {
35
+ throw new Error("Method not implemented.");
36
+ }
37
+ CLOSED: number;
38
+ CLOSING: number;
39
+ CONNECTING: number;
40
+ OPEN: number;
41
+ addEventListener<K extends "close" | "error" | "message" | "open">(
42
+ type: K,
43
+ listener: (this: WebSocket, ev: WebSocketEventMap[K]) => void,
44
+ options?: boolean | AddEventListenerOptions
45
+ ): void;
46
+ addEventListener(
47
+ type: string,
48
+ listener: EventListenerOrEventListenerObject,
49
+ options?: boolean | AddEventListenerOptions
50
+ ): void;
51
+ addEventListener(type: any, listener: any, options?: any) {
52
+ throw new Error("Method not implemented.");
53
+ }
54
+ removeEventListener<K extends "close" | "error" | "message" | "open">(
55
+ type: K,
56
+ listener: (this: WebSocket, ev: WebSocketEventMap[K]) => void,
57
+ options?: boolean | EventListenerOptions
58
+ ): void;
59
+ removeEventListener(
60
+ type: string,
61
+ listener: EventListenerOrEventListenerObject,
62
+ options?: boolean | EventListenerOptions
63
+ ): void;
64
+ removeEventListener(type: any, listener: any, options?: any) {
65
+ throw new Error("Method not implemented.");
66
+ }
67
+ dispatchEvent(event: Event): boolean {
68
+ throw new Error("Method not implemented.");
69
+ }
45
70
  }
46
71
 
47
72
  describe("RealTime subscription link", () => {
48
-
49
- test("Can instantiate link", () => {
50
- expect.assertions(1);
51
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
52
- auth: {
53
- type: AUTH_TYPE.API_KEY,
54
- apiKey: 'xxxxx'
55
- },
56
- region: 'us-west-2',
57
- url: 'https://firsttesturl12345678901234.appsync-api.us-west-2.amazonaws.com/graphql'
58
- });
59
-
60
- expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
73
+ test("Can instantiate link", () => {
74
+ expect.assertions(1);
75
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
76
+ auth: {
77
+ type: AUTH_TYPE.API_KEY,
78
+ apiKey: "xxxxx",
79
+ },
80
+ region: "us-west-2",
81
+ url: "https://firsttesturl12345678901234.appsync-api.us-west-2.amazonaws.com/graphql",
61
82
  });
62
83
 
63
- test("Can instantiate link with custom domain", () => {
64
- expect.assertions(1);
65
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
66
- auth: {
67
- type: AUTH_TYPE.API_KEY,
68
- apiKey: 'xxxxx'
69
- },
70
- region: 'us-west-2',
71
- url: 'https://test1.testcustomdomain.com/graphql'
72
- });
73
-
74
- expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
84
+ expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
85
+ });
86
+
87
+ test("Can instantiate link with custom domain", () => {
88
+ expect.assertions(1);
89
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
90
+ auth: {
91
+ type: AUTH_TYPE.API_KEY,
92
+ apiKey: "xxxxx",
93
+ },
94
+ region: "us-west-2",
95
+ url: "https://test1.testcustomdomain.com/graphql",
75
96
  });
76
97
 
77
- test("Initialize WebSocket correctly for API KEY", (done) => {
78
- expect.assertions(2);
79
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
80
- return "2019-11-13T18:47:04.733Z";
81
- }));
82
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
83
- expect(url).toBe('wss://apikeytesturl1234567890123.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJob3N0IjoiYXBpa2V5dGVzdHVybDEyMzQ1Njc4OTAxMjMuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20iLCJ4LWFtei1kYXRlIjoiMjAxOTExMTNUMTg0NzA0WiIsIngtYXBpLWtleSI6Inh4eHh4In0=&payload=e30=');
84
- expect(protocol).toBe('graphql-ws');
85
- done();
86
- return new myWebSocket();
87
- });
88
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
89
- auth: {
90
- type: AUTH_TYPE.API_KEY,
91
- apiKey: 'xxxxx'
92
- },
93
- region: 'us-west-2',
94
- url: 'https://apikeytesturl1234567890123.appsync-api.us-west-2.amazonaws.com/graphql'
95
- });
96
-
97
- execute(link, { query }).subscribe({
98
- error: (err) => {
99
- console.log(JSON.stringify(err));
100
- fail;
101
- },
102
- next: (data) => {
103
- console.log({ data });
104
- done();
105
- },
106
- complete: () => {
107
- console.log('done with this');
108
- done();
109
- }
110
-
111
- });
98
+ expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
99
+ });
100
+
101
+ test("Initialize WebSocket correctly for API KEY", (done) => {
102
+ expect.assertions(2);
103
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
104
+ jest.fn(() => {
105
+ return "2019-11-13T18:47:04.733Z";
106
+ })
107
+ );
108
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
109
+ (url, protocol) => {
110
+ expect(url).toBe(
111
+ "wss://apikeytesturl1234567890123.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJob3N0IjoiYXBpa2V5dGVzdHVybDEyMzQ1Njc4OTAxMjMuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20iLCJ4LWFtei1kYXRlIjoiMjAxOTExMTNUMTg0NzA0WiIsIngtYXBpLWtleSI6Inh4eHh4In0=&payload=e30="
112
+ );
113
+ expect(protocol).toBe("graphql-ws");
114
+ done();
115
+ return new myWebSocket();
116
+ }
117
+ );
118
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
119
+ auth: {
120
+ type: AUTH_TYPE.API_KEY,
121
+ apiKey: "xxxxx",
122
+ },
123
+ region: "us-west-2",
124
+ url: "https://apikeytesturl1234567890123.appsync-api.us-west-2.amazonaws.com/graphql",
112
125
  });
113
126
 
114
- test("Initialize WebSocket correctly for API KEY with custom domain", (done) => {
115
- expect.assertions(2);
116
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
117
- return "2019-11-13T18:47:04.733Z";
118
- }));
119
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
120
- expect(url).toBe('wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30=');
121
- expect(protocol).toBe('graphql-ws');
122
- done();
123
- return new myWebSocket();
124
- });
125
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
126
- auth: {
127
- type: AUTH_TYPE.API_KEY,
128
- apiKey: 'xxxxx'
129
- },
130
- region: 'us-west-2',
131
- url: 'https://apikeytest.testcustomdomain.com/graphql'
132
- });
133
-
134
- execute(link, { query }).subscribe({
135
- error: (err) => {
136
- console.log(JSON.stringify(err));
137
- fail;
138
- },
139
- next: (data) => {
140
- console.log({ data });
141
- done();
142
- },
143
- complete: () => {
144
- console.log('done with this');
145
- done();
146
- }
147
-
148
- });
127
+ execute(link, { query }, { client: mockClient }).subscribe({
128
+ error: (err) => {
129
+ console.log(JSON.stringify(err));
130
+ fail;
131
+ },
132
+ next: (data) => {
133
+ console.log({ data });
134
+ done();
135
+ },
136
+ complete: () => {
137
+ console.log("done with this");
138
+ done();
139
+ },
149
140
  });
150
-
151
- test("Initialize WebSocket correctly for COGNITO USER POOLS", (done) => {
152
- expect.assertions(2);
153
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
154
- return "2019-11-13T18:47:04.733Z";
155
- }));
156
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
157
- expect(url).toBe('wss://cognitouserpooltesturl1234.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiY29nbml0b3VzZXJwb29sdGVzdHVybDEyMzQuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30=');
158
- expect(protocol).toBe('graphql-ws');
159
- done();
160
- return new myWebSocket();
161
- });
162
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
163
- auth: {
164
- type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
165
- jwtToken: 'token'
166
- },
167
- region: 'us-west-2',
168
- url: 'https://cognitouserpooltesturl1234.appsync-api.us-west-2.amazonaws.com/graphql'
169
- });
170
-
171
- execute(link, { query }).subscribe({
172
- error: (err) => {
173
- console.log(JSON.stringify(err));
174
- fail;
175
- },
176
- next: (data) => {
177
- console.log({ data });
178
- done();
179
- },
180
- complete: () => {
181
- console.log('done with this');
182
- done();
183
- }
184
-
185
- });
141
+ });
142
+
143
+ test("Initialize WebSocket correctly for API KEY with custom domain", (done) => {
144
+ expect.assertions(2);
145
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
146
+ jest.fn(() => {
147
+ return "2019-11-13T18:47:04.733Z";
148
+ })
149
+ );
150
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
151
+ (url, protocol) => {
152
+ expect(url).toBe(
153
+ "wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30="
154
+ );
155
+ expect(protocol).toBe("graphql-ws");
156
+ done();
157
+ return new myWebSocket();
158
+ }
159
+ );
160
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
161
+ auth: {
162
+ type: AUTH_TYPE.API_KEY,
163
+ apiKey: "xxxxx",
164
+ },
165
+ region: "us-west-2",
166
+ url: "https://apikeytest.testcustomdomain.com/graphql",
186
167
  });
187
168
 
188
- test("Initialize WebSocket correctly for COGNITO USER POOLS with custom domain", (done) => {
189
- expect.assertions(2);
190
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
191
- return "2019-11-13T18:47:04.733Z";
192
- }));
193
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
194
- expect(url).toBe('wss://cognitouserpools.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiY29nbml0b3VzZXJwb29scy50ZXN0Y3VzdG9tZG9tYWluLmNvbSJ9&payload=e30=');
195
- expect(protocol).toBe('graphql-ws');
196
- done();
197
- return new myWebSocket();
198
- });
199
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
200
- auth: {
201
- type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
202
- jwtToken: 'token'
203
- },
204
- region: 'us-west-2',
205
- url: 'https://cognitouserpools.testcustomdomain.com/graphql'
206
- });
207
-
208
- execute(link, { query }).subscribe({
209
- error: (err) => {
210
- console.log(JSON.stringify(err));
211
- fail;
212
- },
213
- next: (data) => {
214
- console.log({ data });
215
- done();
216
- },
217
- complete: () => {
218
- console.log('done with this');
219
- done();
220
- }
221
-
222
- });
169
+ execute(link, { query }, { client: mockClient }).subscribe({
170
+ error: (err) => {
171
+ console.log(JSON.stringify(err));
172
+ fail;
173
+ },
174
+ next: (data) => {
175
+ console.log({ data });
176
+ done();
177
+ },
178
+ complete: () => {
179
+ console.log("done with this");
180
+ done();
181
+ },
182
+ });
183
+ });
184
+
185
+ test("Initialize WebSocket correctly for COGNITO USER POOLS", (done) => {
186
+ expect.assertions(2);
187
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
188
+ jest.fn(() => {
189
+ return "2019-11-13T18:47:04.733Z";
190
+ })
191
+ );
192
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
193
+ (url, protocol) => {
194
+ expect(url).toBe(
195
+ "wss://cognitouserpooltesturl1234.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiY29nbml0b3VzZXJwb29sdGVzdHVybDEyMzQuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30="
196
+ );
197
+ expect(protocol).toBe("graphql-ws");
198
+ done();
199
+ return new myWebSocket();
200
+ }
201
+ );
202
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
203
+ auth: {
204
+ type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
205
+ jwtToken: "token",
206
+ },
207
+ region: "us-west-2",
208
+ url: "https://cognitouserpooltesturl1234.appsync-api.us-west-2.amazonaws.com/graphql",
223
209
  });
224
210
 
225
- test("Initialize WebSocket correctly for OPENID_CONNECT", (done) => {
226
- expect.assertions(2);
227
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
228
- return "2019-11-13T18:47:04.733Z";
229
- }));
230
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
231
- expect(url).toBe('wss://openidconnecttesturl123456.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0Ijoib3BlbmlkY29ubmVjdHRlc3R1cmwxMjM0NTYuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30=');
232
- expect(protocol).toBe('graphql-ws');
233
- done();
234
- return new myWebSocket();
235
- });
236
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
237
- auth: {
238
- type: AUTH_TYPE.OPENID_CONNECT,
239
- jwtToken: 'token'
240
- },
241
- region: 'us-west-2',
242
- url: 'https://openidconnecttesturl123456.appsync-api.us-west-2.amazonaws.com/graphql'
243
- });
244
-
245
- execute(link, { query }).subscribe({
246
- error: (err) => {
247
- console.log(JSON.stringify(err));
248
- fail;
249
- },
250
- next: (data) => {
251
- console.log({ data });
252
- done();
253
- },
254
- complete: () => {
255
- console.log('done with this');
256
- done();
257
- }
211
+ execute(link, { query }, { client: mockClient }).subscribe({
212
+ error: (err) => {
213
+ console.log(JSON.stringify(err));
214
+ fail;
215
+ },
216
+ next: (data) => {
217
+ console.log({ data });
218
+ done();
219
+ },
220
+ complete: () => {
221
+ console.log("done with this");
222
+ done();
223
+ },
224
+ });
225
+ });
226
+
227
+ test("Initialize WebSocket correctly for COGNITO USER POOLS with custom domain", (done) => {
228
+ expect.assertions(2);
229
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
230
+ jest.fn(() => {
231
+ return "2019-11-13T18:47:04.733Z";
232
+ })
233
+ );
234
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
235
+ (url, protocol) => {
236
+ expect(url).toBe(
237
+ "wss://cognitouserpools.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiY29nbml0b3VzZXJwb29scy50ZXN0Y3VzdG9tZG9tYWluLmNvbSJ9&payload=e30="
238
+ );
239
+ expect(protocol).toBe("graphql-ws");
240
+ done();
241
+ return new myWebSocket();
242
+ }
243
+ );
244
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
245
+ auth: {
246
+ type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
247
+ jwtToken: "token",
248
+ },
249
+ region: "us-west-2",
250
+ url: "https://cognitouserpools.testcustomdomain.com/graphql",
251
+ });
258
252
 
259
- });
253
+ execute(link, { query }, { client: mockClient }).subscribe({
254
+ error: (err) => {
255
+ console.log(JSON.stringify(err));
256
+ fail;
257
+ },
258
+ next: (data) => {
259
+ console.log({ data });
260
+ done();
261
+ },
262
+ complete: () => {
263
+ console.log("done with this");
264
+ done();
265
+ },
266
+ });
267
+ });
268
+
269
+ test("Initialize WebSocket correctly for OPENID_CONNECT", (done) => {
270
+ expect.assertions(2);
271
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
272
+ jest.fn(() => {
273
+ return "2019-11-13T18:47:04.733Z";
274
+ })
275
+ );
276
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
277
+ (url, protocol) => {
278
+ expect(url).toBe(
279
+ "wss://openidconnecttesturl123456.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0Ijoib3BlbmlkY29ubmVjdHRlc3R1cmwxMjM0NTYuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30="
280
+ );
281
+ expect(protocol).toBe("graphql-ws");
282
+ done();
283
+ return new myWebSocket();
284
+ }
285
+ );
286
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
287
+ auth: {
288
+ type: AUTH_TYPE.OPENID_CONNECT,
289
+ jwtToken: "token",
290
+ },
291
+ region: "us-west-2",
292
+ url: "https://openidconnecttesturl123456.appsync-api.us-west-2.amazonaws.com/graphql",
260
293
  });
261
294
 
262
- test("Initialize WebSocket correctly for OPENID_CONNECT with custom domain", (done) => {
263
- expect.assertions(2);
264
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
265
- return "2019-11-13T18:47:04.733Z";
266
- }));
267
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
268
- expect(url).toBe('wss://openidconnecttesturl.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0Ijoib3BlbmlkY29ubmVjdHRlc3R1cmwudGVzdGN1c3RvbWRvbWFpbi5jb20ifQ==&payload=e30=');
269
- expect(protocol).toBe('graphql-ws');
270
- done();
271
- return new myWebSocket();
272
- });
273
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
274
- auth: {
275
- type: AUTH_TYPE.OPENID_CONNECT,
276
- jwtToken: 'token'
277
- },
278
- region: 'us-west-2',
279
- url: 'https://openidconnecttesturl.testcustomdomain.com/graphql'
280
- });
281
-
282
- execute(link, { query }).subscribe({
283
- error: (err) => {
284
- console.log(JSON.stringify(err));
285
- fail;
286
- },
287
- next: (data) => {
288
- console.log({ data });
289
- done();
290
- },
291
- complete: () => {
292
- console.log('done with this');
293
- done();
294
- }
295
+ execute(link, { query }, { client: mockClient }).subscribe({
296
+ error: (err) => {
297
+ console.log(JSON.stringify(err));
298
+ fail;
299
+ },
300
+ next: (data) => {
301
+ console.log({ data });
302
+ done();
303
+ },
304
+ complete: () => {
305
+ console.log("done with this");
306
+ done();
307
+ },
308
+ });
309
+ });
310
+
311
+ test("Initialize WebSocket correctly for OPENID_CONNECT with custom domain", (done) => {
312
+ expect.assertions(2);
313
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
314
+ jest.fn(() => {
315
+ return "2019-11-13T18:47:04.733Z";
316
+ })
317
+ );
318
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
319
+ (url, protocol) => {
320
+ expect(url).toBe(
321
+ "wss://openidconnecttesturl.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0Ijoib3BlbmlkY29ubmVjdHRlc3R1cmwudGVzdGN1c3RvbWRvbWFpbi5jb20ifQ==&payload=e30="
322
+ );
323
+ expect(protocol).toBe("graphql-ws");
324
+ done();
325
+ return new myWebSocket();
326
+ }
327
+ );
328
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
329
+ auth: {
330
+ type: AUTH_TYPE.OPENID_CONNECT,
331
+ jwtToken: "token",
332
+ },
333
+ region: "us-west-2",
334
+ url: "https://openidconnecttesturl.testcustomdomain.com/graphql",
335
+ });
295
336
 
296
- });
337
+ execute(link, { query }, { client: mockClient }).subscribe({
338
+ error: (err) => {
339
+ console.log(JSON.stringify(err));
340
+ fail;
341
+ },
342
+ next: (data) => {
343
+ console.log({ data });
344
+ done();
345
+ },
346
+ complete: () => {
347
+ console.log("done with this");
348
+ done();
349
+ },
350
+ });
351
+ });
352
+
353
+ test("Initialize WebSocket correctly for AWS_LAMBDA", (done) => {
354
+ expect.assertions(2);
355
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
356
+ jest.fn(() => {
357
+ return "2019-11-13T18:47:04.733Z";
358
+ })
359
+ );
360
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
361
+ (url, protocol) => {
362
+ expect(url).toBe(
363
+ "wss://awslambdatesturl1234567890.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiYXdzbGFtYmRhdGVzdHVybDEyMzQ1Njc4OTAuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30="
364
+ );
365
+ expect(protocol).toBe("graphql-ws");
366
+ done();
367
+ return new myWebSocket();
368
+ }
369
+ );
370
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
371
+ auth: {
372
+ type: AUTH_TYPE.AWS_LAMBDA,
373
+ token: "token",
374
+ },
375
+ region: "us-west-2",
376
+ url: "https://awslambdatesturl1234567890.appsync-api.us-west-2.amazonaws.com/graphql",
297
377
  });
298
378
 
299
- test('Initialize WebSocket correctly for AWS_LAMBDA', (done) => {
300
- expect.assertions(2);
301
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
302
- return "2019-11-13T18:47:04.733Z";
303
- }));
304
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
305
- expect(url).toBe('wss://awslambdatesturl1234567890.appsync-realtime-api.us-west-2.amazonaws.com/graphql?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiYXdzbGFtYmRhdGVzdHVybDEyMzQ1Njc4OTAuYXBwc3luYy1hcGkudXMtd2VzdC0yLmFtYXpvbmF3cy5jb20ifQ==&payload=e30=');
306
- expect(protocol).toBe('graphql-ws');
307
- done();
308
- return new myWebSocket();
309
- });
310
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
311
- auth: {
312
- type: AUTH_TYPE.AWS_LAMBDA,
313
- token: 'token'
314
- },
315
- region: 'us-west-2',
316
- url: 'https://awslambdatesturl1234567890.appsync-api.us-west-2.amazonaws.com/graphql'
317
- });
318
-
319
- execute(link, { query }).subscribe({
320
- error: (err) => {
321
- fail;
322
- },
323
- next: (data) => {
324
- done();
325
- },
326
- complete: () => {
327
- done();
328
- }
379
+ execute(link, { query }, { client: mockClient }).subscribe({
380
+ error: (err) => {
381
+ fail;
382
+ },
383
+ next: (data) => {
384
+ done();
385
+ },
386
+ complete: () => {
387
+ done();
388
+ },
389
+ });
390
+ });
391
+
392
+ test("Initialize WebSocket correctly for AWS_LAMBDA with custom domain", (done) => {
393
+ expect.assertions(2);
394
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
395
+ jest.fn(() => {
396
+ return "2019-11-13T18:47:04.733Z";
397
+ })
398
+ );
399
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
400
+ (url, protocol) => {
401
+ expect(url).toBe(
402
+ "wss://awslambdatesturl.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiYXdzbGFtYmRhdGVzdHVybC50ZXN0Y3VzdG9tZG9tYWluLmNvbSJ9&payload=e30="
403
+ );
404
+ expect(protocol).toBe("graphql-ws");
405
+ done();
406
+ return new myWebSocket();
407
+ }
408
+ );
409
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
410
+ auth: {
411
+ type: AUTH_TYPE.AWS_LAMBDA,
412
+ token: "token",
413
+ },
414
+ region: "us-west-2",
415
+ url: "https://awslambdatesturl.testcustomdomain.com/graphql",
416
+ });
329
417
 
330
- });
331
- })
332
-
333
- test('Initialize WebSocket correctly for AWS_LAMBDA with custom domain', (done) => {
334
- expect.assertions(2);
335
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
336
- return "2019-11-13T18:47:04.733Z";
337
- }));
338
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
339
- expect(url).toBe('wss://awslambdatesturl.testcustomdomain.com/graphql/realtime?header=eyJBdXRob3JpemF0aW9uIjoidG9rZW4iLCJob3N0IjoiYXdzbGFtYmRhdGVzdHVybC50ZXN0Y3VzdG9tZG9tYWluLmNvbSJ9&payload=e30=');
340
- expect(protocol).toBe('graphql-ws');
341
- done();
342
- return new myWebSocket();
343
- });
344
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
345
- auth: {
346
- type: AUTH_TYPE.AWS_LAMBDA,
347
- token: 'token'
348
- },
349
- region: 'us-west-2',
350
- url: 'https://awslambdatesturl.testcustomdomain.com/graphql'
351
- });
352
-
353
- execute(link, { query }).subscribe({
354
- error: (err) => {
355
- fail;
356
- },
357
- next: (data) => {
358
- done();
359
- },
360
- complete: () => {
361
- done();
418
+ execute(link, { query }, { client: mockClient }).subscribe({
419
+ error: (err) => {
420
+ fail;
421
+ },
422
+ next: (data) => {
423
+ done();
424
+ },
425
+ complete: () => {
426
+ done();
427
+ },
428
+ });
429
+ });
430
+
431
+ test("Can use a custom keepAliveTimeoutMs", (done) => {
432
+ const id = "abcd-efgh-ijkl-mnop";
433
+ uuid.mockImplementationOnce(() => id);
434
+
435
+ expect.assertions(5);
436
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
437
+ jest.fn(() => {
438
+ return "2019-11-13T18:47:04.733Z";
439
+ })
440
+ );
441
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
442
+ (url, protocol) => {
443
+ expect(url).toBe(
444
+ "wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30="
445
+ );
446
+ expect(protocol).toBe("graphql-ws");
447
+ const socket = new myWebSocket();
448
+
449
+ setTimeout(() => {
450
+ socket.close = () => {};
451
+ socket.onopen.call(socket, undefined as unknown as Event);
452
+ socket.send = (msg: string) => {
453
+ const { type } = JSON.parse(msg);
454
+
455
+ switch (type) {
456
+ case MESSAGE_TYPES.GQL_CONNECTION_INIT:
457
+ socket.onmessage.call(socket, {
458
+ data: JSON.stringify({
459
+ type: MESSAGE_TYPES.GQL_CONNECTION_ACK,
460
+ payload: {
461
+ connectionTimeoutMs: 99999,
462
+ },
463
+ }),
464
+ } as MessageEvent);
465
+ setTimeout(() => {
466
+ socket.onmessage.call(socket, {
467
+ data: JSON.stringify({
468
+ id,
469
+ type: MESSAGE_TYPES.GQL_DATA,
470
+ payload: {
471
+ data: { something: 123 },
472
+ },
473
+ }),
474
+ } as MessageEvent);
475
+ }, 100);
476
+ break;
362
477
  }
363
-
364
- });
478
+ };
479
+ }, 100);
480
+
481
+ return socket;
482
+ }
483
+ );
484
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
485
+ auth: {
486
+ type: AUTH_TYPE.API_KEY,
487
+ apiKey: "xxxxx",
488
+ },
489
+ region: "us-west-2",
490
+ url: "https://apikeytest.testcustomdomain.com/graphql",
491
+ keepAliveTimeoutMs: 123456,
365
492
  });
366
493
 
367
- test("Can use a custom keepAliveTimeoutMs", (done) => {
368
- const id = "abcd-efgh-ijkl-mnop";
369
- uuid.mockImplementationOnce(() => id);
370
-
371
- expect.assertions(5);
372
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
373
- return "2019-11-13T18:47:04.733Z";
374
- }));
375
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
376
- expect(url).toBe('wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30=');
377
- expect(protocol).toBe('graphql-ws');
378
- const socket = new myWebSocket();
379
-
380
- setTimeout(() => {
381
- socket.close = () => {};
382
- socket.onopen.call(socket, (undefined as unknown as Event));
383
- socket.send = (msg: string) => {
384
- const { type } = JSON.parse(msg);
385
-
386
- switch (type) {
387
- case MESSAGE_TYPES.GQL_CONNECTION_INIT:
388
- socket.onmessage.call(socket, {
389
- data: JSON.stringify({
390
- type: MESSAGE_TYPES.GQL_CONNECTION_ACK,
391
- payload: {
392
- connectionTimeoutMs: 99999,
393
- },
394
- })
395
- } as MessageEvent);
396
- setTimeout(() => {
397
- socket.onmessage.call(socket, {
398
- data: JSON.stringify({
399
- id,
400
- type: MESSAGE_TYPES.GQL_DATA,
401
- payload: {
402
- data: { something: 123 },
403
- },
404
- })
405
- } as MessageEvent);
406
-
407
- }, 100);
408
- break;
409
- }
410
- };
411
- }, 100);
412
-
413
- return socket;
414
- });
415
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
416
- auth: {
417
- type: AUTH_TYPE.API_KEY,
418
- apiKey: 'xxxxx'
419
- },
420
- region: 'us-west-2',
421
- url: 'https://apikeytest.testcustomdomain.com/graphql',
422
- keepAliveTimeoutMs: 123456,
423
- });
424
-
425
- expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
426
- expect((link as any).keepAliveTimeout).toBe(123456);
494
+ expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
495
+ expect((link as any).keepAliveTimeout).toBe(123456);
427
496
 
428
- const sub = execute(link, { query }).subscribe({
429
- error: (err) => {
430
- console.log(JSON.stringify(err));
431
- fail();
432
- },
433
- next: (data) => {
434
- expect((link as any).keepAliveTimeout).toBe(123456);
435
- done();
436
- sub.unsubscribe();
437
- },
438
- complete: () => {
439
- console.log('done with this');
440
- fail();
441
- }
442
-
443
- });
497
+ const sub = execute(link, { query }, { client: mockClient }).subscribe({
498
+ error: (err) => {
499
+ console.log(JSON.stringify(err));
500
+ fail();
501
+ },
502
+ next: (data) => {
503
+ expect((link as any).keepAliveTimeout).toBe(123456);
504
+ done();
505
+ sub.unsubscribe();
506
+ },
507
+ complete: () => {
508
+ console.log("done with this");
509
+ fail();
510
+ },
444
511
  });
445
-
446
- test("Uses service-provided timeout when no custom keepAliveTimeoutMs is configured", (done) => {
447
- const id = "abcd-efgh-ijkl-mnop";
448
- uuid.mockImplementationOnce(() => id);
449
-
450
- expect.assertions(5);
451
- jest.spyOn(Date.prototype, 'toISOString').mockImplementation(jest.fn(() => {
452
- return "2019-11-13T18:47:04.733Z";
453
- }));
454
- AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn((url, protocol) => {
455
- expect(url).toBe('wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30=');
456
- expect(protocol).toBe('graphql-ws');
457
- const socket = new myWebSocket();
458
-
459
- setTimeout(() => {
460
- socket.close = () => {};
461
- socket.onopen.call(socket, (undefined as unknown as Event));
462
- socket.send = (msg: string) => {
463
- const { type } = JSON.parse(msg);
464
-
465
- switch (type) {
466
- case MESSAGE_TYPES.GQL_CONNECTION_INIT:
467
- socket.onmessage.call(socket, {
468
- data: JSON.stringify({
469
- type: MESSAGE_TYPES.GQL_CONNECTION_ACK,
470
- payload: {
471
- connectionTimeoutMs: 99999,
472
- },
473
- })
474
- } as MessageEvent);
475
- setTimeout(() => {
476
- socket.onmessage.call(socket, {
477
- data: JSON.stringify({
478
- id,
479
- type: MESSAGE_TYPES.GQL_DATA,
480
- payload: {
481
- data: { something: 123 },
482
- },
483
- })
484
- } as MessageEvent);
485
-
486
- }, 100);
487
- break;
488
- }
489
- };
490
- }, 100);
491
-
492
- return socket;
493
- });
494
- const link = new AppSyncRealTimeSubscriptionHandshakeLink({
495
- auth: {
496
- type: AUTH_TYPE.API_KEY,
497
- apiKey: 'xxxxx'
498
- },
499
- region: 'us-west-2',
500
- url: 'https://apikeytest.testcustomdomain.com/graphql',
501
- });
502
-
503
- expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
504
- expect((link as any).keepAliveTimeout).toBeUndefined();
505
-
506
- const sub = execute(link, { query }).subscribe({
507
- error: (err) => {
508
- console.log(JSON.stringify(err));
509
- fail();
510
- },
511
- next: (data) => {
512
- expect((link as any).keepAliveTimeout).toBe(99999);
513
- done();
514
- sub.unsubscribe();
515
- },
516
- complete: () => {
517
- console.log('done with this');
518
- fail();
512
+ });
513
+
514
+ test("Uses service-provided timeout when no custom keepAliveTimeoutMs is configured", (done) => {
515
+ const id = "abcd-efgh-ijkl-mnop";
516
+ uuid.mockImplementationOnce(() => id);
517
+
518
+ expect.assertions(5);
519
+ jest.spyOn(Date.prototype, "toISOString").mockImplementation(
520
+ jest.fn(() => {
521
+ return "2019-11-13T18:47:04.733Z";
522
+ })
523
+ );
524
+ AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket = jest.fn(
525
+ (url, protocol) => {
526
+ expect(url).toBe(
527
+ "wss://apikeytest.testcustomdomain.com/graphql/realtime?header=eyJob3N0IjoiYXBpa2V5dGVzdC50ZXN0Y3VzdG9tZG9tYWluLmNvbSIsIngtYW16LWRhdGUiOiIyMDE5MTExM1QxODQ3MDRaIiwieC1hcGkta2V5IjoieHh4eHgifQ==&payload=e30="
528
+ );
529
+ expect(protocol).toBe("graphql-ws");
530
+ const socket = new myWebSocket();
531
+
532
+ setTimeout(() => {
533
+ socket.close = () => {};
534
+ socket.onopen.call(socket, undefined as unknown as Event);
535
+ socket.send = (msg: string) => {
536
+ const { type } = JSON.parse(msg);
537
+
538
+ switch (type) {
539
+ case MESSAGE_TYPES.GQL_CONNECTION_INIT:
540
+ socket.onmessage.call(socket, {
541
+ data: JSON.stringify({
542
+ type: MESSAGE_TYPES.GQL_CONNECTION_ACK,
543
+ payload: {
544
+ connectionTimeoutMs: 99999,
545
+ },
546
+ }),
547
+ } as MessageEvent);
548
+ setTimeout(() => {
549
+ socket.onmessage.call(socket, {
550
+ data: JSON.stringify({
551
+ id,
552
+ type: MESSAGE_TYPES.GQL_DATA,
553
+ payload: {
554
+ data: { something: 123 },
555
+ },
556
+ }),
557
+ } as MessageEvent);
558
+ }, 100);
559
+ break;
519
560
  }
520
-
521
- });
561
+ };
562
+ }, 100);
563
+
564
+ return socket;
565
+ }
566
+ );
567
+ const link = new AppSyncRealTimeSubscriptionHandshakeLink({
568
+ auth: {
569
+ type: AUTH_TYPE.API_KEY,
570
+ apiKey: "xxxxx",
571
+ },
572
+ region: "us-west-2",
573
+ url: "https://apikeytest.testcustomdomain.com/graphql",
522
574
  });
523
575
 
524
-
576
+ expect(link).toBeInstanceOf(AppSyncRealTimeSubscriptionHandshakeLink);
577
+ expect((link as any).keepAliveTimeout).toBeUndefined();
578
+
579
+ const sub = execute(link, { query }, { client: mockClient }).subscribe({
580
+ error: (err) => {
581
+ console.log(JSON.stringify(err));
582
+ fail();
583
+ },
584
+ next: (data) => {
585
+ expect((link as any).keepAliveTimeout).toBe(99999);
586
+ done();
587
+ sub.unsubscribe();
588
+ },
589
+ complete: () => {
590
+ console.log("done with this");
591
+ fail();
592
+ },
593
+ });
594
+ });
525
595
  });