@@ -93,24 +93,38 @@ describe('OpenWhiskInvoke', () => {
9393 } ) ;
9494 } ) ;
9595
96- it ( 'it should throw if file is not parsed as JSON object' , ( ) => {
96+ it ( 'it should parse stdin as JSON data without explicit options' , ( ) => {
97+ const data = '{"hello": "world"}' ;
98+ sinon . stub ( openwhiskInvoke , 'getStdin' ) . returns ( BbPromise . resolve ( data ) ) ;
99+
100+ serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
101+ return openwhiskInvoke . validate ( ) . then ( ( ) => {
102+ expect ( openwhiskInvoke . options . data ) . to . deep . equal ( { hello : "world" } ) ;
103+ openwhiskInvoke . options . data = null ;
104+ } ) ;
105+ } ) ;
106+
107+ it ( 'it should throw if file is not parsed as JSON object (invalid)' , ( ) => {
97108 serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
98- const data = {
99- testProp : 'testValue' ,
100- } ;
101109 openwhiskInvoke . options . data = '{"hello": "world"' ;
102- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
110+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( 'Error parsing' )
111+ } ) ;
112+
113+ it ( 'it should throw if file is not parsed as JSON object (number)' , ( ) => {
114+ serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
103115 openwhiskInvoke . options . data = '1' ;
104- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
116+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( ' Error parsing' )
105117 } ) ;
106118
107119 it ( 'it should parse file if file path is provided' , ( ) => {
108120 serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
109121 const data = {
110122 testProp : 'testValue' ,
111123 } ;
112- openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true , readFileSync : ( ) => data } ;
124+ openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true } ;
125+ openwhiskInvoke . readFileSync = ( ) => JSON . stringify ( data ) ;
113126 openwhiskInvoke . options . path = 'data.json' ;
127+ openwhiskInvoke . options . data = null ;
114128
115129 return openwhiskInvoke . validate ( ) . then ( ( ) => {
116130 expect ( openwhiskInvoke . options . data ) . to . deep . equal ( data ) ;
@@ -121,40 +135,40 @@ describe('OpenWhiskInvoke', () => {
121135
122136 it ( 'it should throw if file is not parsed as JSON object' , ( ) => {
123137 serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
124- openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true , readFileSync : ( ) => 'testing' } ;
138+ openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true } ;
125139 openwhiskInvoke . options . path = 'data.txt' ;
140+ openwhiskInvoke . readFileSync = ( ) => 'testing' ;
126141
127- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
142+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( ' Error parsing' )
128143 } ) ;
129144
130145 it ( 'it should throw if type parameter is not valid value' , ( ) => {
131146 openwhiskInvoke . options . type = 'random' ;
132147 openwhiskInvoke . options . path = null ;
133148 openwhiskInvoke . options . data = null ;
134- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( 'blocking or nonblocking' ) ;
149+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( 'blocking or nonblocking' )
135150 } ) ;
136151
137152 it ( 'it should throw if log parameter is not valid value' , ( ) => {
138153 openwhiskInvoke . options . type = 'blocking' ;
139154 openwhiskInvoke . options . log = 'random' ;
140155 openwhiskInvoke . options . path = null ;
141- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( 'result or response' ) ;
156+ openwhiskInvoke . options . data = '{}' ;
157+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( 'result or response' )
142158 } ) ;
143159
144160 it ( 'it should throw error if service path is not set' , ( ) => {
145161 serverless . config . servicePath = false ;
146162 expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
147- serverless . config . servicePath = true ;
148163 } ) ;
149164
150165 it ( 'it should throw error if file path does not exist' , ( ) => {
151166 serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
167+ openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => false } ;
152168 openwhiskInvoke . options . path = 'some/path' ;
169+ openwhiskInvoke . options . data = null ;
153170
154- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
155-
156- openwhiskInvoke . options . path = false ;
157- serverless . config . servicePath = true ;
171+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( 'does not exist' )
158172 } ) ;
159173 } ) ;
160174
0 commit comments