11var request = require ( 'supertest' )
2- var assert = require ( 'assert' )
3- var jsonServer = require ( '../src/' )
4-
2+ var assert = require ( 'assert' )
3+ var jsonServer = require ( '../src/' )
4+ var utils = require ( '../src/utils' )
55describe ( 'Server' , function ( ) {
66
77 var server
@@ -23,11 +23,17 @@ describe('Server', function() {
2323 ]
2424
2525 db . comments = [
26- { id : 1 , published : true , postId : 1 } ,
26+ { id : 1 , published : true , postId : 1 } ,
2727 { id : 2 , published : false , postId : 1 } ,
2828 { id : 3 , published : false , postId : 2 } ,
2929 { id : 4 , published : false , postId : 2 } ,
3030 { id : 5 , published : false , postId : 2 } ,
31+ { id : 6 , published : false , postId : 2 } ,
32+ { id : 7 , published : false , postId : 2 } ,
33+ { id : 8 , published : false , postId : 2 } ,
34+ { id : 9 , published : false , postId : 2 } ,
35+ { id : 10 , published : false , postId : 2 } ,
36+ { id : 11 , published : false , postId : 2 }
3137 ]
3238
3339 db . refs = [
@@ -89,11 +95,11 @@ describe('Server', function() {
8995 } )
9096
9197 it ( 'should return an empty array when nothing is matched' , function ( done ) {
92- request ( server )
93- . get ( '/tags?q=nope' )
94- . expect ( 'Content-Type' , / j s o n / )
95- . expect ( [ ] )
96- . expect ( 200 , done )
98+ request ( server )
99+ . get ( '/tags?q=nope' )
100+ . expect ( 'Content-Type' , / j s o n / )
101+ . expect ( [ ] )
102+ . expect ( 200 , done )
97103 } )
98104 } )
99105
@@ -110,29 +116,29 @@ describe('Server', function() {
110116 } )
111117
112118 describe ( 'GET /:resource?sort=' , function ( ) {
113- it ( 'should respond with json and sort on a field' , function ( done ) {
114- request ( server )
115- . get ( '/tags?_sort=body' )
116- . expect ( 'Content-Type' , / j s o n / )
117- . expect ( [ db . tags [ 1 ] , db . tags [ 0 ] , db . tags [ 2 ] ] )
118- . expect ( 200 , done )
119- } )
119+ it ( 'should respond with json and sort on a field' , function ( done ) {
120+ request ( server )
121+ . get ( '/tags?_sort=body' )
122+ . expect ( 'Content-Type' , / j s o n / )
123+ . expect ( [ db . tags [ 1 ] , db . tags [ 0 ] , db . tags [ 2 ] ] )
124+ . expect ( 200 , done )
125+ } )
120126
121- it ( 'should reverse sorting with _order=DESC' , function ( done ) {
122- request ( server )
123- . get ( '/tags?_sort=body&_order=DESC' )
124- . expect ( 'Content-Type' , / j s o n / )
125- . expect ( [ db . tags [ 2 ] , db . tags [ 0 ] , db . tags [ 1 ] ] )
126- . expect ( 200 , done )
127- } )
127+ it ( 'should reverse sorting with _order=DESC' , function ( done ) {
128+ request ( server )
129+ . get ( '/tags?_sort=body&_order=DESC' )
130+ . expect ( 'Content-Type' , / j s o n / )
131+ . expect ( [ db . tags [ 2 ] , db . tags [ 0 ] , db . tags [ 1 ] ] )
132+ . expect ( 200 , done )
133+ } )
128134
129- it ( 'should sort on numerical field' , function ( done ) {
130- request ( server )
131- . get ( '/posts?_sort=id&_order=DESC' )
132- . expect ( 'Content-Type' , / j s o n / )
133- . expect ( db . posts . reverse ( ) )
134- . expect ( 200 , done )
135- } )
135+ it ( 'should sort on numerical field' , function ( done ) {
136+ request ( server )
137+ . get ( '/posts?_sort=id&_order=DESC' )
138+ . expect ( 'Content-Type' , / j s o n / )
139+ . expect ( db . posts . reverse ( ) )
140+ . expect ( 200 , done )
141+ } )
136142 } )
137143
138144 describe ( 'GET /:resource?_start=&_end=' , function ( ) {
@@ -147,14 +153,27 @@ describe('Server', function() {
147153 } )
148154 } )
149155
156+ describe ( 'GET /:resource?_start=&_limit=' , function ( ) {
157+ it ( 'should respond with a limited array' , function ( done ) {
158+ request ( server )
159+ . get ( '/comments?_start=5&_limit=3' )
160+ . expect ( 'Content-Type' , / j s o n / )
161+ . expect ( 'x-total-count' , db . comments . length . toString ( ) )
162+ . expect ( 'Access-Control-Expose-Headers' , 'X-Total-Count' )
163+ . expect ( utils . limitArray ( db . comments , 5 , 3 ) )
164+ . expect ( 200 , done )
165+ } )
166+ } )
167+
150168 describe ( 'GET /:parent/:parentId/:resource' , function ( ) {
151169 it ( 'should respond with json and corresponding nested resources' , function ( done ) {
152170 request ( server )
153171 . get ( '/posts/1/comments' )
154172 . expect ( 'Content-Type' , / j s o n / )
155173 . expect ( [
156174 db . comments [ 0 ] ,
157- db . comments [ 1 ]
175+ db . comments [ 1 ] ,
176+
158177 ] )
159178 . expect ( 200 , done )
160179 } )
@@ -196,7 +215,7 @@ describe('Server', function() {
196215 . expect ( 'Content-Type' , / j s o n / )
197216 . expect ( { id : 3 , body : 'foo' , booleanValue : true , integerValue : 1 } )
198217 . expect ( 200 )
199- . end ( function ( err , res ) {
218+ . end ( function ( err , res ) {
200219 if ( err ) return done ( err )
201220 assert . equal ( db . posts . length , 3 )
202221 done ( )
@@ -210,7 +229,7 @@ describe('Server', function() {
210229 . send ( { url : 'http://foo.com' , postId : '1' } )
211230 . expect ( 'Content-Type' , / j s o n / )
212231 . expect ( 200 )
213- . end ( function ( err , res ) {
232+ . end ( function ( err , res ) {
214233 if ( err ) return done ( err )
215234 assert . equal ( db . refs . length , 2 )
216235 done ( )
@@ -226,7 +245,7 @@ describe('Server', function() {
226245 . expect ( 'Content-Type' , / j s o n / )
227246 . expect ( { id : 1 , body : 'bar' , booleanValue : true , integerValue : 1 } )
228247 . expect ( 200 )
229- . end ( function ( err , res ) {
248+ . end ( function ( err , res ) {
230249 if ( err ) return done ( err )
231250 // assert it was created in database too
232251 assert . deepEqual ( db . posts [ 0 ] , { id : 1 , body : 'bar' , booleanValue : true , integerValue : 1 } )
@@ -252,7 +271,7 @@ describe('Server', function() {
252271 . expect ( 'Content-Type' , / j s o n / )
253272 . expect ( { id : 1 , body : 'bar' } )
254273 . expect ( 200 )
255- . end ( function ( err , res ) {
274+ . end ( function ( err , res ) {
256275 if ( err ) return done ( err )
257276 // assert it was created in database too
258277 assert . deepEqual ( db . posts [ 0 ] , { id : 1 , body : 'bar' } )
@@ -276,10 +295,10 @@ describe('Server', function() {
276295 . del ( '/posts/1' )
277296 . expect ( { } )
278297 . expect ( 200 )
279- . end ( function ( err , res ) {
298+ . end ( function ( err , res ) {
280299 if ( err ) return done ( err )
281300 assert . equal ( db . posts . length , 1 )
282- assert . equal ( db . comments . length , 3 )
301+ assert . equal ( db . comments . length , 9 )
283302 done ( )
284303 } )
285304 } )
0 commit comments