mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
Fix code style
This commit is contained in:
11
bin/index.js
11
bin/index.js
@ -33,12 +33,12 @@ var argv = yargs
|
|||||||
.argv
|
.argv
|
||||||
|
|
||||||
// Start server function
|
// Start server function
|
||||||
function start(object, filename) {
|
function start (object, filename) {
|
||||||
var port = process.env.PORT || argv.port
|
var port = process.env.PORT || argv.port
|
||||||
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
|
var hostname = argv.host === '0.0.0.0' ? 'localhost' : argv.host
|
||||||
|
|
||||||
for (var prop in object) {
|
for (var prop in object) {
|
||||||
console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop))
|
console.log(chalk.gray(' http://' + hostname + ':' + port + '/') + chalk.cyan(prop))
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
@ -59,10 +59,11 @@ function start(object, filename) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var router
|
||||||
if (filename) {
|
if (filename) {
|
||||||
var router = jsonServer.router(filename)
|
router = jsonServer.router(filename)
|
||||||
} else {
|
} else {
|
||||||
var router = jsonServer.router(object)
|
router = jsonServer.router(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
var server = jsonServer.create()
|
var server = jsonServer.create()
|
||||||
@ -90,7 +91,7 @@ if (/\.js$/.test(source)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (/^http/.test(source)) {
|
if (/^http/.test(source)) {
|
||||||
got(source, function(err, data) {
|
got(source, function (err, data) {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
var object = JSON.parse(data)
|
var object = JSON.parse(data)
|
||||||
start(object)
|
start(object)
|
||||||
|
@ -27,10 +27,11 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"husky": "^0.6.1",
|
"husky": "^0.6.1",
|
||||||
"mocha": "^2.2.4",
|
"mocha": "^2.2.4",
|
||||||
|
"standard": "^3.8.0",
|
||||||
"supertest": "~0.8.1"
|
"supertest": "~0.8.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha -R spec test",
|
"test": "standard && mocha -R spec test",
|
||||||
"start": "node bin",
|
"start": "node bin",
|
||||||
"prepush": "npm t"
|
"prepush": "npm t"
|
||||||
},
|
},
|
||||||
|
@ -8,14 +8,14 @@ var arr = []
|
|||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
arr.push(logger('dev', {
|
arr.push(logger('dev', {
|
||||||
skip: function(req, res) { return req.path === '/favicon.ico' }
|
skip: function (req, res) { return req.path === '/favicon.ico' }
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Serve static files
|
// Serve static files
|
||||||
if (fs.existsSync(process.cwd() + '/public')) {
|
if (fs.existsSync(process.cwd() + '/public')) {
|
||||||
arr.push(express.static(process.cwd() + '/public'));
|
arr.push(express.static(process.cwd() + '/public'))
|
||||||
} else {
|
} else {
|
||||||
arr.push(express.static(__dirname + '/public'));
|
arr.push(express.static(__dirname + '/public'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORS
|
// CORS
|
||||||
@ -26,4 +26,4 @@ if (process.env.NODE_ENV === 'development') {
|
|||||||
arr.push(errorhandler())
|
arr.push(errorhandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = arr
|
module.exports = arr
|
||||||
|
@ -8,4 +8,4 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
defaults: require('./defaults'),
|
defaults: require('./defaults'),
|
||||||
router: require('./router')
|
router: require('./router')
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ low.mixin(require('underscore.inflections'))
|
|||||||
// utils.createId can generate incremental id or uuid
|
// utils.createId can generate incremental id or uuid
|
||||||
low.mixin({createId: utils.createId})
|
low.mixin({createId: utils.createId})
|
||||||
|
|
||||||
module.exports = function(source) {
|
module.exports = function (source) {
|
||||||
// Create router
|
// Create router
|
||||||
var router = express.Router()
|
var router = express.Router()
|
||||||
|
|
||||||
@ -23,18 +23,19 @@ module.exports = function(source) {
|
|||||||
router.use(methodOverride())
|
router.use(methodOverride())
|
||||||
|
|
||||||
// Create database
|
// Create database
|
||||||
|
var db
|
||||||
if (_.isObject(source)) {
|
if (_.isObject(source)) {
|
||||||
var db = low()
|
db = low()
|
||||||
db.object = source
|
db.object = source
|
||||||
} else {
|
} else {
|
||||||
var db = low(source)
|
db = low(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose database
|
// Expose database
|
||||||
router.db = db
|
router.db = db
|
||||||
|
|
||||||
// GET /db
|
// GET /db
|
||||||
function showDatabase(req, res, next) {
|
function showDatabase (req, res, next) {
|
||||||
res.jsonp(db.object)
|
res.jsonp(db.object)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ module.exports = function(source) {
|
|||||||
// GET /:parent/:parentId/:resource?attr=&attr=
|
// GET /:parent/:parentId/:resource?attr=&attr=
|
||||||
// GET /*?*&_end=
|
// GET /*?*&_end=
|
||||||
// GET /*?*&_start=&_end=
|
// GET /*?*&_start=&_end=
|
||||||
function list(req, res, next) {
|
function list (req, res, next) {
|
||||||
// Test if resource exists
|
// Test if resource exists
|
||||||
if (!db.object.hasOwnProperty(req.params.resource)) {
|
if (!db.object.hasOwnProperty(req.params.resource)) {
|
||||||
return res.sendStatus(404)
|
return res.sendStatus(404)
|
||||||
@ -74,7 +75,7 @@ module.exports = function(source) {
|
|||||||
// Full-text search
|
// Full-text search
|
||||||
var q = req.query.q.toLowerCase()
|
var q = req.query.q.toLowerCase()
|
||||||
|
|
||||||
array = db(req.params.resource).filter(function(obj) {
|
array = db(req.params.resource).filter(function (obj) {
|
||||||
for (var key in obj) {
|
for (var key in obj) {
|
||||||
var value = obj[key]
|
var value = obj[key]
|
||||||
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
|
if (_.isString(value) && value.toLowerCase().indexOf(q) !== -1) {
|
||||||
@ -112,12 +113,12 @@ module.exports = function(source) {
|
|||||||
if (_sort) {
|
if (_sort) {
|
||||||
_order = _order || 'ASC'
|
_order = _order || 'ASC'
|
||||||
|
|
||||||
array = _.sortBy(array, function(element) {
|
array = _.sortBy(array, function (element) {
|
||||||
return element[_sort];
|
return element[_sort]
|
||||||
})
|
})
|
||||||
|
|
||||||
if (_order === 'DESC') {
|
if (_order === 'DESC') {
|
||||||
array.reverse();
|
array.reverse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,20 +128,21 @@ module.exports = function(source) {
|
|||||||
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
res.setHeader('Access-Control-Expose-Headers', 'X-Total-Count')
|
||||||
}
|
}
|
||||||
|
|
||||||
_start = parseInt(_start) || 0
|
_start = parseInt(_start, 10) || 0
|
||||||
|
|
||||||
if (_end) {
|
if (_end) {
|
||||||
array = array.slice(_start, parseInt(_end))
|
_end = parseInt(_end, 10)
|
||||||
|
array = array.slice(_start, _end)
|
||||||
} else if (_limit) {
|
} else if (_limit) {
|
||||||
// Convert strings to int and sum to get end value
|
_limit = parseInt(_limit, 10)
|
||||||
array = array.slice(_start, parseInt(_start) + parseInt(_limit))
|
array = array.slice(_start, _start + _limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.jsonp(array)
|
res.jsonp(array)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /:resource/:id
|
// GET /:resource/:id
|
||||||
function show(req, res, next) {
|
function show (req, res, next) {
|
||||||
var resource = db(req.params.resource)
|
var resource = db(req.params.resource)
|
||||||
.get(utils.toNative(req.params.id))
|
.get(utils.toNative(req.params.id))
|
||||||
|
|
||||||
@ -152,7 +154,7 @@ module.exports = function(source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// POST /:resource
|
// POST /:resource
|
||||||
function create(req, res, next) {
|
function create (req, res, next) {
|
||||||
for (var key in req.body) {
|
for (var key in req.body) {
|
||||||
req.body[key] = utils.toNative(req.body[key])
|
req.body[key] = utils.toNative(req.body[key])
|
||||||
}
|
}
|
||||||
@ -165,7 +167,7 @@ module.exports = function(source) {
|
|||||||
|
|
||||||
// PUT /:resource/:id
|
// PUT /:resource/:id
|
||||||
// PATCH /:resource/:id
|
// PATCH /:resource/:id
|
||||||
function update(req, res, next) {
|
function update (req, res, next) {
|
||||||
for (var key in req.body) {
|
for (var key in req.body) {
|
||||||
req.body[key] = utils.toNative(req.body[key])
|
req.body[key] = utils.toNative(req.body[key])
|
||||||
}
|
}
|
||||||
@ -181,13 +183,13 @@ module.exports = function(source) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DELETE /:resource/:id
|
// DELETE /:resource/:id
|
||||||
function destroy(req, res, next) {
|
function destroy (req, res, next) {
|
||||||
db(req.params.resource).remove(utils.toNative(req.params.id))
|
db(req.params.resource).remove(utils.toNative(req.params.id))
|
||||||
|
|
||||||
// Remove dependents documents
|
// Remove dependents documents
|
||||||
var removable = utils.getRemovable(db.object)
|
var removable = utils.getRemovable(db.object)
|
||||||
|
|
||||||
_(removable).each(function(item) {
|
_(removable).each(function (item) {
|
||||||
db(item.name).remove(item.id)
|
db(item.name).remove(item.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
17
src/utils.js
17
src/utils.js
@ -7,7 +7,7 @@ _.mixin(_inflections)
|
|||||||
// Example:
|
// Example:
|
||||||
// 'true' -> true
|
// 'true' -> true
|
||||||
// '1' -> 1
|
// '1' -> 1
|
||||||
function toNative(value) {
|
function toNative (value) {
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
if (value === '' || value.trim() !== value) {
|
if (value === '' || value.trim() !== value) {
|
||||||
return value
|
return value
|
||||||
@ -21,11 +21,11 @@ function toNative(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return incremented id or uuid
|
// Return incremented id or uuid
|
||||||
function createId(coll) {
|
function createId (coll) {
|
||||||
if (_.isEmpty(coll)) {
|
if (_.isEmpty(coll)) {
|
||||||
return 1
|
return 1
|
||||||
} else {
|
} else {
|
||||||
var id = _.max(coll, function(doc) {
|
var id = _.max(coll, function (doc) {
|
||||||
return doc.id
|
return doc.id
|
||||||
}).id
|
}).id
|
||||||
|
|
||||||
@ -39,15 +39,14 @@ function createId(coll) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns document ids that have unsatisfied relations
|
// Returns document ids that have unsatisfied relations
|
||||||
// Example: a comment that references a post that doesn't exist
|
// Example: a comment that references a post that doesn't exist
|
||||||
function getRemovable(db) {
|
function getRemovable (db) {
|
||||||
var removable = []
|
var removable = []
|
||||||
|
|
||||||
_(db).each(function(coll, collName) {
|
_(db).each(function (coll, collName) {
|
||||||
_(coll).each(function(doc) {
|
_(coll).each(function (doc) {
|
||||||
_(doc).each(function(value, key) {
|
_(doc).each(function (value, key) {
|
||||||
if (/Id$/.test(key)) {
|
if (/Id$/.test(key)) {
|
||||||
var refName = _.pluralize(key.slice(0, -2))
|
var refName = _.pluralize(key.slice(0, -2))
|
||||||
// Test if table exists
|
// Test if table exists
|
||||||
@ -70,4 +69,4 @@ module.exports = {
|
|||||||
toNative: toNative,
|
toNative: toNative,
|
||||||
createId: createId,
|
createId: createId,
|
||||||
getRemovable: getRemovable
|
getRemovable: getRemovable
|
||||||
}
|
}
|
||||||
|
110
test/index.js
110
test/index.js
@ -1,14 +1,16 @@
|
|||||||
var request = require('supertest')
|
var request = require('supertest')
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var jsonServer = require('../src/')
|
var jsonServer = require('../src/')
|
||||||
var utils = require('../src/utils')
|
|
||||||
describe('Server', function() {
|
/* global beforeEach, describe, it */
|
||||||
|
|
||||||
|
describe('Server', function () {
|
||||||
|
|
||||||
var server
|
var server
|
||||||
var router
|
var router
|
||||||
var db
|
var db
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function () {
|
||||||
db = {}
|
db = {}
|
||||||
|
|
||||||
db.posts = [
|
db.posts = [
|
||||||
@ -40,8 +42,8 @@ describe('Server', function() {
|
|||||||
server.use(router)
|
server.use(router)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /db', function() {
|
describe('GET /db', function () {
|
||||||
it('should respond with json and full database', function(done) {
|
it('should respond with json and full database', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/db')
|
.get('/db')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -50,8 +52,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource', function() {
|
describe('GET /:resource', function () {
|
||||||
it('should respond with json and corresponding resources', function(done) {
|
it('should respond with json and corresponding resources', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts')
|
.get('/posts')
|
||||||
.set('Origin', 'http://example.com')
|
.set('Origin', 'http://example.com')
|
||||||
@ -62,15 +64,15 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/undefined')
|
.get('/undefined')
|
||||||
.expect(404, done)
|
.expect(404, done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?attr=&attr=', function() {
|
describe('GET /:resource?attr=&attr=', function () {
|
||||||
it('should respond with json and filter resources', function(done) {
|
it('should respond with json and filter resources', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?postId=1&published=true')
|
.get('/comments?postId=1&published=true')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -79,8 +81,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?q=', function() {
|
describe('GET /:resource?q=', function () {
|
||||||
it('should respond with json and make a full-text search', function(done) {
|
it('should respond with json and make a full-text search', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/tags?q=pho')
|
.get('/tags?q=pho')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -88,7 +90,7 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/tags?q=nope')
|
.get('/tags?q=nope')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -97,8 +99,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?_end=', function() {
|
describe('GET /:resource?_end=', function () {
|
||||||
it('should respond with a sliced array', function(done) {
|
it('should respond with a sliced array', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?_end=2')
|
.get('/comments?_end=2')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -109,8 +111,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?sort=', function() {
|
describe('GET /:resource?sort=', function () {
|
||||||
it('should respond with json and sort on a field', function(done) {
|
it('should respond with json and sort on a field', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/tags?_sort=body')
|
.get('/tags?_sort=body')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -118,7 +120,7 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should reverse sorting with _order=DESC', function(done) {
|
it('should reverse sorting with _order=DESC', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/tags?_sort=body&_order=DESC')
|
.get('/tags?_sort=body&_order=DESC')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -126,7 +128,7 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should sort on numerical field', function(done) {
|
it('should sort on numerical field', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts?_sort=id&_order=DESC')
|
.get('/posts?_sort=id&_order=DESC')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -135,8 +137,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?_start=&_end=', function() {
|
describe('GET /:resource?_start=&_end=', function () {
|
||||||
it('should respond with a sliced array', function(done) {
|
it('should respond with a sliced array', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?_start=1&_end=2')
|
.get('/comments?_start=1&_end=2')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -147,8 +149,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource?_start=&_limit=', function() {
|
describe('GET /:resource?_start=&_limit=', function () {
|
||||||
it('should respond with a limited array', function(done) {
|
it('should respond with a limited array', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/comments?_start=1&_limit=1')
|
.get('/comments?_start=1&_limit=1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -159,22 +161,21 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:parent/:parentId/:resource', function() {
|
describe('GET /:parent/:parentId/:resource', function () {
|
||||||
it('should respond with json and corresponding nested resources', function(done) {
|
it('should respond with json and corresponding nested resources', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts/1/comments')
|
.get('/posts/1/comments')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect([
|
.expect([
|
||||||
db.comments[0],
|
db.comments[0],
|
||||||
db.comments[1],
|
db.comments[1]
|
||||||
|
|
||||||
])
|
])
|
||||||
.expect(200, done)
|
.expect(200, done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /:resource/:id', function() {
|
describe('GET /:resource/:id', function () {
|
||||||
it('should respond with json and corresponding resource', function(done) {
|
it('should respond with json and corresponding resource', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts/1')
|
.get('/posts/1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -182,7 +183,7 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/refs/abcd-1234')
|
.get('/refs/abcd-1234')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -190,7 +191,7 @@ describe('Server', function() {
|
|||||||
.expect(200, done)
|
.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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/posts/9001')
|
.get('/posts/9001')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
@ -199,17 +200,16 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('POST /:resource', function () {
|
||||||
describe('POST /:resource', function() {
|
|
||||||
it('should respond with json, create a resource and increment id',
|
it('should respond with json, create a resource and increment id',
|
||||||
function(done) {
|
function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.post('/posts')
|
.post('/posts')
|
||||||
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
|
.send({body: 'foo', booleanValue: 'true', integerValue: '1'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
|
.expect({id: 3, body: 'foo', booleanValue: true, integerValue: 1})
|
||||||
.expect(201)
|
.expect(201)
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.equal(db.posts.length, 3)
|
assert.equal(db.posts.length, 3)
|
||||||
done()
|
done()
|
||||||
@ -217,13 +217,13 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should respond with json, create a resource and generate string id',
|
it('should respond with json, create a resource and generate string id',
|
||||||
function(done) {
|
function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.post('/refs')
|
.post('/refs')
|
||||||
.send({url: 'http://foo.com', postId: '1'})
|
.send({url: 'http://foo.com', postId: '1'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(201)
|
.expect(201)
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.equal(db.refs.length, 2)
|
assert.equal(db.refs.length, 2)
|
||||||
done()
|
done()
|
||||||
@ -231,15 +231,15 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('PUT /:resource/:id', function() {
|
describe('PUT /:resource/:id', function () {
|
||||||
it('should respond with json and update resource', function(done) {
|
it('should respond with json and update resource', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.put('/posts/1')
|
.put('/posts/1')
|
||||||
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
// assert it was created in database too
|
// assert it was created in database too
|
||||||
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||||
@ -247,7 +247,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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.put('/posts/9001')
|
.put('/posts/9001')
|
||||||
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
||||||
@ -257,15 +257,15 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('PATCH /:resource/:id', function() {
|
describe('PATCH /:resource/:id', function () {
|
||||||
it('should respond with json and update resource', function(done) {
|
it('should respond with json and update resource', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.patch('/posts/1')
|
.patch('/posts/1')
|
||||||
.send({body: 'bar'})
|
.send({body: 'bar'})
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect({id: 1, body: 'bar'})
|
.expect({id: 1, body: 'bar'})
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
// assert it was created in database too
|
// assert it was created in database too
|
||||||
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
|
assert.deepEqual(db.posts[0], {id: 1, body: 'bar'})
|
||||||
@ -273,7 +273,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', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.patch('/posts/9001')
|
.patch('/posts/9001')
|
||||||
.send({body: 'bar'})
|
.send({body: 'bar'})
|
||||||
@ -283,13 +283,13 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('DELETE /:resource/:id', function() {
|
describe('DELETE /:resource/:id', function () {
|
||||||
it('should respond with empty data, destroy resource and dependent resources', function(done) {
|
it('should respond with empty data, destroy resource and dependent resources', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.del('/posts/1')
|
.del('/posts/1')
|
||||||
.expect({})
|
.expect({})
|
||||||
.expect(200)
|
.expect(200)
|
||||||
.end(function(err, res) {
|
.end(function (err, res) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
assert.equal(db.posts.length, 1)
|
assert.equal(db.posts.length, 1)
|
||||||
assert.equal(db.comments.length, 3)
|
assert.equal(db.comments.length, 3)
|
||||||
@ -298,10 +298,10 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Static routes', function() {
|
describe('Static routes', function () {
|
||||||
|
|
||||||
describe('GET /', function() {
|
describe('GET /', function () {
|
||||||
it('should respond with html', function(done) {
|
it('should respond with html', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/')
|
.get('/')
|
||||||
.expect(/You're successfully running JSON Server/)
|
.expect(/You're successfully running JSON Server/)
|
||||||
@ -309,8 +309,8 @@ describe('Server', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('GET /stylesheets/style.css', function() {
|
describe('GET /stylesheets/style.css', function () {
|
||||||
it('should respond with css', function(done) {
|
it('should respond with css', function (done) {
|
||||||
request(server)
|
request(server)
|
||||||
.get('/stylesheets/style.css')
|
.get('/stylesheets/style.css')
|
||||||
.expect('Content-Type', /css/)
|
.expect('Content-Type', /css/)
|
||||||
@ -320,8 +320,8 @@ describe('Server', function() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Database #object', function() {
|
describe('Database #object', function () {
|
||||||
it('should be accessible', function() {
|
it('should be accessible', function () {
|
||||||
assert(router.db.object)
|
assert(router.db.object)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var utils = require('../src/utils')
|
var utils = require('../src/utils')
|
||||||
|
|
||||||
describe('utils', function() {
|
/* global describe, it */
|
||||||
|
|
||||||
describe('getRemovable', function() {
|
describe('utils', function () {
|
||||||
|
|
||||||
it('should return removable documents', function() {
|
describe('getRemovable', function () {
|
||||||
|
|
||||||
|
it('should return removable documents', function () {
|
||||||
|
|
||||||
var db = {
|
var db = {
|
||||||
posts: [
|
posts: [
|
||||||
@ -15,7 +17,7 @@ describe('utils', function() {
|
|||||||
{id: 1, postId: 1},
|
{id: 1, postId: 1},
|
||||||
// Comments below references a post that doesn't exist
|
// Comments below references a post that doesn't exist
|
||||||
{id: 2, postId: 2},
|
{id: 2, postId: 2},
|
||||||
{id: 3, postId: 2},
|
{id: 3, postId: 2}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,9 +31,9 @@ describe('utils', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('toNative', function() {
|
describe('toNative', function () {
|
||||||
|
|
||||||
it('should convert string to native type', function() {
|
it('should convert string to native type', function () {
|
||||||
// should convert
|
// should convert
|
||||||
assert.strictEqual(utils.toNative('1'), 1)
|
assert.strictEqual(utils.toNative('1'), 1)
|
||||||
assert.strictEqual(utils.toNative('true'), true)
|
assert.strictEqual(utils.toNative('true'), true)
|
||||||
@ -46,4 +48,4 @@ describe('utils', function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user