This commit is contained in:
typicode
2016-10-06 00:15:13 +02:00
parent aa3ad62092
commit 954c0b61d3
22 changed files with 590 additions and 574 deletions

View File

@ -1,70 +1,73 @@
var fs = require('fs')
var path = require('path')
var cp = require('child_process')
var assert = require('assert')
var supertest = require('supertest')
var osTmpdir = require('os-tmpdir')
var tempWrite = require('temp-write')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var express = require('express')
var serverReady = require('server-ready')
var pkg = require('../../package.json')
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const assert = require('assert')
const supertest = require('supertest')
const osTmpdir = require('os-tmpdir')
const tempWrite = require('temp-write')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const express = require('express')
const serverReady = require('server-ready')
const pkg = require('../../package.json')
var PORT = 3100
let PORT = 3100
const middlewareFiles = {
en: './fixtures/middlewares/en.js',
jp: './fixtures/middlewares/jp.js'
}
function cli (args) {
var bin = path.join(__dirname, '../..', pkg.bin)
const bin = path.join(__dirname, '../..', pkg.bin)
return cp.spawn('node', [bin, '-p', PORT].concat(args), {
cwd: __dirname,
stdio: ['pipe', process.stdout, process.stderr]
})
}
/* global beforeEach, afterEach, describe, it */
describe('cli', () => {
let child
let request
let dbFile
let routesFile
describe('cli', function () {
var child
var request
var dbFile
var routesFile
var middlewareFiles = {
en: './fixtures/middlewares/en.js',
jp: './fixtures/middlewares/jp.js'
}
beforeEach(() => {
dbFile = tempWrite.sync(
JSON.stringify({
posts: [
{ id: 1 },
{ _id: 2 }
]
}),
'db.json'
)
beforeEach(function () {
dbFile = tempWrite.sync(JSON.stringify({
posts: [
{ id: 1 },
{ _id: 2 }
]
}), 'db.json')
routesFile = tempWrite.sync(JSON.stringify({
'/blog/': '/'
}), 'routes.json')
routesFile = tempWrite.sync(
JSON.stringify({ '/blog/': '/' }),
'routes.json'
)
++PORT
request = supertest('http://localhost:' + PORT)
request = supertest(`http://localhost:${PORT}`)
})
afterEach(function () {
afterEach(() => {
child.kill()
})
describe('db.json', function () {
beforeEach(function (done) {
child = cli([dbFile])
describe('db.json', () => {
beforeEach((done) => {
child = cli([ dbFile ])
serverReady(PORT, done)
})
it('should support JSON file', function (done) {
it('should support JSON file', (done) => {
request.get('/posts').expect(200, done)
})
it('should send CORS headers', function (done) {
var origin = 'http://example.com'
it('should send CORS headers', (done) => {
const origin = 'http://example.com'
request.get('/posts')
.set('Origin', origin)
@ -72,12 +75,12 @@ describe('cli', function () {
.expect(200, done)
})
it('should update JSON file', function (done) {
it('should update JSON file', (done) => {
request.post('/posts')
.send({ title: 'hello' })
.end(function () {
setTimeout(function () {
var str = fs.readFileSync(dbFile, 'utf8')
.end(() => {
setTimeout(() => {
const str = fs.readFileSync(dbFile, 'utf8')
assert(str.indexOf('hello') !== -1)
done()
}, 1000)
@ -85,118 +88,122 @@ describe('cli', function () {
})
})
describe('seed.js', function () {
beforeEach(function (done) {
child = cli(['fixtures/seed.js'])
describe('seed.js', () => {
beforeEach((done) => {
child = cli([ 'fixtures/seed.js' ])
serverReady(PORT, done)
})
it('should support JS file', function (done) {
it('should support JS file', (done) => {
request.get('/posts').expect(200, done)
})
})
describe('http://localhost:8080/db', function () {
beforeEach(function (done) {
var fakeServer = express()
fakeServer.get('/db', function (req, res) {
describe('http://localhost:8080/db', () => {
beforeEach((done) => {
const fakeServer = express()
fakeServer.get('/db', (req, res) => {
res.jsonp({ posts: [] })
})
fakeServer.listen(8080, function () {
child = cli(['http://localhost:8080/db'])
fakeServer.listen(8080, () => {
child = cli([ 'http://localhost:8080/db' ])
serverReady(PORT, done)
})
})
it('should support URL file', function (done) {
it('should support URL file', (done) => {
request.get('/posts').expect(200, done)
})
})
describe('db.json -r routes.json -m middleware.js -i _id --read-only', function () {
beforeEach(function (done) {
child = cli([dbFile, '-r', routesFile, '-m', middlewareFiles.en, '-i', '_id', '--read-only'])
describe('db.json -r routes.json -m middleware.js -i _id --read-only', () => {
beforeEach((done) => {
child = cli([ dbFile, '-r', routesFile, '-m', middlewareFiles.en, '-i', '_id', '--read-only' ])
serverReady(PORT, done)
})
it('should use routes.json and _id as the identifier', function (done) {
it('should use routes.json and _id as the identifier', (done) => {
request.get('/blog/posts/2').expect(200, done)
})
it('should apply middlewares', function (done) {
it('should apply middlewares', (done) => {
request.get('/blog/posts/2').expect('X-Hello', 'World', done)
})
it('should allow only GET requests', function (done) {
it('should allow only GET requests', (done) => {
request.post('/blog/posts').expect(403, done)
})
})
describe('db.json -m first-middleware.js second-middleware.js', function () {
beforeEach(function (done) {
child = cli([dbFile, '-m', middlewareFiles.en, middlewareFiles.jp])
describe('db.json -m first-middleware.js second-middleware.js', () => {
beforeEach((done) => {
child = cli([ dbFile, '-m', middlewareFiles.en, middlewareFiles.jp ])
serverReady(PORT, done)
})
it('should apply all middlewares', function (done) {
it('should apply all middlewares', (done) => {
request.get('/posts')
.expect('X-Hello', 'World')
.expect('X-Konnichiwa', 'Sekai', done)
})
})
describe('db.json -d 1000', function () {
beforeEach(function (done) {
child = cli([dbFile, '-d', 1000])
describe('db.json -d 1000', () => {
beforeEach((done) => {
child = cli([ dbFile, '-d', 1000 ])
serverReady(PORT, done)
})
it('should delay response', function (done) {
var start = new Date()
it('should delay response', (done) => {
const start = new Date()
request.get('/posts').expect(200, function (err) {
var end = new Date()
done(end - start > 1000 ? err : new Error('Request wasn\'t delayed'))
const end = new Date()
done(
end - start > 1000
? err
: new Error('Request wasn\'t delayed')
)
})
})
})
describe('db.json -s fixtures/public -S /some/path/snapshots', function () {
var snapshotsDir = path.join(osTmpdir(), 'snapshots')
var publicDir = 'fixtures/public'
describe('db.json -s fixtures/public -S /some/path/snapshots', () => {
const snapshotsDir = path.join(osTmpdir(), 'snapshots')
const publicDir = 'fixtures/public'
beforeEach(function (done) {
beforeEach((done) => {
rimraf.sync(snapshotsDir)
mkdirp.sync(snapshotsDir)
child = cli([dbFile, '-s', publicDir, '-S', snapshotsDir])
serverReady(PORT, function () {
child = cli([ dbFile, '-s', publicDir, '-S', snapshotsDir ])
serverReady(PORT, () => {
child.stdin.write('s\n')
setTimeout(done, 100)
})
})
it('should serve fixtures/public', function (done) {
it('should serve fixtures/public', (done) => {
request.get('/').expect(/Hello/, done)
})
it('should save a snapshot in snapshots dir', function () {
it('should save a snapshot in snapshots dir', () => {
assert.equal(fs.readdirSync(snapshotsDir).length, 1)
})
})
describe('fixtures/seed.json --no-cors=true', function () {
beforeEach(function (done) {
child = cli(['fixtures/seed.js', '--no-cors=true'])
describe('fixtures/seed.json --no-cors=true', () => {
beforeEach((done) => {
child = cli([ 'fixtures/seed.js', '--no-cors=true' ])
serverReady(PORT, done)
})
it('should not send Access-Control-Allow-Origin headers', function (done) {
var origin = 'http://example.com'
it('should not send Access-Control-Allow-Origin headers', (done) => {
const origin = 'http://example.com'
request.get('/posts')
.set('Origin', origin)
.expect(200)
.end(function (err, res) {
.end((err, res) => {
if (err) {
done(err)
} if ('access-control-allow-origin' in res.headers) {
@ -208,13 +215,13 @@ describe('cli', function () {
})
})
describe('fixtures/seed.json --no-gzip=true', function () {
beforeEach(function (done) {
child = cli(['fixtures/seed.js', '--no-gzip=true'])
describe('fixtures/seed.json --no-gzip=true', () => {
beforeEach((done) => {
child = cli([ 'fixtures/seed.js', '--no-gzip=true' ])
serverReady(PORT, done)
})
it('should not set Content-Encoding to gzip', function (done) {
it('should not set Content-Encoding to gzip', (done) => {
request.get('/posts')
.expect(200)
.end(function (err, res) {
@ -229,15 +236,15 @@ describe('cli', function () {
})
})
describe('--watch db.json -r routes.json', function () {
beforeEach(function (done) {
child = cli(['--watch', dbFile, '-r', routesFile])
describe('--watch db.json -r routes.json', () => {
beforeEach((done) => {
child = cli([ '--watch', dbFile, '-r', routesFile ])
serverReady(PORT, done)
})
it('should watch db file', function (done) {
it('should watch db file', (done) => {
fs.writeFileSync(dbFile, JSON.stringify({ foo: [] }))
setTimeout(function () {
setTimeout(() => {
request.get('/foo').expect(200, done)
}, 1000)
})
@ -246,19 +253,19 @@ describe('cli', function () {
// Can be very slow
this.timeout(10000)
fs.writeFileSync(routesFile, JSON.stringify({ '/api/': '/' }))
setTimeout(function () {
setTimeout(() => {
request.get('/api/posts').expect(200, done)
}, 9000)
})
})
describe('db.json --config some-config.json', function (done) {
beforeEach(function (done) {
child = cli([dbFile, '--config', 'fixtures/config.json'])
describe('db.json --config some-config.json', (done) => {
beforeEach((done) => {
child = cli([ dbFile, '--config', 'fixtures/config.json' ])
serverReady(PORT, done)
})
it('should apply all middlewares', function (done) {
it('should apply all middlewares', (done) => {
request.get('/posts')
.expect('X-Hello', 'World')
.expect('X-Konnichiwa', 'Sekai', done)

2
test/mocha.opts Normal file
View File

@ -0,0 +1,2 @@
--compilers js:babel-register
--reporter spec

View File

@ -1,34 +1,52 @@
var assert = require('assert')
var _ = require('lodash')
var _db = require('underscore-db')
var mixins = require('../../src/server/mixins')
const assert = require('assert')
const _ = require('lodash')
const _db = require('underscore-db')
const mixins = require('../../src/server/mixins')
/* global describe, it */
describe('mixins', () => {
let db
describe('mixins', function () {
describe('getRemovable', function () {
it('should return removable documents', function () {
var db = {
posts: [
{id: 1, comment: 1}
],
comments: [
{id: 1, postId: 1},
// Comments below references a post that doesn't exist
{id: 2, postId: 2},
{id: 3, postId: 2}
]
}
before(() => {
_.mixin(_db)
_.mixin(mixins)
})
var expected = [
{name: 'comments', id: 2},
{name: 'comments', id: 3}
beforeEach(() => {
db = {
posts: [
{ id: 1, comment: 1 }
],
comments: [
{ id: 1, postId: 1 },
// Comments below references a post that doesn't exist
{ id: 2, postId: 2 },
{ id: 3, postId: 2 }
],
photos: [
{ id: '1' },
{ id: '2' }
]
}
})
_.mixin(_db)
_.mixin(mixins)
describe('getRemovable', () => {
it('should return removable documents', () => {
const expected = [
{ name: 'comments', id: 2 },
{ name: 'comments', id: 3 }
]
assert.deepEqual(_.getRemovable(db), expected)
})
})
describe('createId', () => {
it('should return a new id', () => {
assert.equal(_.createId(db.comments), 4)
})
it('should return a new uuid', () => {
assert.notEqual(_.createId(db.photos), 3)
})
})
})

View File

@ -1,47 +1,46 @@
var assert = require('assert')
var _ = require('lodash')
var request = require('supertest')
var jsonServer = require('../../src/server')
const assert = require('assert')
const _ = require('lodash')
const request = require('supertest')
const jsonServer = require('../../src/server')
/* global beforeEach, describe, it */
describe('Server', function () {
var server
var router
var db
describe('Server', () => {
let server
let router
let db
beforeEach(function () {
beforeEach(() => {
db = {}
db.posts = [
{id: 1, body: 'foo'},
{id: 2, body: 'bar'}
{ id: 1, body: 'foo' },
{ id: 2, body: 'bar' }
]
db.tags = [
{id: 1, body: 'Technology'},
{id: 2, body: 'Photography'},
{id: 3, body: 'photo'}
{ id: 1, body: 'Technology' },
{ id: 2, body: 'Photography' },
{ id: 3, body: 'photo' }
]
db.users = [
{id: 1, username: 'Jim', tel: '0123'},
{id: 2, username: 'George', tel: '123'}
{ id: 1, username: 'Jim', tel: '0123' },
{ id: 2, username: 'George', tel: '123' }
]
db.comments = [
{id: 1, body: 'foo', published: true, postId: 1, userId: 1},
{id: 2, body: 'bar', published: false, postId: 1, userId: 2},
{id: 3, body: 'baz', published: false, postId: 2, userId: 1},
{id: 4, body: 'qux', published: true, postId: 2, userId: 2},
{id: 5, body: 'quux', published: false, postId: 2, userId: 1}
{ id: 1, body: 'foo', published: true, postId: 1, userId: 1 },
{ id: 2, body: 'bar', published: false, postId: 1, userId: 2 },
{ id: 3, body: 'baz', published: false, postId: 2, userId: 1 },
{ id: 4, body: 'qux', published: true, postId: 2, userId: 2 },
{ id: 5, body: 'quux', published: false, postId: 2, userId: 1 }
]
db.refs = [
{id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1}
{ id: 'abcd-1234', url: 'http://example.com', postId: 1, userId: 1 }
]
db.stringIds = [
{id: '1234'}
{ id: '1234' }
]
db.deep = [
@ -50,27 +49,27 @@ describe('Server', function () {
]
db.nested = [
{resource: {name: 'dewey'}},
{resource: {name: 'cheatem'}},
{resource: {name: 'howe'}}
{ resource: {name: 'dewey'} },
{ resource: {name: 'cheatem'} },
{ resource: {name: 'howe'} }
]
db.list = [
{id: 1},
{id: 2},
{id: 3},
{id: 4},
{id: 5},
{id: 6},
{id: 7},
{id: 8},
{id: 9},
{id: 10},
{id: 11},
{id: 12},
{id: 13},
{id: 14},
{id: 15}
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
{ id: 6 },
{ id: 7 },
{ id: 8 },
{ id: 9 },
{ id: 10 },
{ id: 11 },
{ id: 12 },
{ id: 13 },
{ id: 14 },
{ id: 15 }
]
server = jsonServer.create()
@ -83,8 +82,8 @@ describe('Server', function () {
server.use(router)
})
describe('GET /db', function () {
it('should respond with json and full database', function (done) {
describe('GET /db', () => {
it('should respond with json and full database', (done) => {
request(server)
.get('/db')
.expect('Content-Type', /json/)
@ -93,8 +92,8 @@ describe('Server', function () {
})
})
describe('GET /:resource', function () {
it('should respond with json and corresponding resources', function (done) {
describe('GET /:resource', () => {
it('should respond with json and corresponding resources', (done) => {
request(server)
.get('/posts')
.set('Origin', 'http://example.com')
@ -105,47 +104,47 @@ describe('Server', function () {
.expect(200, done)
})
it('should respond with 404 if resource is not found', function (done) {
it('should respond with 404 if resource is not found', (done) => {
request(server)
.get('/undefined')
.expect(404, done)
})
})
describe('GET /:resource?attr=&attr=', function () {
it('should respond with json and filter resources', function (done) {
describe('GET /:resource?attr=&attr=', () => {
it('should respond with json and filter resources', (done) => {
request(server)
.get('/comments?postId=1&published=true')
.expect('Content-Type', /json/)
.expect([db.comments[0]])
.expect([ db.comments[0] ])
.expect(200, done)
})
it('should be strict', function (done) {
it('should be strict', (done) => {
request(server)
.get('/users?tel=123')
.expect('Content-Type', /json/)
.expect([db.users[1]])
.expect([ db.users[1] ])
.expect(200, done)
})
it('should support multiple filters', function (done) {
it('should support multiple filters', (done) => {
request(server)
.get('/comments?id=1&id=2')
.expect('Content-Type', /json/)
.expect([db.comments[0], db.comments[1]])
.expect([ db.comments[0], db.comments[1] ])
.expect(200, done)
})
it('should support deep filter', function (done) {
it('should support deep filter', (done) => {
request(server)
.get('/deep?a.b=1')
.expect('Content-Type', /json/)
.expect([db.deep[0]])
.expect([ db.deep[0] ])
.expect(200, done)
})
it('should ignore JSONP query parameters callback and _ ', function (done) {
it('should ignore JSONP query parameters callback and _ ', (done) => {
request(server)
.get('/comments?callback=1&_=1')
.expect('Content-Type', /text/)
@ -153,7 +152,7 @@ describe('Server', function () {
.expect(200, done)
})
it('should ignore unknown query parameters', function (done) {
it('should ignore unknown query parameters', (done) => {
request(server)
.get('/comments?foo=1&bar=2')
.expect('Content-Type', /json/)
@ -162,16 +161,16 @@ describe('Server', function () {
})
})
describe('GET /:resource?q=', function () {
it('should respond with json and make a full-text search', function (done) {
describe('GET /:resource?q=', () => {
it('should respond with json and make a full-text search', (done) => {
request(server)
.get('/tags?q=pho')
.expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[2]])
.expect([ db.tags[1], db.tags[2] ])
.expect(200, done)
})
it('should respond with json and make a deep full-text search', function (done) {
it('should respond with json and make a deep full-text search', (done) => {
request(server)
.get('/deep?q=1')
.expect('Content-Type', /json/)
@ -179,25 +178,25 @@ describe('Server', function () {
.expect(200, done)
})
it('should return an empty array when nothing is matched', function (done) {
it('should return an empty array when nothing is matched', (done) => {
request(server)
.get('/tags?q=nope')
.expect('Content-Type', /json/)
.expect([])
.expect([ ])
.expect(200, done)
})
it('should support other query parameters', function (done) {
it('should support other query parameters', (done) => {
request(server)
.get('/comments?q=qu&published=true')
.expect('Content-Type', /json/)
.expect([db.comments[3]])
.expect([ db.comments[3] ])
.expect(200, done)
})
})
describe('GET /:resource?_end=', function () {
it('should respond with a sliced array', function (done) {
describe('GET /:resource?_end=', () => {
it('should respond with a sliced array', (done) => {
request(server)
.get('/comments?_end=2')
.expect('Content-Type', /json/)
@ -208,24 +207,24 @@ describe('Server', function () {
})
})
describe('GET /:resource?_sort=', function () {
it('should respond with json and sort on a field', function (done) {
describe('GET /:resource?_sort=', () => {
it('should respond with json and sort on a field', (done) => {
request(server)
.get('/tags?_sort=body')
.expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[0], db.tags[2]])
.expect([ db.tags[1], db.tags[0], db.tags[2] ])
.expect(200, done)
})
it('should reverse sorting with _order=DESC', function (done) {
it('should reverse sorting with _order=DESC', (done) => {
request(server)
.get('/tags?_sort=body&_order=DESC')
.expect('Content-Type', /json/)
.expect([db.tags[2], db.tags[0], db.tags[1]])
.expect([ db.tags[2], db.tags[0], db.tags[1] ])
.expect(200, done)
})
it('should sort on numerical field', function (done) {
it('should sort on numerical field', (done) => {
request(server)
.get('/posts?_sort=id&_order=DESC')
.expect('Content-Type', /json/)
@ -233,54 +232,54 @@ describe('Server', function () {
.expect(200, done)
})
it('should sort on nested field', function (done) {
it('should sort on nested field', (done) => {
request(server)
.get('/nested?_sort=resource.name')
.expect('Content-Type', /json/)
.expect([db.nested[1], db.nested[0], db.nested[2]])
.expect([ db.nested[1], db.nested[0], db.nested[2] ])
.expect(200, done)
})
})
describe('GET /:resource?_start=&_end=', function () {
it('should respond with a sliced array', function (done) {
describe('GET /:resource?_start=&_end=', () => {
it('should respond with a sliced array', (done) => {
request(server)
.get('/comments?_start=1&_end=2')
.expect('Content-Type', /json/)
.expect('x-total-count', db.comments.length.toString())
.expect('X-Total-Count', db.comments.length.toString())
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
.expect(db.comments.slice(1, 2))
.expect(200, done)
})
})
describe('GET /:resource?_start=&_limit=', function () {
it('should respond with a limited array', function (done) {
describe('GET /:resource?_start=&_limit=', () => {
it('should respond with a limited array', (done) => {
request(server)
.get('/comments?_start=1&_limit=1')
.expect('Content-Type', /json/)
.expect('x-total-count', db.comments.length.toString())
.expect('X-Total-Count', db.comments.length.toString())
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
.expect(db.comments.slice(1, 2))
.expect(200, done)
})
})
describe('GET /:resource?_page=', function () {
it('should paginate', function (done) {
describe('GET /:resource?_page=', () => {
it('should paginate', (done) => {
request(server)
.get('/list?_page=2')
.expect('Content-Type', /json/)
.expect('x-total-count', db.list.length.toString())
.expect('X-Total-Count', db.list.length.toString())
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
.expect(db.list.slice(10, 20))
.expect(200, done)
})
})
describe('GET /:resource?_page=&_limit=', function () {
it('should paginate with a custom limit', function (done) {
var link = [
describe('GET /:resource?_page=&_limit=', () => {
it('should paginate with a custom limit', (done) => {
const link = [
'<http://localhost/list?_page=1&_limit=1>; rel="first"',
'<http://localhost/list?_page=1&_limit=1>; rel="prev"',
'<http://localhost/list?_page=3&_limit=1>; rel="next"',
@ -298,8 +297,8 @@ describe('Server', function () {
})
})
describe('GET /:resource?attr_gte=&attr_lte=', function () {
it('should respond with a limited array', function (done) {
describe('GET /:resource?attr_gte=&attr_lte=', () => {
it('should respond with a limited array', (done) => {
request(server)
.get('/comments?id_gte=2&id_lte=3')
.expect('Content-Type', /json/)
@ -308,8 +307,8 @@ describe('Server', function () {
})
})
describe('GET /:resource?attr_ne=', function () {
it('should respond with a limited array', function (done) {
describe('GET /:resource?attr_ne=', () => {
it('should respond with a limited array', (done) => {
request(server)
.get('/comments?id_ne=1')
.expect('Content-Type', /json/)
@ -318,8 +317,8 @@ describe('Server', function () {
})
})
describe('GET /:resource?attr_like=', function () {
it('should respond with an array that matches the like operator (case insensitive)', function (done) {
describe('GET /:resource?attr_like=', () => {
it('should respond with an array that matches the like operator (case insensitive)', (done) => {
request(server)
.get('/tags?body_like=photo')
.expect('Content-Type', /json/)
@ -331,8 +330,8 @@ describe('Server', function () {
})
})
describe('GET /:parent/:parentId/:resource', function () {
it('should respond with json and corresponding nested resources', function (done) {
describe('GET /:parent/:parentId/:resource', () => {
it('should respond with json and corresponding nested resources', (done) => {
request(server)
.get('/posts/1/comments')
.expect('Content-Type', /json/)
@ -344,8 +343,8 @@ describe('Server', function () {
})
})
describe('GET /:resource/:id', function () {
it('should respond with json and corresponding resource', function (done) {
describe('GET /:resource/:id', () => {
it('should respond with json and corresponding resource', (done) => {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
@ -353,7 +352,7 @@ describe('Server', function () {
.expect(200, done)
})
it('should support string id, respond with json and corresponding resource', function (done) {
it('should support string id, respond with json and corresponding resource', (done) => {
request(server)
.get('/refs/abcd-1234')
.expect('Content-Type', /json/)
@ -361,7 +360,7 @@ describe('Server', function () {
.expect(200, done)
})
it('should support integer id as string', function (done) {
it('should support integer id as string', (done) => {
request(server)
.get('/stringIds/1234')
.expect('Content-Type', /json/)
@ -369,7 +368,7 @@ describe('Server', function () {
.expect(200, done)
})
it('should respond with 404 if resource is not found', function (done) {
it('should respond with 404 if resource is not found', (done) => {
request(server)
.get('/posts/9001')
.expect('Content-Type', /json/)
@ -378,11 +377,11 @@ describe('Server', function () {
})
})
describe('GET /:resource?_embed=', function () {
it('should respond with corresponding resources and embedded resources', function (done) {
var posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]]
posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]]
describe('GET /:resource?_embed=', () => {
it('should respond with corresponding resources and embedded resources', (done) => {
const posts = _.cloneDeep(db.posts)
posts[0].comments = [ db.comments[0], db.comments[1] ]
posts[1].comments = [ db.comments[2], db.comments[3], db.comments[4] ]
request(server)
.get('/posts?_embed=comments')
.expect('Content-Type', /json/)
@ -391,12 +390,12 @@ describe('Server', function () {
})
})
describe('GET /:resource?_embed&_embed=', function () {
it('should respond with corresponding resources and embedded resources', function (done) {
var posts = _.cloneDeep(db.posts)
posts[0].comments = [db.comments[0], db.comments[1]]
posts[0].refs = [db.refs[0]]
posts[1].comments = [db.comments[2], db.comments[3], db.comments[4]]
describe('GET /:resource?_embed&_embed=', () => {
it('should respond with corresponding resources and embedded resources', (done) => {
const posts = _.cloneDeep(db.posts)
posts[0].comments = [ db.comments[0], db.comments[1] ]
posts[0].refs = [ db.refs[0] ]
posts[1].comments = [ db.comments[2], db.comments[3], db.comments[4] ]
posts[1].refs = []
request(server)
.get('/posts?_embed=comments&_embed=refs')
@ -406,10 +405,10 @@ describe('Server', function () {
})
})
describe('GET /:resource/:id?_embed=', function () {
it('should respond with corresponding resources and embedded resources', function (done) {
var post = _.cloneDeep(db.posts[0])
post.comments = [db.comments[0], db.comments[1]]
describe('GET /:resource/:id?_embed=', () => {
it('should respond with corresponding resources and embedded resources', (done) => {
const post = _.cloneDeep(db.posts[0])
post.comments = [ db.comments[0], db.comments[1] ]
request(server)
.get('/posts/1?_embed=comments')
.expect('Content-Type', /json/)
@ -418,10 +417,10 @@ describe('Server', function () {
})
})
describe('GET /:resource/:id?_embed=&_embed=', function () {
it('should respond with corresponding resource and embedded resources', function (done) {
var post = _.cloneDeep(db.posts[0])
post.comments = [db.comments[0], db.comments[1]]
describe('GET /:resource/:id?_embed=&_embed=', () => {
it('should respond with corresponding resource and embedded resources', (done) => {
const post = _.cloneDeep(db.posts[0])
post.comments = [ db.comments[0], db.comments[1] ]
post.refs = [db.refs[0]]
request(server)
.get('/posts/1?_embed=comments&_embed=refs')
@ -431,9 +430,9 @@ describe('Server', function () {
})
})
describe('GET /:resource?_expand=', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var refs = _.cloneDeep(db.refs)
describe('GET /:resource?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', (done) => {
const refs = _.cloneDeep(db.refs)
refs[0].post = db.posts[0]
request(server)
.get('/refs?_expand=post')
@ -443,9 +442,9 @@ describe('Server', function () {
})
})
describe('GET /:resource/:id?_expand=', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var comment = _.cloneDeep(db.comments[0])
describe('GET /:resource/:id?_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', (done) => {
const comment = _.cloneDeep(db.comments[0])
comment.post = db.posts[0]
request(server)
.get('/comments/1?_expand=post')
@ -455,9 +454,9 @@ describe('Server', function () {
})
})
describe('GET /:resource?_expand=&_expand', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var refs = _.cloneDeep(db.refs)
describe('GET /:resource?_expand=&_expand', () => {
it('should respond with corresponding resource and expanded inner resources', (done) => {
const refs = _.cloneDeep(db.refs)
refs[0].post = db.posts[0]
refs[0].user = db.users[0]
request(server)
@ -468,9 +467,9 @@ describe('Server', function () {
})
})
describe('GET /:resource/:id?_expand=&_expand=', function () {
it('should respond with corresponding resource and expanded inner resources', function (done) {
var comments = db.comments[0]
describe('GET /:resource/:id?_expand=&_expand=', () => {
it('should respond with corresponding resource and expanded inner resources', (done) => {
const comments = db.comments[0]
comments.post = db.posts[0]
comments.user = db.users[0]
request(server)
@ -481,16 +480,16 @@ describe('Server', function () {
})
})
describe('POST /:resource', function () {
describe('POST /:resource', () => {
it('should respond with json, create a resource and increment id',
function (done) {
(done) => {
request(server)
.post('/posts')
.send({body: 'foo', booleanValue: true, integerValue: 1})
.expect('Content-Type', /json/)
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
.expect(201)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
assert.equal(db.posts.length, 3)
done()
@ -499,7 +498,7 @@ describe('Server', function () {
)
it('should support x-www-form-urlencoded',
function (done) {
(done) => {
request(server)
.post('/posts')
.type('form')
@ -508,7 +507,7 @@ describe('Server', function () {
// x-www-form-urlencoded will convert to string
.expect({id: 3, body: 'foo', booleanValue: 'true', integerValue: '1'})
.expect(201)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
assert.equal(db.posts.length, 3)
done()
@ -517,13 +516,13 @@ describe('Server', function () {
)
it('should respond with json, create a resource and generate string id',
function (done) {
(done) => {
request(server)
.post('/refs')
.send({url: 'http://foo.com', postId: '1'})
.expect('Content-Type', /json/)
.expect(201)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
assert.equal(db.refs.length, 2)
done()
@ -531,8 +530,8 @@ describe('Server', function () {
})
})
describe('POST /:parent/:parentId/:resource', function () {
it('should respond with json and set parentId', function (done) {
describe('POST /:parent/:parentId/:resource', () => {
it('should respond with json and set parentId', (done) => {
request(server)
.post('/posts/1/comments')
.send({body: 'foo'})
@ -542,8 +541,8 @@ describe('Server', function () {
})
})
describe('PUT /:resource/:id', function () {
it('should respond with json and replace resource', function (done) {
describe('PUT /:resource/:id', () => {
it('should respond with json and replace resource', (done) => {
var post = {id: 1, booleanValue: true, integerValue: 1}
request(server)
.put('/posts/1')
@ -552,7 +551,7 @@ describe('Server', function () {
.expect('Content-Type', /json/)
.expect(post)
.expect(200)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
// assert it was created in database too
assert.deepEqual(db.posts[0], post)
@ -560,7 +559,7 @@ describe('Server', function () {
})
})
it('should respond with 404 if resource is not found', function (done) {
it('should respond with 404 if resource is not found', (done) => {
request(server)
.put('/posts/9001')
.send({id: 1, body: 'bar'})
@ -570,15 +569,15 @@ describe('Server', function () {
})
})
describe('PATCH /:resource/:id', function () {
it('should respond with json and update resource', function (done) {
describe('PATCH /:resource/:id', () => {
it('should respond with json and update resource', (done) => {
request(server)
.patch('/posts/1')
.send({body: 'bar'})
.expect('Content-Type', /json/)
.expect({id: 1, body: 'bar'})
.expect(200)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
// assert it was created in database too
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
@ -586,7 +585,7 @@ describe('Server', function () {
})
})
it('should respond with 404 if resource is not found', function (done) {
it('should respond with 404 if resource is not found', (done) => {
request(server)
.patch('/posts/9001')
.send({body: 'bar'})
@ -596,13 +595,13 @@ describe('Server', function () {
})
})
describe('DELETE /:resource/:id', function () {
it('should respond with empty data, destroy resource and dependent resources', function (done) {
describe('DELETE /:resource/:id', () => {
it('should respond with empty data, destroy resource and dependent resources', (done) => {
request(server)
.del('/posts/1')
.expect({})
.expect(200)
.end(function (err, res) {
.end((err, res) => {
if (err) return done(err)
assert.equal(db.posts.length, 1)
assert.equal(db.comments.length, 3)
@ -610,7 +609,7 @@ describe('Server', function () {
})
})
it('should respond with 404 if resource is not found', function (done) {
it('should respond with 404 if resource is not found', (done) => {
request(server)
.del('/posts/9001')
.expect('Content-Type', /json/)
@ -619,9 +618,9 @@ describe('Server', function () {
})
})
describe('Static routes', function () {
describe('GET /', function () {
it('should respond with html', function (done) {
describe('Static routes', () => {
describe('GET /', () => {
it('should respond with html', (done) => {
request(server)
.get('/')
.expect(/You're successfully running JSON Server/)
@ -629,8 +628,8 @@ describe('Server', function () {
})
})
describe('GET /stylesheets/style.css', function () {
it('should respond with css', function (done) {
describe('GET /stylesheets/style.css', () => {
it('should respond with css', (done) => {
request(server)
.get('/stylesheets/style.css')
.expect('Content-Type', /css/)
@ -639,14 +638,14 @@ describe('Server', function () {
})
})
describe('Database state', function () {
it('should be accessible', function () {
describe('Database state', () => {
it('should be accessible', () => {
assert(router.db.getState())
})
})
describe('Responses', function () {
it('should have no cache headers (for IE)', function (done) {
describe('Responses', () => {
it('should have no cache headers (for IE)', (done) => {
request(server)
.get('/db')
.expect('Cache-Control', 'no-cache')
@ -656,15 +655,15 @@ describe('Server', function () {
})
})
describe('Rewriter', function () {
it('should rewrite using prefix', function (done) {
describe('Rewriter', () => {
it('should rewrite using prefix', (done) => {
request(server)
.get('/api/posts/1')
.expect(db.posts[0])
.end(done)
})
it('should rewrite using params', function (done) {
it('should rewrite using params', (done) => {
request(server)
.get('/blog/posts/1/show')
.expect(db.posts[0])
@ -672,16 +671,16 @@ describe('Server', function () {
})
})
describe('router.render', function (done) {
beforeEach(function () {
router.render = function (req, res) {
describe('router.render', (done) => {
beforeEach(() => {
router.render = (req, res) => {
res.jsonp({
data: res.locals.data
})
}
})
it('should be possible to wrap response', function (done) {
it('should be possible to wrap response', (done) => {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
@ -690,8 +689,8 @@ describe('Server', function () {
})
})
describe('router.db._.id', function (done) {
beforeEach(function () {
describe('router.db._.id', (done) => {
beforeEach(() => {
router.db.setState({
posts: [
{ _id: 1 }
@ -701,7 +700,7 @@ describe('Server', function () {
router.db._.id = '_id'
})
it('should be possible to GET using a different id property', function (done) {
it('should be possible to GET using a different id property', (done) => {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
@ -709,12 +708,12 @@ describe('Server', function () {
.expect(200, done)
})
it('should be possible to POST using a different id property', function (done) {
it('should be possible to POST using a different id property', (done) => {
request(server)
.post('/posts')
.send({ body: 'hello' })
.expect('Content-Type', /json/)
.expect({_id: 2, body: 'hello'})
.expect({ _id: 2, body: 'hello' })
.expect(201, done)
})
})

View File

@ -1,11 +1,10 @@
var request = require('supertest')
var jsonServer = require('../../src/server')
const request = require('supertest')
const jsonServer = require('../../src/server')
/* global beforeEach, describe, it */
describe('Server', function () {
var server
var router
var db
let server
let router
let db
beforeEach(function () {
db = {}
@ -32,7 +31,7 @@ describe('Server', function () {
describe('POST /:resource', function () {
it('should create resource', function (done) {
var user = { name: 'bar' }
const user = { name: 'bar' }
request(server)
.post('/user')
.send(user)
@ -43,7 +42,7 @@ describe('Server', function () {
describe('PUT /:resource', function () {
it('should update resource', function (done) {
var user = { name: 'bar' }
const user = { name: 'bar' }
request(server)
.put('/user')
.send(user)

View File

@ -1,12 +1,10 @@
var assert = require('assert')
var utils = require('../../src/server/utils')
/* global describe, it */
const assert = require('assert')
const utils = require('../../src/server/utils')
describe('utils', function () {
describe('getPage', function () {
var array = [1, 2, 3, 4, 5]
var perPage = 2
const array = [1, 2, 3, 4, 5]
const perPage = 2
it('should return first page', function () {
assert.deepEqual(