@@ -20,161 +20,177 @@ Deno.test("app.ws() - throws 400 on non-WebSocket request", async () => {
2020 expect ( res . status ) . toEqual ( 400 ) ;
2121} ) ;
2222
23- Deno . test ( {
24- name : "ctx.upgrade() - managed echo" ,
25- sanitizeOps : false ,
26- sanitizeResources : false ,
27- async fn ( ) {
28- const app = new App ( )
29- . get ( "/ws" , ( ctx ) =>
30- ctx . upgrade ( {
31- message ( socket , event ) {
32- socket . send ( `echo: ${ event . data } ` ) ;
33- } ,
34- } ) ) ;
35-
36- const ac = new AbortController ( ) ;
37- const server = Deno . serve ( {
38- hostname : "127.0.0.1" ,
39- port : 0 ,
40- signal : ac . signal ,
41- onListen : ( ) => { } ,
42- } , app . handler ( ) ) ;
43-
44- const port = server . addr . port ;
45-
46- const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
47- const received = new Promise < string > ( ( resolve , reject ) => {
48- ws . onmessage = ( e ) => resolve ( e . data ) ;
49- ws . onerror = ( e ) => reject ( e ) ;
23+ Deno . test ( "ctx.upgrade() - bare mode with options" , async ( ) => {
24+ const app = new App ( )
25+ . get ( "/ws" , ( ctx ) => {
26+ // Options-only call must return { socket, response } (bare mode),
27+ // NOT a plain Response (managed mode).
28+ const { socket, response } = ctx . upgrade ( { protocol : "graphql-ws" } ) ;
29+ socket . onmessage = ( e ) => socket . send ( `bare-opts: ${ e . data } ` ) ;
30+ return response ;
5031 } ) ;
51- ws . onopen = ( ) => ws . send ( "hello" ) ;
5232
53- const msg = await received ;
54- expect ( msg ) . toEqual ( "echo: hello" ) ;
33+ const ac = new AbortController ( ) ;
34+ const server = Deno . serve ( {
35+ hostname : "127.0.0.1" ,
36+ port : 0 ,
37+ signal : ac . signal ,
38+ onListen : ( ) => { } ,
39+ } , app . handler ( ) ) ;
40+
41+ const port = server . addr . port ;
42+
43+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` , "graphql-ws" ) ;
44+ const received = new Promise < string > ( ( resolve , reject ) => {
45+ ws . onmessage = ( e ) => resolve ( e . data ) ;
46+ ws . onerror = ( e ) => reject ( e ) ;
47+ } ) ;
48+ ws . onopen = ( ) => ws . send ( "hello" ) ;
49+
50+ const msg = await received ;
51+ expect ( msg ) . toEqual ( "bare-opts: hello" ) ;
52+ expect ( ws . protocol ) . toEqual ( "graphql-ws" ) ;
53+
54+ ws . close ( ) ;
55+ ac . abort ( ) ;
56+ await server . finished ;
57+ } ) ;
5558
56- ws . close ( ) ;
57- ac . abort ( ) ;
58- await server . finished ;
59- } ,
59+ Deno . test ( "ctx.upgrade() - managed echo" , async ( ) => {
60+ const app = new App ( )
61+ . get ( "/ws" , ( ctx ) =>
62+ ctx . upgrade ( {
63+ message ( socket , event ) {
64+ socket . send ( `echo: ${ event . data } ` ) ;
65+ } ,
66+ } ) ) ;
67+
68+ const ac = new AbortController ( ) ;
69+ const server = Deno . serve ( {
70+ hostname : "127.0.0.1" ,
71+ port : 0 ,
72+ signal : ac . signal ,
73+ onListen : ( ) => { } ,
74+ } , app . handler ( ) ) ;
75+
76+ const port = server . addr . port ;
77+
78+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
79+ const received = new Promise < string > ( ( resolve , reject ) => {
80+ ws . onmessage = ( e ) => resolve ( e . data ) ;
81+ ws . onerror = ( e ) => reject ( e ) ;
82+ } ) ;
83+ ws . onopen = ( ) => ws . send ( "hello" ) ;
84+
85+ const msg = await received ;
86+ expect ( msg ) . toEqual ( "echo: hello" ) ;
87+
88+ ws . close ( ) ;
89+ ac . abort ( ) ;
90+ await server . finished ;
6091} ) ;
6192
62- Deno . test ( {
63- name : "ctx.upgrade() - bare overload" ,
64- sanitizeOps : false ,
65- sanitizeResources : false ,
66- async fn ( ) {
67- const app = new App ( )
68- . get ( "/ws" , ( ctx ) => {
69- const { socket, response } = ctx . upgrade ( ) ;
70- socket . onmessage = ( e ) => socket . send ( `bare: ${ e . data } ` ) ;
71- return response ;
72- } ) ;
73-
74- const ac = new AbortController ( ) ;
75- const server = Deno . serve ( {
76- hostname : "127.0.0.1" ,
77- port : 0 ,
78- signal : ac . signal ,
79- onListen : ( ) => { } ,
80- } , app . handler ( ) ) ;
81-
82- const port = server . addr . port ;
83-
84- const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
85- const received = new Promise < string > ( ( resolve , reject ) => {
86- ws . onmessage = ( e ) => resolve ( e . data ) ;
87- ws . onerror = ( e ) => reject ( e ) ;
93+ Deno . test ( "ctx.upgrade() - bare overload" , async ( ) => {
94+ const app = new App ( )
95+ . get ( "/ws" , ( ctx ) => {
96+ const { socket, response } = ctx . upgrade ( ) ;
97+ socket . onmessage = ( e ) => socket . send ( `bare: ${ e . data } ` ) ;
98+ return response ;
8899 } ) ;
89- ws . onopen = ( ) => ws . send ( "hello" ) ;
90100
91- const msg = await received ;
92- expect ( msg ) . toEqual ( "bare: hello" ) ;
101+ const ac = new AbortController ( ) ;
102+ const server = Deno . serve ( {
103+ hostname : "127.0.0.1" ,
104+ port : 0 ,
105+ signal : ac . signal ,
106+ onListen : ( ) => { } ,
107+ } , app . handler ( ) ) ;
108+
109+ const port = server . addr . port ;
110+
111+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
112+ const received = new Promise < string > ( ( resolve , reject ) => {
113+ ws . onmessage = ( e ) => resolve ( e . data ) ;
114+ ws . onerror = ( e ) => reject ( e ) ;
115+ } ) ;
116+ ws . onopen = ( ) => ws . send ( "hello" ) ;
117+
118+ const msg = await received ;
119+ expect ( msg ) . toEqual ( "bare: hello" ) ;
120+
121+ ws . close ( ) ;
122+ ac . abort ( ) ;
123+ await server . finished ;
124+ } ) ;
93125
94- ws . close ( ) ;
95- ac . abort ( ) ;
96- await server . finished ;
97- } ,
126+ Deno . test ( "app.ws() - echo shorthand" , async ( ) => {
127+ const app = new App ( )
128+ . ws ( "/ws" , {
129+ message ( socket , event ) {
130+ socket . send ( `ws: ${ event . data } ` ) ;
131+ } ,
132+ } ) ;
133+
134+ const ac = new AbortController ( ) ;
135+ const server = Deno . serve ( {
136+ hostname : "127.0.0.1" ,
137+ port : 0 ,
138+ signal : ac . signal ,
139+ onListen : ( ) => { } ,
140+ } , app . handler ( ) ) ;
141+
142+ const port = server . addr . port ;
143+
144+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
145+ const received = new Promise < string > ( ( resolve , reject ) => {
146+ ws . onmessage = ( e ) => resolve ( e . data ) ;
147+ ws . onerror = ( e ) => reject ( e ) ;
148+ } ) ;
149+ ws . onopen = ( ) => ws . send ( "hello" ) ;
150+
151+ const msg = await received ;
152+ expect ( msg ) . toEqual ( "ws: hello" ) ;
153+
154+ ws . close ( ) ;
155+ ac . abort ( ) ;
156+ await server . finished ;
98157} ) ;
99158
100- Deno . test ( {
101- name : "app.ws() - echo shorthand" ,
102- sanitizeOps : false ,
103- sanitizeResources : false ,
104- async fn ( ) {
105- const app = new App ( )
106- . ws ( "/ws" , {
107- message ( socket , event ) {
108- socket . send ( `ws: ${ event . data } ` ) ;
159+ Deno . test ( "ctx.upgrade() - open and close handlers fire" , async ( ) => {
160+ const events : string [ ] = [ ] ;
161+ const closed = Promise . withResolvers < void > ( ) ;
162+
163+ const app = new App ( )
164+ . get ( "/ws" , ( ctx ) =>
165+ ctx . upgrade ( {
166+ open ( ) {
167+ events . push ( "open" ) ;
109168 } ,
110- } ) ;
111-
112- const ac = new AbortController ( ) ;
113- const server = Deno . serve ( {
114- hostname : "127.0.0.1" ,
115- port : 0 ,
116- signal : ac . signal ,
117- onListen : ( ) => { } ,
118- } , app . handler ( ) ) ;
119-
120- const port = server . addr . port ;
121-
122- const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
123- const received = new Promise < string > ( ( resolve , reject ) => {
124- ws . onmessage = ( e ) => resolve ( e . data ) ;
125- ws . onerror = ( e ) => reject ( e ) ;
126- } ) ;
127- ws . onopen = ( ) => ws . send ( "hello" ) ;
169+ close ( ) {
170+ events . push ( "close" ) ;
171+ closed . resolve ( ) ;
172+ } ,
173+ } ) ) ;
128174
129- const msg = await received ;
130- expect ( msg ) . toEqual ( "ws: hello" ) ;
175+ const ac = new AbortController ( ) ;
176+ const server = Deno . serve ( {
177+ hostname : "127.0.0.1" ,
178+ port : 0 ,
179+ signal : ac . signal ,
180+ onListen : ( ) => { } ,
181+ } , app . handler ( ) ) ;
131182
132- ws . close ( ) ;
133- ac . abort ( ) ;
134- await server . finished ;
135- } ,
136- } ) ;
183+ const port = server . addr . port ;
137184
138- Deno . test ( {
139- name : "ctx.upgrade() - open and close handlers fire" ,
140- sanitizeOps : false ,
141- sanitizeResources : false ,
142- async fn ( ) {
143- const events : string [ ] = [ ] ;
144- const closed = Promise . withResolvers < void > ( ) ;
145-
146- const app = new App ( )
147- . get ( "/ws" , ( ctx ) =>
148- ctx . upgrade ( {
149- open ( ) {
150- events . push ( "open" ) ;
151- } ,
152- close ( ) {
153- events . push ( "close" ) ;
154- closed . resolve ( ) ;
155- } ,
156- } ) ) ;
157-
158- const ac = new AbortController ( ) ;
159- const server = Deno . serve ( {
160- hostname : "127.0.0.1" ,
161- port : 0 ,
162- signal : ac . signal ,
163- onListen : ( ) => { } ,
164- } , app . handler ( ) ) ;
165-
166- const port = server . addr . port ;
167-
168- const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
169- await new Promise < void > ( ( resolve ) => {
170- ws . onopen = ( ) => resolve ( ) ;
171- } ) ;
172- ws . close ( ) ;
185+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /ws` ) ;
186+ await new Promise < void > ( ( resolve ) => {
187+ ws . onopen = ( ) => resolve ( ) ;
188+ } ) ;
189+ ws . close ( ) ;
173190
174- await closed . promise ;
175- expect ( events ) . toEqual ( [ "open" , "close" ] ) ;
191+ await closed . promise ;
192+ expect ( events ) . toEqual ( [ "open" , "close" ] ) ;
176193
177- ac . abort ( ) ;
178- await server . finished ;
179- } ,
194+ ac . abort ( ) ;
195+ await server . finished ;
180196} ) ;
0 commit comments