@@ -49,25 +49,33 @@ async function runCallableTest(test: CallTest): Promise<any> {
4949 cors : { origin : true , methods : "POST" } ,
5050 ...test . callableOption ,
5151 } ;
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+ ) ;
5660
5761 const responseV1 = await runHandler ( callableFunctionV1 , test . httpRequest ) ;
5862
59- expect ( responseV1 . body ) . to . deep . equal ( test . expectedHttpResponse . body ) ;
63+ expect ( responseV1 . body ) . to . deep . equal ( JSON . stringify ( test . expectedHttpResponse . body ) ) ;
6064 expect ( responseV1 . headers ) . to . deep . equal ( test . expectedHttpResponse . headers ) ;
6165 expect ( responseV1 . status ) . to . equal ( test . expectedHttpResponse . status ) ;
6266
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+ ) ;
6775
6876 const responseV2 = await runHandler ( callableFunctionV2 , test . httpRequest ) ;
6977
70- expect ( responseV2 . body ) . to . deep . equal ( test . expectedHttpResponse . body ) ;
78+ expect ( responseV2 . body ) . to . deep . equal ( JSON . stringify ( test . expectedHttpResponse . body ) ) ;
7179 expect ( responseV2 . headers ) . to . deep . equal ( test . expectedHttpResponse . headers ) ;
7280 expect ( responseV2 . status ) . to . equal ( test . expectedHttpResponse . status ) ;
7381}
@@ -165,7 +173,7 @@ describe("onCallHandler", () => {
165173 status : 400 ,
166174 headers : expectedResponseHeaders ,
167175 body : {
168- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
176+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
169177 } ,
170178 } ,
171179 } ) ;
@@ -203,7 +211,7 @@ describe("onCallHandler", () => {
203211 status : 400 ,
204212 headers : expectedResponseHeaders ,
205213 body : {
206- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
214+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
207215 } ,
208216 } ,
209217 } ) ;
@@ -225,7 +233,7 @@ describe("onCallHandler", () => {
225233 status : 400 ,
226234 headers : expectedResponseHeaders ,
227235 body : {
228- error : { status : "INVALID_ARGUMENT " , message : "Bad Request " } ,
236+ error : { message : "Bad Request " , status : "INVALID_ARGUMENT " } ,
229237 } ,
230238 } ,
231239 } ) ;
@@ -244,7 +252,7 @@ describe("onCallHandler", () => {
244252 expectedHttpResponse : {
245253 status : 500 ,
246254 headers : expectedResponseHeaders ,
247- body : { error : { status : "INTERNAL" , message : "INTERNAL" } } ,
255+ body : { error : { message : "INTERNAL" , status : "INTERNAL" } } ,
248256 } ,
249257 } ) ;
250258 } ) ;
@@ -262,7 +270,7 @@ describe("onCallHandler", () => {
262270 expectedHttpResponse : {
263271 status : 500 ,
264272 headers : expectedResponseHeaders ,
265- body : { error : { status : "INTERNAL" , message : "INTERNAL" } } ,
273+ body : { error : { message : "INTERNAL" , status : "INTERNAL" } } ,
266274 } ,
267275 } ) ;
268276 } ) ;
@@ -280,7 +288,7 @@ describe("onCallHandler", () => {
280288 expectedHttpResponse : {
281289 status : 404 ,
282290 headers : expectedResponseHeaders ,
283- body : { error : { status : "NOT_FOUND" , message : "i am error" } } ,
291+ body : { error : { message : "i am error" , status : "NOT_FOUND " } } ,
284292 } ,
285293 } ) ;
286294 } ) ;
@@ -364,8 +372,8 @@ describe("onCallHandler", () => {
364372 headers : expectedResponseHeaders ,
365373 body : {
366374 error : {
367- status : "UNAUTHENTICATED" ,
368375 message : "Unauthenticated" ,
376+ status : "UNAUTHENTICATED" ,
369377 } ,
370378 } ,
371379 } ,
@@ -391,8 +399,8 @@ describe("onCallHandler", () => {
391399 headers : expectedResponseHeaders ,
392400 body : {
393401 error : {
394- status : "UNAUTHENTICATED" ,
395402 message : "Unauthenticated" ,
403+ status : "UNAUTHENTICATED" ,
396404 } ,
397405 } ,
398406 } ,
@@ -461,8 +469,8 @@ describe("onCallHandler", () => {
461469 headers : expectedResponseHeaders ,
462470 body : {
463471 error : {
464- status : "UNAUTHENTICATED" ,
465472 message : "Unauthenticated" ,
473+ status : "UNAUTHENTICATED" ,
466474 } ,
467475 } ,
468476 } ,
@@ -748,6 +756,53 @@ describe("onCallHandler", () => {
748756 } ) ;
749757 } ) ;
750758 } ) ;
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+ } ) ;
751806} ) ;
752807
753808describe ( "encoding/decoding" , ( ) => {
0 commit comments