mirror of
https://github.com/typicode/json-server.git
synced 2025-08-01 09:13:32 +08:00
Fix PUT should replace resource
This commit is contained in:
@ -21,7 +21,7 @@
|
||||
"morgan": "^1.3.1",
|
||||
"node-uuid": "^1.4.2",
|
||||
"pluralize": "^1.1.2",
|
||||
"underscore-db": "^0.9.0",
|
||||
"underscore-db": "^0.9.1",
|
||||
"update-notifier": "^0.5.0",
|
||||
"yargs": "^3.10.0"
|
||||
},
|
||||
|
@ -215,8 +215,11 @@ module.exports = function (db, name) {
|
||||
req.body[key] = utils.toNative(req.body[key])
|
||||
}
|
||||
|
||||
var resource = db(name)
|
||||
.updateById(utils.toNative(req.params.id), req.body)
|
||||
var id = utils.toNative(req.params.id)
|
||||
|
||||
var resource = req.method === 'PATCH' ?
|
||||
db(name).updateById(id, req.body) :
|
||||
db(name).replaceById(id, req.body)
|
||||
|
||||
if (resource) {
|
||||
res.locals.data = resource
|
||||
|
@ -1,5 +1,4 @@
|
||||
var express = require('express')
|
||||
var utils = require('../utils')
|
||||
|
||||
module.exports = function (db, name) {
|
||||
|
||||
@ -11,18 +10,19 @@ module.exports = function (db, name) {
|
||||
}
|
||||
|
||||
function create (req, res, next) {
|
||||
for (var prop in req.body) {
|
||||
req.body[prop] = utils.toNative(req.body[prop])
|
||||
}
|
||||
|
||||
res.locals.data = db.object[name] = req.body
|
||||
res.status(201)
|
||||
next()
|
||||
}
|
||||
|
||||
function update (req, res, next) {
|
||||
if (req.method === 'PUT') {
|
||||
delete db.object[name]
|
||||
db.object[name] = {}
|
||||
}
|
||||
|
||||
for (var prop in req.body) {
|
||||
db.object[name][prop] = utils.toNative(req.body[prop])
|
||||
db.object[name][prop] = req.body[prop]
|
||||
}
|
||||
|
||||
res.locals.data = db.object[name]
|
||||
|
@ -408,17 +408,19 @@ describe('Server', function () {
|
||||
})
|
||||
|
||||
describe('PUT /:resource/:id', function () {
|
||||
it('should respond with json and update resource', function (done) {
|
||||
it('should respond with json and replace resource', function (done) {
|
||||
var post = {id: 1, booleanValue: true, integerValue: 1}
|
||||
request(server)
|
||||
.put('/posts/1')
|
||||
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
|
||||
// body property omitted to test that the resource is replaced
|
||||
.send({id: 1, booleanValue: 'true', integerValue: '1'})
|
||||
.expect('Content-Type', /json/)
|
||||
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
|
||||
.expect(post)
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) return done(err)
|
||||
// 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], post)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
@ -44,18 +44,18 @@ describe('Server', function () {
|
||||
})
|
||||
|
||||
describe('PUT /:resource', function () {
|
||||
it('should uptade resource', function (done) {
|
||||
it('should update resource', function (done) {
|
||||
var user = { name: 'bar' }
|
||||
request(server)
|
||||
.put('/user')
|
||||
.send(user)
|
||||
.expect(db.user)
|
||||
.expect(user)
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('PATCH /:resource', function () {
|
||||
it('should uptade resource', function (done) {
|
||||
it('should update resource', function (done) {
|
||||
request(server)
|
||||
.patch('/user')
|
||||
.send({ name: 'bar' })
|
||||
|
Reference in New Issue
Block a user