json-mock-kuitos
Version:
Mock a REST API just using JSON. An advanced fork of typicode/json-server
368 lines (331 loc) • 11.2 kB
JavaScript
var request = require('supertest')
var assert = require('assert')
var jsonServer = require('../src/')
var utils = require('../src/utils')
describe('Server', function() {
var server
var router
var db
beforeEach(function() {
db = {}
db.posts = [
{id: 1, body: 'foo'},
{id: 2, body: 'bar'}
]
db.tags = [
{id: 1, body: 'Technology'},
{id: 2, body: 'Photography'},
{id: 3, body: 'photo'}
]
db.comments = [
{id: 1, published: true, postId: 1},
{id: 2, published: false, postId: 1},
{id: 3, published: false, postId: 2},
{id: 4, published: false, postId: 2},
{id: 5, published: false, postId: 2}
]
db.subComments = [
{id: 1, moderated: true, commentId: 1},
{id: 2, moderated: false, commentId: 1},
{id: 3, moderated: true, commentId: 2},
{id: 4, moderated: false, commentId: 3},
{id: 5, moderated: false, commentId: 2}
]
db.subSubComments = [
{id: 1, active: true, subCommentId: 1},
{id: 2, active: false, subCommentId: 1},
{id: 3, active: true, subCommentId: 2},
{id: 4, active: false, subCommentId: 3},
{id: 5, active: false, subCommentId: 2}
]
db.refs = [
{id: 'abcd-1234', url: 'http://example.com', postId: 1}
]
server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults)
server.use(router)
})
describe('GET /db', function() {
it('should respond with json and full database', function(done) {
request(server)
.get('/db')
.expect('Content-Type', /json/)
.expect(db)
.expect(200, done)
})
})
describe('GET /:resource', function() {
it('should respond with json and corresponding resources', function(done) {
request(server)
.get('/posts')
.set('Origin', 'http://example.com')
.expect('Content-Type', /json/)
.expect('Access-Control-Allow-Credentials', 'true')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(db.posts)
.expect(200, done)
})
it('should respond with 404 if resource is not found', function(done) {
request(server)
.get('/undefined')
.expect(404, done)
})
})
describe('GET /:resource?attr=&attr=', function() {
it('should respond with json and filter resources', function(done) {
request(server)
.get('/comments?postId=1&published=true')
.expect('Content-Type', /json/)
.expect([db.comments[0]])
.expect(200, done)
})
})
describe('GET /:resource?q=', function() {
it('should respond with json and make a full-text search', function(done) {
request(server)
.get('/tags?q=pho')
.expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[2]])
.expect(200, done)
})
it('should return an empty array when nothing is matched', function(done) {
request(server)
.get('/tags?q=nope')
.expect('Content-Type', /json/)
.expect([])
.expect(200, done)
})
})
describe('GET /:resource?_end=', function() {
it('should respond with a sliced array', function(done) {
request(server)
.get('/comments?_end=2')
.expect('Content-Type', /json/)
.expect('x-total-count', db.comments.length.toString())
.expect('Access-Control-Expose-Headers', 'X-Total-Count')
.expect(db.comments.slice(0, 2))
.expect(200, done)
})
})
describe('GET /:resource?sort=', function() {
it('should respond with json and sort on a field', function(done) {
request(server)
.get('/tags?_sort=body')
.expect('Content-Type', /json/)
.expect([db.tags[1], db.tags[0], db.tags[2]])
.expect(200, done)
})
it('should reverse sorting with _order=DESC', function(done) {
request(server)
.get('/tags?_sort=body&_order=DESC')
.expect('Content-Type', /json/)
.expect([db.tags[2], db.tags[0], db.tags[1]])
.expect(200, done)
})
it('should sort on numerical field', function(done) {
request(server)
.get('/posts?_sort=id&_order=DESC')
.expect('Content-Type', /json/)
.expect(db.posts.reverse())
.expect(200, done)
})
})
describe('GET /:resource?_start=&_end=', function() {
it('should respond with a sliced array', function(done) {
request(server)
.get('/comments?_start=1&_end=2')
.expect('Content-Type', /json/)
.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) {
request(server)
.get('/comments?_start=1&_limit=1')
.expect('Content-Type', /json/)
.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 /:parent/:parentId/:resource/(:id)', function() {
it('should respond with json and corresponding nested resources (1 level - no id)', function(done) {
request(server)
.get('/posts/1/comments')
.expect('Content-Type', /json/)
.expect([
db.comments[0],
db.comments[1]
])
.expect(200, done)
})
it('should respond with json and corresponding nested resource (1 level - with id)', function(done) {
request(server)
.get('/posts/1/comments/2')
.expect('Content-Type', /json/)
.expect(db.comments[1])
.expect(200, done)
})
})
describe('GET /:parent/:parentId/:resource/:id/:subResource/(:subId)', function() {
it('should respond with json and corresponding nested resources (2 levels - no id)', function(done) {
request(server)
.get('/posts/1/comments/2/subComments')
.expect('Content-Type', /json/)
.expect([
db.subComments[2],
db.subComments[4]
])
.expect(200, done)
})
it('should respond with json and corresponding nested resource (2 levels - with id)', function(done) {
request(server)
.get('/posts/1/comments/2/subComments/3')
.expect('Content-Type', /json/)
.expect(db.subComments[2])
.expect(200, done)
})
})
describe('GET /:resource/:id', function() {
it('should respond with json and corresponding resource', function(done) {
request(server)
.get('/posts/1')
.expect('Content-Type', /json/)
.expect(db.posts[0])
.expect(200, done)
})
it('should support string id, respond with json and corresponding resource', function(done) {
request(server)
.get('/refs/abcd-1234')
.expect('Content-Type', /json/)
.expect(db.refs[0])
.expect(200, done)
})
it('should respond with 404 if resource is not found', function(done) {
request(server)
.get('/posts/9001')
.expect('Content-Type', /json/)
.expect({})
.expect(404, done)
})
})
describe('POST /:resource', function() {
it('should respond with json, create a resource and increment id',
function(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(200)
.end(function(err, res) {
if (err) return done(err)
assert.equal(db.posts.length, 3)
done()
})
})
it('should respond with json, create a resource and generate string id',
function(done) {
request(server)
.post('/refs')
.send({url: 'http://foo.com', postId: '1'})
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err)
assert.equal(db.refs.length, 2)
done()
})
})
})
describe('PUT /:resource/:id', function() {
it('should respond with json and update resource', function(done) {
request(server)
.put('/posts/1')
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
.expect('Content-Type', /json/)
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1})
.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})
done()
})
})
it('should respond with 404 if resource is not found', function(done) {
request(server)
.put('/posts/9001')
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'})
.expect('Content-Type', /json/)
.expect({})
.expect(404, done)
})
})
describe('PATCH /:resource/:id', function() {
it('should respond with json and update resource', function(done) {
request(server)
.patch('/posts/1')
.send({body: 'bar'})
.expect('Content-Type', /json/)
.expect({id: 1, body: 'bar'})
.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'})
done()
})
})
it('should respond with 404 if resource is not found', function(done) {
request(server)
.patch('/posts/9001')
.send({body: 'bar'})
.expect('Content-Type', /json/)
.expect({})
.expect(404, done)
})
})
describe('DELETE /:resource/:id', function() {
it('should respond with empty data, destroy resource and dependent resources', function(done) {
request(server)
.del('/posts/1')
.expect({})
.expect(200)
.end(function(err, res) {
if (err) return done(err)
assert.equal(db.posts.length, 1)
assert.equal(db.comments.length, 3)
done()
})
})
})
describe('Static routes', function() {
describe('GET /', function() {
it('should respond with html', function(done) {
request(server)
.get('/')
.expect(/You're successfully running JSON Server/)
.expect(200, done)
})
})
describe('GET /stylesheets/style.css', function() {
it('should respond with css', function(done) {
request(server)
.get('/stylesheets/style.css')
.expect('Content-Type', /css/)
.expect(200, done)
})
})
})
describe('Database #object', function() {
it('should be accessible', function() {
assert(router.db.object)
})
})
})