22
33var request = require ( 'supertest' )
44var loopback = require ( 'loopback' )
5+ var _ = require ( 'lodash' )
56var expect = require ( 'chai' ) . expect
67var JSONAPIComponent = require ( '../' )
78var app
@@ -24,7 +25,7 @@ describe('disabling loopback-component-jsonapi error handler', function () {
2425 request ( app ) . get ( '/posts/100' ) . end ( function ( err , res ) {
2526 expect ( err ) . to . equal ( null )
2627 expect ( res . body ) . to . have . keys ( 'error' )
27- expect ( res . body . error ) . to . have . keys ( 'name' , 'message' , 'statusCode' )
28+ expect ( res . body . error ) . to . have . keys ( 'name' , 'message' , 'statusCode' , 'code' )
2829 done ( )
2930 } )
3031 } )
@@ -87,7 +88,7 @@ describe('loopback json api errors', function () {
8788 status : 404 ,
8889 code : 'MODEL_NOT_FOUND' ,
8990 detail : 'Unknown "post" id "100".' ,
90- source : '' ,
91+ source : { } ,
9192 title : 'Error'
9293 } )
9394 done ( )
@@ -129,7 +130,7 @@ describe('loopback json api errors', function () {
129130 status : 422 ,
130131 code : 'presence' ,
131132 detail : 'JSON API resource object must contain `data.type` property' ,
132- source : '' ,
133+ source : { } ,
133134 title : 'ValidationError'
134135 } )
135136 done ( )
@@ -178,3 +179,49 @@ describe('loopback json api errors', function () {
178179 )
179180 } )
180181} )
182+
183+ describe ( 'loopback json api errors with `errorStackInResponse` enabled' , function ( ) {
184+ beforeEach ( function ( ) {
185+ app = loopback ( )
186+ app . set ( 'legacyExplorer' , false )
187+ var ds = loopback . createDataSource ( 'memory' )
188+ Post = ds . createModel ( 'post' , {
189+ id : { type : Number , id : true } ,
190+ title : String ,
191+ content : String
192+ } )
193+ app . model ( Post )
194+ app . use ( loopback . rest ( ) )
195+ JSONAPIComponent ( app , { restApiRoot : '' , errorStackInResponse : true } )
196+ } )
197+
198+ it (
199+ 'POST /models should return a more specific 422 status code on the error object if type key is not present' ,
200+ function ( done ) {
201+ request ( app )
202+ . post ( '/posts' )
203+ . send ( {
204+ data : {
205+ attributes : { title : 'my post' , content : 'my post content' }
206+ }
207+ } )
208+ . set ( 'Content-Type' , 'application/json' )
209+ . end ( function ( err , res ) {
210+ expect ( err ) . to . equal ( null )
211+ expect ( res . body ) . to . have . keys ( 'errors' )
212+ expect ( res . body . errors . length ) . to . equal ( 1 )
213+
214+ expect ( res . body . errors [ 0 ] . source ) . to . haveOwnProperty ( 'stack' )
215+ expect ( res . body . errors [ 0 ] . source . stack . length ) . to . be . above ( 100 )
216+
217+ expect ( _ . omit ( res . body . errors [ 0 ] , 'source' ) ) . to . deep . equal ( {
218+ status : 422 ,
219+ code : 'presence' ,
220+ detail : 'JSON API resource object must contain `data.type` property' ,
221+ title : 'ValidationError'
222+ } )
223+ done ( )
224+ } )
225+ }
226+ )
227+ } )
0 commit comments