@@ -49,25 +49,33 @@ async function runCallableTest(test: CallTest): Promise<any> {
49
49
cors : { origin : true , methods : "POST" } ,
50
50
...test . callableOption ,
51
51
} ;
52
- const callableFunctionV1 = https . onCallHandler ( opts , ( data , context ) => {
53
- expect ( data ) . to . deep . equal ( test . expectedData ) ;
54
- return test . callableFunction ( data , context ) ;
55
- } ) ;
52
+ const callableFunctionV1 = https . onCallHandler (
53
+ opts ,
54
+ ( data , context ) => {
55
+ expect ( data ) . to . deep . equal ( test . expectedData ) ;
56
+ return test . callableFunction ( data , context ) ;
57
+ } ,
58
+ "v1"
59
+ ) ;
56
60
57
61
const responseV1 = await runHandler ( callableFunctionV1 , test . httpRequest ) ;
58
62
59
- expect ( responseV1 . body ) . to . deep . equal ( test . expectedHttpResponse . body ) ;
63
+ expect ( responseV1 . body ) . to . deep . equal ( JSON . stringify ( test . expectedHttpResponse . body ) ) ;
60
64
expect ( responseV1 . headers ) . to . deep . equal ( test . expectedHttpResponse . headers ) ;
61
65
expect ( responseV1 . status ) . to . equal ( test . expectedHttpResponse . status ) ;
62
66
63
- const callableFunctionV2 = https . onCallHandler ( opts , ( request ) => {
64
- expect ( request . data ) . to . deep . equal ( test . expectedData ) ;
65
- return test . callableFunction2 ( request ) ;
66
- } ) ;
67
+ const callableFunctionV2 = https . onCallHandler (
68
+ opts ,
69
+ ( request ) => {
70
+ expect ( request . data ) . to . deep . equal ( test . expectedData ) ;
71
+ return test . callableFunction2 ( request ) ;
72
+ } ,
73
+ "v2"
74
+ ) ;
67
75
68
76
const responseV2 = await runHandler ( callableFunctionV2 , test . httpRequest ) ;
69
77
70
- expect ( responseV2 . body ) . to . deep . equal ( test . expectedHttpResponse . body ) ;
78
+ expect ( responseV2 . body ) . to . deep . equal ( JSON . stringify ( test . expectedHttpResponse . body ) ) ;
71
79
expect ( responseV2 . headers ) . to . deep . equal ( test . expectedHttpResponse . headers ) ;
72
80
expect ( responseV2 . status ) . to . equal ( test . expectedHttpResponse . status ) ;
73
81
}
@@ -165,7 +173,7 @@ describe("onCallHandler", () => {
165
173
status : 400 ,
166
174
headers : expectedResponseHeaders ,
167
175
body : {
168
- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
176
+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
169
177
} ,
170
178
} ,
171
179
} ) ;
@@ -203,7 +211,7 @@ describe("onCallHandler", () => {
203
211
status : 400 ,
204
212
headers : expectedResponseHeaders ,
205
213
body : {
206
- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
214
+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
207
215
} ,
208
216
} ,
209
217
} ) ;
@@ -225,7 +233,7 @@ describe("onCallHandler", () => {
225
233
status : 400 ,
226
234
headers : expectedResponseHeaders ,
227
235
body : {
228
- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
236
+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
229
237
} ,
230
238
} ,
231
239
} ) ;
@@ -244,7 +252,7 @@ describe("onCallHandler", () => {
244
252
expectedHttpResponse : {
245
253
status : 500 ,
246
254
headers : expectedResponseHeaders ,
247
- body : { error : { status : "INTERNAL" , message : "INTERNAL" } } ,
255
+ body : { error : { message : "INTERNAL" , status : "INTERNAL" } } ,
248
256
} ,
249
257
} ) ;
250
258
} ) ;
@@ -262,7 +270,7 @@ describe("onCallHandler", () => {
262
270
expectedHttpResponse : {
263
271
status : 500 ,
264
272
headers : expectedResponseHeaders ,
265
- body : { error : { status : "INTERNAL" , message : "INTERNAL" } } ,
273
+ body : { error : { message : "INTERNAL" , status : "INTERNAL" } } ,
266
274
} ,
267
275
} ) ;
268
276
} ) ;
@@ -280,7 +288,7 @@ describe("onCallHandler", () => {
280
288
expectedHttpResponse : {
281
289
status : 404 ,
282
290
headers : expectedResponseHeaders ,
283
- body : { error : { status : "NOT_FOUND" , message : "i am error" } } ,
291
+ body : { error : { message : "i am error" , status : "NOT_FOUND " } } ,
284
292
} ,
285
293
} ) ;
286
294
} ) ;
@@ -364,8 +372,8 @@ describe("onCallHandler", () => {
364
372
headers : expectedResponseHeaders ,
365
373
body : {
366
374
error : {
367
- status : "UNAUTHENTICATED" ,
368
375
message : "Unauthenticated" ,
376
+ status : "UNAUTHENTICATED" ,
369
377
} ,
370
378
} ,
371
379
} ,
@@ -391,8 +399,8 @@ describe("onCallHandler", () => {
391
399
headers : expectedResponseHeaders ,
392
400
body : {
393
401
error : {
394
- status : "UNAUTHENTICATED" ,
395
402
message : "Unauthenticated" ,
403
+ status : "UNAUTHENTICATED" ,
396
404
} ,
397
405
} ,
398
406
} ,
@@ -461,8 +469,8 @@ describe("onCallHandler", () => {
461
469
headers : expectedResponseHeaders ,
462
470
body : {
463
471
error : {
464
- status : "UNAUTHENTICATED" ,
465
472
message : "Unauthenticated" ,
473
+ status : "UNAUTHENTICATED" ,
466
474
} ,
467
475
} ,
468
476
} ,
@@ -748,6 +756,57 @@ describe("onCallHandler", () => {
748
756
} ) ;
749
757
} ) ;
750
758
} ) ;
759
+
760
+ describe ( "Streaming callables" , ( ) => {
761
+ it ( "returns data in SSE format for requests Accept: text/event-stream header" , async ( ) => {
762
+ const mockReq = mockRequest (
763
+ { message : "hello streaming" } ,
764
+ "application/json" , { } ,
765
+ { accept : "text/event-stream" }
766
+ ) as any ;
767
+ const fn = https . onCallHandler (
768
+ {
769
+ cors : { origin : true , methods : "POST" } ,
770
+ } ,
771
+ ( req , resp ) => {
772
+ resp . write ( "hello" )
773
+ return 'world' ;
774
+ } ,
775
+ "v2"
776
+ ) ;
777
+
778
+ const resp = await runHandler ( fn , mockReq ) ;
779
+ const data = [
780
+ `data: {"message":"hello"}` ,
781
+ `data: {"result":"world"}` ,
782
+ ]
783
+ expect ( resp . body ) . to . equal ( [ ...data , "" ] . join ( "\n" ) ) ;
784
+ } ) ;
785
+
786
+ it ( "returns error in SSE format" , async ( ) => {
787
+ const mockReq = mockRequest (
788
+ { message : "hello streaming" } ,
789
+ "application/json" ,
790
+ { } ,
791
+ { accept : "text/event-stream" }
792
+ ) as any ;
793
+ const fn = https . onCallHandler (
794
+ {
795
+ cors : { origin : true , methods : "POST" } ,
796
+ } ,
797
+ ( req , resp ) => {
798
+ throw new Error ( "BOOM" )
799
+ } ,
800
+ "v2"
801
+ ) ;
802
+
803
+ const resp = await runHandler ( fn , mockReq ) ;
804
+ const data = [
805
+ `data: {"error":{"message":"INTERNAL","status":"INTERNAL"}}` ,
806
+ ]
807
+ expect ( resp . body ) . to . equal ( [ ...data , "" ] . join ( "\n" ) ) ;
808
+ } ) ;
809
+ } ) ;
751
810
} ) ;
752
811
753
812
describe ( "encoding/decoding" , ( ) => {
0 commit comments