22import { deepStrictEqual , ifError , ok } from 'assert' ;
33import { createServer } from 'net' ;
44
5- import { get } from 'lodash' ;
5+ import { get , isEmpty } from 'lodash' ;
66import * as shell from 'shelljs' ;
77import * as MQTT from 'aedes' ;
88
@@ -54,7 +54,16 @@ const TEST_CONTEXT: OpenFunctionContext = {
5454 } ,
5555 } ,
5656} ;
57+
5758const TEST_PAYLOAD = { data : 'hello world' } ;
59+ const TEST_CLOUD_EVENT = {
60+ specversion : '1.0' ,
61+ id : 'test-1234-1234' ,
62+ type : 'ce.openfunction' ,
63+ source : 'https://github.com/OpenFunction/functions-framework-nodejs' ,
64+ traceparent : '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01' ,
65+ data : TEST_PAYLOAD ,
66+ } ;
5867
5968describe ( 'OpenFunction - Async - Binding' , ( ) => {
6069 const APPID = 'async.dapr' ;
@@ -139,15 +148,12 @@ describe('OpenFunction - Async - Binding', () => {
139148
140149 it ( 'mqtt sub w/ pub output' , done => {
141150 const app = getAysncServer ( ( ctx , data ) => {
151+ if ( isEmpty ( data ) ) return ;
152+
142153 // Assert that user function receives correct data from input binding
143- try {
144- const recieved = JSON . parse ( data as string ) ;
145- deepStrictEqual ( recieved , TEST_PAYLOAD ) ;
146- } catch ( err ) {
147- ifError ( err ) ;
148- }
154+ deepStrictEqual ( data , TEST_PAYLOAD ) ;
149155
150- // Then write recived data to a local file
156+ // Then forward received data to output channel
151157 const output = 'mqtt_pub' ;
152158 broker . subscribe (
153159 get ( TEST_CONTEXT , `outputs.${ output } .uri` ) ,
@@ -165,15 +171,17 @@ describe('OpenFunction - Async - Binding', () => {
165171
166172 // First, we start the async server
167173 app . start ( ) . then ( ( ) => {
168- // Then, we publish a message via Dapr CLI
169- const formatted = JSON . stringify ( TEST_PAYLOAD ) . replace ( / " / g, '\\"' ) ;
170- shell . exec (
171- `dapr publish -i ${ APPID } -p ${
172- TEST_CONTEXT . inputs ! . mqtt_sub ! . componentName
173- } -t ${ TEST_CONTEXT . inputs ! . mqtt_sub . uri } -d '"${ formatted } "'`,
174+ // Then, we send a cloudevent format message to server
175+ broker . publish (
174176 {
175- silent : true ,
176- }
177+ cmd : 'publish' ,
178+ topic : TEST_CONTEXT . inputs ! . mqtt_sub . uri ! ,
179+ payload : JSON . stringify ( TEST_CLOUD_EVENT ) ,
180+ qos : 0 ,
181+ retain : false ,
182+ dup : false ,
183+ } ,
184+ err => ifError ( err )
177185 ) ;
178186 } ) ;
179187 } ) ;
0 commit comments