Last active
August 29, 2015 14:03
-
-
Save thebergamo/a475a009f7f9ff3a5ff4 to your computer and use it in GitHub Desktop.
Mocha + SuperTest + REST
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
describe('DELETE /api/user', function () { | |
it('Delete User Sucess', function (done) { | |
request('http://localhost:8080/api/') | |
.del('user/53b48b72f4211ee61908efac') | |
.expect(202, 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
describe('GET /api/users', function(){ | |
it('recovery users list', function(done){ | |
request('http://localhost:8080/api/') | |
.get('users') | |
.set('Content-Type', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if(('error' in res.body)) return "Error here!"; | |
}) | |
.expect(200, done); | |
}); | |
it('alias recovery user list - alias', function(done){ | |
request('http://localhost:8080/api/') | |
.get('user') | |
.set('Content-Type', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if(('error' in res.body)) return "Error here!"; | |
}) | |
.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
npm install -g mocha |
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
npm install --save 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
describe('POST /api/user', function () { | |
it('Create User Sucess', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(202, done); | |
}); | |
it('Create User Error email is required', function (done) { | |
var user = { | |
//"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "Email is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br" | |
/*"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
}*/ | |
}; | |
var error = { | |
"error": { | |
body: "Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error first name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
//"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "First Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error last name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos" | |
//"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "Last Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, 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
describe('PUT /api/user', function () { | |
it('Update User Sucess', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.put('user/53b48b72f4211ee61908efac') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if((res.body.length === 0 )) return "No data returned"; | |
if(!("marcos@thedon.com.br" === res.body.email)) return "Update not effect"; | |
if(!("Marcos" === res.body.name.first)) return "Update not effect"; | |
}) | |
.expect(202, 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 assert = require('assert'); | |
describe('API / USER endpoint', function(){ | |
describe('POST /api/user', function () { | |
it('Create User Sucess', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(202, done); | |
}); | |
it('Create User Error email is required', function (done) { | |
var user = { | |
//"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "Email is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br" | |
/*"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
}*/ | |
}; | |
var error = { | |
"error": { | |
body: "Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error first name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
//"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "First Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
it('Create User Error last name is required', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos" | |
//"last": "Bérgamo" | |
} | |
}; | |
var error = { | |
"error": { | |
body: "Last Name is required", | |
code: 400 | |
} | |
} | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.post('user') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(400, error, done); | |
}); | |
}); | |
describe('GET /api/users', function(){ | |
it('recovery users list', function(done){ | |
request('http://localhost:8080/api/') | |
.get('users') | |
.set('Content-Type', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if(('error' in res.body)) return "Error here!"; | |
}) | |
.expect(200, done); | |
}); | |
it('alias recovery user list - alias', function(done){ | |
request('http://localhost:8080/api/') | |
.get('user') | |
.set('Content-Type', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if(('error' in res.body)) return "Error here!"; | |
}) | |
.expect(200, done); | |
}); | |
}); | |
describe('PUT /api/user', function () { | |
it('Update User Sucess', function (done) { | |
var user = { | |
"email": "marcos@thedon.com.br", | |
"name":{ | |
"first": "Marcos", | |
"last": "Bérgamo" | |
} | |
}; | |
data = JSON.stringify(user); | |
request('http://localhost:8080/api/') | |
.put('user/53b48b72f4211ee61908efac') | |
.set('Content-Type', 'application/json') | |
.send(data) | |
.expect('Content-Type', /json/) | |
.expect(function(res){ | |
if((res.body.length === 0 )) return "No data returned"; | |
if(!("marcos@thedon.com.br" === res.body.email)) return "Update not effect"; | |
if(!("Marcos" === res.body.name.first)) return "Update not effect"; | |
}) | |
.expect(202, done); | |
}); | |
}); | |
describe('DELETE /api/user', function () { | |
it('Delete User Sucess', function (done) { | |
request('http://localhost:8080/api/') | |
.del('user/53b48b72f4211ee61908efac') | |
.expect(202, done); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment