1
1
var 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' )
5
5
describe ( 'Server' , function ( ) {
6
6
7
7
var server
@@ -23,11 +23,17 @@ describe('Server', function() {
23
23
]
24
24
25
25
db . comments = [
26
- { id : 1 , published : true , postId : 1 } ,
26
+ { id : 1 , published : true , postId : 1 } ,
27
27
{ id : 2 , published : false , postId : 1 } ,
28
28
{ id : 3 , published : false , postId : 2 } ,
29
29
{ id : 4 , published : false , postId : 2 } ,
30
30
{ 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 }
31
37
]
32
38
33
39
db . refs = [
@@ -89,11 +95,11 @@ describe('Server', function() {
89
95
} )
90
96
91
97
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 )
97
103
} )
98
104
} )
99
105
@@ -110,29 +116,29 @@ describe('Server', function() {
110
116
} )
111
117
112
118
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
+ } )
120
126
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
+ } )
128
134
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
+ } )
136
142
} )
137
143
138
144
describe ( 'GET /:resource?_start=&_end=' , function ( ) {
@@ -147,14 +153,27 @@ describe('Server', function() {
147
153
} )
148
154
} )
149
155
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
+
150
168
describe ( 'GET /:parent/:parentId/:resource' , function ( ) {
151
169
it ( 'should respond with json and corresponding nested resources' , function ( done ) {
152
170
request ( server )
153
171
. get ( '/posts/1/comments' )
154
172
. expect ( 'Content-Type' , / j s o n / )
155
173
. expect ( [
156
174
db . comments [ 0 ] ,
157
- db . comments [ 1 ]
175
+ db . comments [ 1 ] ,
176
+
158
177
] )
159
178
. expect ( 200 , done )
160
179
} )
@@ -196,7 +215,7 @@ describe('Server', function() {
196
215
. expect ( 'Content-Type' , / j s o n / )
197
216
. expect ( { id : 3 , body : 'foo' , booleanValue : true , integerValue : 1 } )
198
217
. expect ( 200 )
199
- . end ( function ( err , res ) {
218
+ . end ( function ( err , res ) {
200
219
if ( err ) return done ( err )
201
220
assert . equal ( db . posts . length , 3 )
202
221
done ( )
@@ -210,7 +229,7 @@ describe('Server', function() {
210
229
. send ( { url : 'http://foo.com' , postId : '1' } )
211
230
. expect ( 'Content-Type' , / j s o n / )
212
231
. expect ( 200 )
213
- . end ( function ( err , res ) {
232
+ . end ( function ( err , res ) {
214
233
if ( err ) return done ( err )
215
234
assert . equal ( db . refs . length , 2 )
216
235
done ( )
@@ -226,7 +245,7 @@ describe('Server', function() {
226
245
. expect ( 'Content-Type' , / j s o n / )
227
246
. expect ( { id : 1 , body : 'bar' , booleanValue : true , integerValue : 1 } )
228
247
. expect ( 200 )
229
- . end ( function ( err , res ) {
248
+ . end ( function ( err , res ) {
230
249
if ( err ) return done ( err )
231
250
// assert it was created in database too
232
251
assert . deepEqual ( db . posts [ 0 ] , { id : 1 , body : 'bar' , booleanValue : true , integerValue : 1 } )
@@ -252,7 +271,7 @@ describe('Server', function() {
252
271
. expect ( 'Content-Type' , / j s o n / )
253
272
. expect ( { id : 1 , body : 'bar' } )
254
273
. expect ( 200 )
255
- . end ( function ( err , res ) {
274
+ . end ( function ( err , res ) {
256
275
if ( err ) return done ( err )
257
276
// assert it was created in database too
258
277
assert . deepEqual ( db . posts [ 0 ] , { id : 1 , body : 'bar' } )
@@ -276,10 +295,10 @@ describe('Server', function() {
276
295
. del ( '/posts/1' )
277
296
. expect ( { } )
278
297
. expect ( 200 )
279
- . end ( function ( err , res ) {
298
+ . end ( function ( err , res ) {
280
299
if ( err ) return done ( err )
281
300
assert . equal ( db . posts . length , 1 )
282
- assert . equal ( db . comments . length , 3 )
301
+ assert . equal ( db . comments . length , 9 )
283
302
done ( )
284
303
} )
285
304
} )
0 commit comments