Last active
March 22, 2016 21:29
-
-
Save s-leroux/999b36946bd7502645f1 to your computer and use it in GitHub Desktop.
"Introduction to Server-side Development" course by Jogesh Muppala on Coursera -- sample RESTful test with Mocha + Supertest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node-express", | |
"version": "1.0.0", | |
"description": "", | |
"main": "dishRouter.js", | |
"directories": { | |
"test": "test" | |
}, | |
"scripts": { | |
"test": "./node_modules/.bin/mocha", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"mocha": "^2.4.5", | |
"supertest": "^1.2.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('supertest'); | |
var SERVER = 'http://localhost:3000'; | |
describe('GET /dish', function(){ | |
it('respond with code 404 (wrong spelling)', function(done){ | |
request(SERVER) | |
.get('/dish') | |
.expect(404, done); | |
}) | |
}); | |
describe('GET /dishes', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.get('/dishes') | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /dishes', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.delete('/dishes') | |
.expect(200, done); | |
}) | |
}); | |
describe('POST /dishes', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.post('/dishes') | |
.set('Accept', 'application/json') | |
.send({'name':'Apple Pie', description:"America's favorite"}) | |
.expect(/Apple Pie/) | |
.expect(/America's favorite/) | |
.expect(200, done); | |
}) | |
}); | |
describe('GET /dishes/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.get('/dishes/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('PUT /dishes/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.put('/dishes/10') | |
.set('Accept', 'application/json') | |
.send({'name':'Apple Pie', description:"America's favorite"}) | |
.expect(/Apple Pie/) | |
.expect(/America's favorite/) | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /dishes/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.delete('/dishes/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('supertest'); | |
var SERVER = 'http://localhost:3000'; | |
describe('GET /leaderships', function(){ | |
it('respond with code 404 (wrong spelling)', function(done){ | |
request(SERVER) | |
.get('/leaderships') | |
.expect(404, done); | |
}) | |
}); | |
describe('GET /leadership', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.get('/leadership') | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /leadership', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.delete('/leadership') | |
.expect(200, done); | |
}) | |
}); | |
describe('POST /leadership', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.post('/leadership') | |
.set('Accept', 'application/json') | |
.send({'name':'Sylvain Leroux', description:"Web Bakery"}) | |
.expect(/Sylvain Leroux/) | |
.expect(/Web Bakery/) | |
.expect(200, done); | |
}) | |
}); | |
describe('GET /leadership/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.get('/leadership/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('PUT /leadership/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.put('/leadership/10') | |
.set('Accept', 'application/json') | |
.send({'name':'Sylvain Leroux', description:"Web Bakery"}) | |
.expect(/Sylvain Leroux/) | |
.expect(/Web Bakery/) | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /leadership/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.delete('/leadership/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('supertest'); | |
var SERVER = 'http://localhost:3000'; | |
describe('GET /promo', function(){ | |
it('respond with code 404 (wrong spelling)', function(done){ | |
request(SERVER) | |
.get('/promo') | |
.expect(404, done); | |
}) | |
}); | |
describe('GET /promotions', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.get('/promotions') | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /promotions', function(){ | |
it('respond with code 200', function(done){ | |
request(SERVER) | |
.delete('/promotions') | |
.expect(200, done); | |
}) | |
}); | |
describe('POST /promotions', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.post('/promotions') | |
.set('Accept', 'application/json') | |
.send({'name':'St Valentin Special', description:"All Pink Meals"}) | |
.expect(/St Valentin Special/) | |
.expect(/All Pink Meals/) | |
.expect(200, done); | |
}) | |
}); | |
describe('GET /promotions/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.get('/promotions/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('PUT /promotions/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.put('/promotions/10') | |
.set('Accept', 'application/json') | |
.send({'name':'Apple Pie', description:"America's favorite"}) | |
.send({'name':'St Valentin Special', description:"All Pink Meals"}) | |
.expect(/St Valentin Special/) | |
.expect(/All Pink Meals/) | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); | |
describe('DELETE /promotions/10', function(){ | |
it('respond with code 200 + data content', function(done){ | |
request(SERVER) | |
.delete('/promotions/10') | |
.set('Accept', 'application/json') | |
.expect(/10/) | |
.expect(200, done); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment