@@ -4102,3 +4102,213 @@ describe('sendEmail', () => {
4102
4102
) ;
4103
4103
} ) ;
4104
4104
} ) ;
4105
+
4106
+ describe ( 'custom HTTP codes' , ( ) => {
4107
+ it ( 'should set custom statusCode in save hook' , async ( ) => {
4108
+ Parse . Cloud . beforeSave ( 'TestObject' , ( req , res ) => {
4109
+ res . status ( 201 ) ;
4110
+ } ) ;
4111
+
4112
+ const request = await fetch ( 'http://localhost:8378/1/classes/TestObject' , {
4113
+ method : 'POST' ,
4114
+ headers : {
4115
+ 'X-Parse-Application-Id' : 'test' ,
4116
+ 'X-Parse-REST-API-Key' : 'rest' ,
4117
+ }
4118
+ } ) ;
4119
+
4120
+ expect ( request . status ) . toBe ( 201 ) ;
4121
+ } ) ;
4122
+
4123
+ it ( 'should set custom headers in save hook' , async ( ) => {
4124
+ Parse . Cloud . beforeSave ( 'TestObject' , ( req , res ) => {
4125
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4126
+ } ) ;
4127
+
4128
+ const request = await fetch ( 'http://localhost:8378/1/classes/TestObject' , {
4129
+ method : 'POST' ,
4130
+ headers : {
4131
+ 'X-Parse-Application-Id' : 'test' ,
4132
+ 'X-Parse-REST-API-Key' : 'rest' ,
4133
+ }
4134
+ } ) ;
4135
+
4136
+ expect ( request . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4137
+ } ) ;
4138
+
4139
+ it ( 'should set custom statusCode in delete hook' , async ( ) => {
4140
+ Parse . Cloud . beforeDelete ( 'TestObject' , ( req , res ) => {
4141
+ res . status ( 201 ) ;
4142
+ return true
4143
+ } ) ;
4144
+
4145
+ const obj = new Parse . Object ( 'TestObject' ) ;
4146
+ await obj . save ( ) ;
4147
+
4148
+ const request = await fetch ( `http://localhost:8378/1/classes/TestObject/${ obj . id } ` , {
4149
+ method : 'DELETE' ,
4150
+ headers : {
4151
+ 'X-Parse-Application-Id' : 'test' ,
4152
+ 'X-Parse-REST-API-Key' : 'rest' ,
4153
+ }
4154
+ } ) ;
4155
+
4156
+ expect ( request . status ) . toBe ( 201 ) ;
4157
+ } ) ;
4158
+
4159
+ it ( 'should set custom headers in delete hook' , async ( ) => {
4160
+ Parse . Cloud . beforeDelete ( 'TestObject' , ( req , res ) => {
4161
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4162
+ } ) ;
4163
+
4164
+ const obj = new TestObject ( ) ;
4165
+ await obj . save ( ) ;
4166
+ const request = await fetch ( `http://localhost:8378/1/classes/TestObject/${ obj . id } ` , {
4167
+ method : 'DELETE' ,
4168
+ headers : {
4169
+ 'X-Parse-Application-Id' : 'test' ,
4170
+ 'X-Parse-REST-API-Key' : 'rest' ,
4171
+ }
4172
+ } ) ;
4173
+
4174
+ expect ( request . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4175
+ } ) ;
4176
+
4177
+ it ( 'should set custom statusCode in find hook' , async ( ) => {
4178
+ Parse . Cloud . beforeFind ( 'TestObject' , ( req , res ) => {
4179
+ res . status ( 201 ) ;
4180
+ } ) ;
4181
+
4182
+ const request = await fetch ( 'http://localhost:8378/1/classes/TestObject' , {
4183
+ headers : {
4184
+ 'X-Parse-Application-Id' : 'test' ,
4185
+ 'X-Parse-REST-API-Key' : 'rest' ,
4186
+ }
4187
+ } ) ;
4188
+
4189
+ expect ( request . status ) . toBe ( 201 ) ;
4190
+ } ) ;
4191
+
4192
+ it ( 'should set custom headers in find hook' , async ( ) => {
4193
+ Parse . Cloud . beforeFind ( 'TestObject' , ( req , res ) => {
4194
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4195
+ } ) ;
4196
+
4197
+ const request = await fetch ( 'http://localhost:8378/1/classes/TestObject' , {
4198
+ headers : {
4199
+ 'X-Parse-Application-Id' : 'test' ,
4200
+ 'X-Parse-REST-API-Key' : 'rest' ,
4201
+ }
4202
+ } ) ;
4203
+
4204
+ expect ( request . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4205
+ } ) ;
4206
+
4207
+ it ( 'should set custom statusCode in cloud function' , async ( ) => {
4208
+ Parse . Cloud . define ( 'customStatusCode' , ( req , res ) => {
4209
+ res . status ( 201 ) ;
4210
+ return true ;
4211
+ } ) ;
4212
+
4213
+ const response = await fetch ( 'http://localhost:8378/1/functions/customStatusCode' , {
4214
+ method : 'POST' ,
4215
+ headers : {
4216
+ 'X-Parse-Application-Id' : 'test' ,
4217
+ 'X-Parse-REST-API-Key' : 'rest' ,
4218
+ }
4219
+ } ) ;
4220
+
4221
+ expect ( response . status ) . toBe ( 201 ) ;
4222
+ } ) ;
4223
+
4224
+ it ( 'should set custom headers in cloud function' , async ( ) => {
4225
+ Parse . Cloud . define ( 'customHeaders' , ( req , res ) => {
4226
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4227
+ return true ;
4228
+ } ) ;
4229
+
4230
+ const response = await fetch ( 'http://localhost:8378/1/functions/customHeaders' , {
4231
+ method : 'POST' ,
4232
+ headers : {
4233
+ 'X-Parse-Application-Id' : 'test' ,
4234
+ 'X-Parse-REST-API-Key' : 'rest' ,
4235
+ }
4236
+ } ) ;
4237
+
4238
+ expect ( response . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4239
+ } ) ;
4240
+
4241
+ it ( 'should set custom statusCode in beforeLogin hook' , async ( ) => {
4242
+ Parse . Cloud . beforeLogin ( ( req , res ) => {
4243
+ res . status ( 201 ) ;
4244
+ } ) ;
4245
+
4246
+ await Parse . User . signUp ( '[email protected] ' , 'password' ) ;
4247
+ const response = await fetch ( 'http://localhost:8378/1/login' , {
4248
+ method : 'POST' ,
4249
+ headers : {
4250
+ 'X-Parse-Application-Id' : 'test' ,
4251
+ 'X-Parse-REST-API-Key' : 'rest' ,
4252
+ } ,
4253
+ body :
JSON . stringify ( { username :
'[email protected] ' , password :
'password' } )
4254
+ } ) ;
4255
+
4256
+ expect ( response . status ) . toBe ( 201 ) ;
4257
+ } ) ;
4258
+
4259
+ it ( 'should set custom headers in beforeLogin hook' , async ( ) => {
4260
+ Parse . Cloud . beforeLogin ( ( req , res ) => {
4261
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4262
+ } ) ;
4263
+
4264
+ await Parse . User . signUp ( '[email protected] ' , 'password' ) ;
4265
+ const response = await fetch ( 'http://localhost:8378/1/login' , {
4266
+ method : 'POST' ,
4267
+ headers : {
4268
+ 'X-Parse-Application-Id' : 'test' ,
4269
+ 'X-Parse-REST-API-Key' : 'rest' ,
4270
+ } ,
4271
+ body :
JSON . stringify ( { username :
'[email protected] ' , password :
'password' } )
4272
+ } ) ;
4273
+
4274
+ expect ( response . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4275
+ } ) ;
4276
+
4277
+ it ( 'should set custom statusCode in file trigger' , async ( ) => {
4278
+ Parse . Cloud . beforeSave ( Parse . File , ( req , res ) => {
4279
+ res . status ( 201 ) ;
4280
+ } ) ;
4281
+
4282
+ const file = new Parse . File ( 'test.txt' , [ 1 , 2 , 3 ] ) ;
4283
+ const response = await fetch ( 'http://localhost:8378/1/files/test.txt' , {
4284
+ method : 'POST' ,
4285
+ headers : {
4286
+ 'X-Parse-Application-Id' : 'test' ,
4287
+ 'X-Parse-REST-API-Key' : 'rest' ,
4288
+ 'Content-Type' : 'text/plain' ,
4289
+ } ,
4290
+ body : file . getData ( )
4291
+ } ) ;
4292
+
4293
+ expect ( response . status ) . toBe ( 201 ) ;
4294
+ } ) ;
4295
+
4296
+ it ( 'should set custom headers in file trigger' , async ( ) => {
4297
+ Parse . Cloud . beforeSave ( Parse . File , ( req , res ) => {
4298
+ res . setHeader ( 'X-Custom-Header' , 'custom-value' ) ;
4299
+ } ) ;
4300
+
4301
+ const file = new Parse . File ( 'test.txt' , [ 1 , 2 , 3 ] ) ;
4302
+ const response = await fetch ( 'http://localhost:8378/1/files/test.txt' , {
4303
+ method : 'POST' ,
4304
+ headers : {
4305
+ 'X-Parse-Application-Id' : 'test' ,
4306
+ 'X-Parse-REST-API-Key' : 'rest' ,
4307
+ 'Content-Type' : 'text/plain' ,
4308
+ } ,
4309
+ body : file . getData ( )
4310
+ } ) ;
4311
+
4312
+ expect ( response . headers . get ( 'X-Custom-Header' ) ) . toBe ( 'custom-value' ) ;
4313
+ } ) ;
4314
+ } )
0 commit comments