@@ -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
+ "gcfv1"
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
+ "gcfv2"
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,53 @@ 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
+ { } ,
766
+ { accept : "text/event-stream" }
767
+ ) as any ;
768
+ const fn = https . onCallHandler (
769
+ {
770
+ cors : { origin : true , methods : "POST" } ,
771
+ } ,
772
+ ( req , resp ) => {
773
+ resp . write ( "hello" ) ;
774
+ return "world" ;
775
+ } ,
776
+ "gcfv2"
777
+ ) ;
778
+
779
+ const resp = await runHandler ( fn , mockReq ) ;
780
+ const data = [ `data: {"message":"hello"}` , `data: {"result":"world"}` ] ;
781
+ expect ( resp . body ) . to . equal ( [ ...data , "" ] . join ( "\n" ) ) ;
782
+ } ) ;
783
+
784
+ it ( "returns error in SSE format" , async ( ) => {
785
+ const mockReq = mockRequest (
786
+ { message : "hello streaming" } ,
787
+ "application/json" ,
788
+ { } ,
789
+ { accept : "text/event-stream" }
790
+ ) as any ;
791
+ const fn = https . onCallHandler (
792
+ {
793
+ cors : { origin : true , methods : "POST" } ,
794
+ } ,
795
+ ( ) => {
796
+ throw new Error ( "BOOM" ) ;
797
+ } ,
798
+ "gcfv2"
799
+ ) ;
800
+
801
+ const resp = await runHandler ( fn , mockReq ) ;
802
+ const data = [ `data: {"error":{"message":"INTERNAL","status":"INTERNAL"}}` ] ;
803
+ expect ( resp . body ) . to . equal ( [ ...data , "" ] . join ( "\n" ) ) ;
804
+ } ) ;
805
+ } ) ;
751
806
} ) ;
752
807
753
808
describe ( "encoding/decoding" , ( ) => {
0 commit comments