@@ -93,24 +93,38 @@ describe('OpenWhiskInvoke', () => {
93
93
} ) ;
94
94
} ) ;
95
95
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)' , ( ) => {
97
108
serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
98
- const data = {
99
- testProp : 'testValue' ,
100
- } ;
101
109
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 ( ) ) ;
103
115
openwhiskInvoke . options . data = '1' ;
104
- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
116
+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( ' Error parsing' )
105
117
} ) ;
106
118
107
119
it ( 'it should parse file if file path is provided' , ( ) => {
108
120
serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
109
121
const data = {
110
122
testProp : 'testValue' ,
111
123
} ;
112
- openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true , readFileSync : ( ) => data } ;
124
+ openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => true } ;
125
+ openwhiskInvoke . readFileSync = ( ) => JSON . stringify ( data ) ;
113
126
openwhiskInvoke . options . path = 'data.json' ;
127
+ openwhiskInvoke . options . data = null ;
114
128
115
129
return openwhiskInvoke . validate ( ) . then ( ( ) => {
116
130
expect ( openwhiskInvoke . options . data ) . to . deep . equal ( data ) ;
@@ -121,40 +135,40 @@ describe('OpenWhiskInvoke', () => {
121
135
122
136
it ( 'it should throw if file is not parsed as JSON object' , ( ) => {
123
137
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 } ;
125
139
openwhiskInvoke . options . path = 'data.txt' ;
140
+ openwhiskInvoke . readFileSync = ( ) => 'testing' ;
126
141
127
- expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
142
+ return expect ( openwhiskInvoke . validate ( ) ) . to . eventually . be . rejectedWith ( ' Error parsing' )
128
143
} ) ;
129
144
130
145
it ( 'it should throw if type parameter is not valid value' , ( ) => {
131
146
openwhiskInvoke . options . type = 'random' ;
132
147
openwhiskInvoke . options . path = null ;
133
148
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' )
135
150
} ) ;
136
151
137
152
it ( 'it should throw if log parameter is not valid value' , ( ) => {
138
153
openwhiskInvoke . options . type = 'blocking' ;
139
154
openwhiskInvoke . options . log = 'random' ;
140
155
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' )
142
158
} ) ;
143
159
144
160
it ( 'it should throw error if service path is not set' , ( ) => {
145
161
serverless . config . servicePath = false ;
146
162
expect ( ( ) => openwhiskInvoke . validate ( ) ) . to . throw ( Error ) ;
147
- serverless . config . servicePath = true ;
148
163
} ) ;
149
164
150
165
it ( 'it should throw error if file path does not exist' , ( ) => {
151
166
serverless . config . servicePath = path . join ( os . tmpdir ( ) , ( new Date ) . getTime ( ) . toString ( ) ) ;
167
+ openwhiskInvoke . serverless . utils = { fileExistsSync : ( ) => false } ;
152
168
openwhiskInvoke . options . path = 'some/path' ;
169
+ openwhiskInvoke . options . data = null ;
153
170
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' )
158
172
} ) ;
159
173
} ) ;
160
174
0 commit comments