Skip to content

Instantly share code, notes, and snippets.

@imposibrus
Last active December 15, 2015 19:12
Show Gist options
  • Save imposibrus/38bf0d4618bed1fbe03a to your computer and use it in GitHub Desktop.
Save imposibrus/38bf0d4618bed1fbe03a to your computer and use it in GitHub Desktop.
Tests
var server = require('../../../bin/www'),
request = require('supertest'),
models = require('../../../models');
describe('Feedback', function() {
it('should all fields be required', function(done) {
request(server)
.post('/api/feedback')
.expect('Content-type', /application\/json/)
.expect(400)
.end(function(err, res) {
if(err) {
throw err;
}
res.body.status.should.be.equal(400);
res.body.message.should.be.equal('Path(s) `name`, `email`, `phone`, `text` is(are) required.');
done();
});
});
it('should `name` field be required', function(done) {
request(server)
.post('/api/feedback')
.send({email: 'email', phone: 'phone', text: 'text'})
.expect('Content-type', /application\/json/)
.expect(400)
.end(function(err, res) {
if(err) {
throw err;
}
res.body.status.should.be.equal(400);
res.body.message.should.be.equal('Path(s) `name` is(are) required.');
done();
});
});
});
var server = require('../bin/www'),
request = require('supertest');
describe('GET /', function() {
it('should respond with main page', function(done) {
request(server)
.get('/')
.expect('Content-Type', /text\/html/)
.expect(200)
.end(done);
});
});
describe('GET /404', function() {
it('should respond with 404 error page', function(done) {
request(server)
.get('/404')
.expect('Content-Type', /text\/html/)
.expect(404)
.end(done);
});
});
--require ./testSetup
--timeout 5000
--bail
/**
* Normalize phone number
* @param {String|Array} phone
* @returns {Boolean|String}
*/
module.exports = function normalizePhone(phone) {
if(!phone) {
return false;
}
phone = phone.replace(/\D+/g, '').split('');
if(phone.length != 11) {
return false;
}
if(phone[0] == '8') {
phone[0] = '7';
}
return phone.join('');
};
var normalizePhone = require('../../lib/normalizePhone');
describe('normalize phone', function() {
it('should remove + characters', function() {
normalizePhone('+79297285597').should.be.equal('79297285597');
});
it('should remove - characters', function() {
normalizePhone('7-929-728-55-97').should.be.equal('79297285597');
});
it('should remove + and - characters', function() {
normalizePhone('+7-929-728-55-97').should.be.equal('79297285597');
});
it('should remove spaces', function() {
normalizePhone('+7 929 728 55 97').should.be.equal('79297285597');
});
it('should replace 8 to 7', function() {
normalizePhone('8 929 728 55 97').should.be.equal('79297285597');
});
it('should return false if no number provided', function() {
normalizePhone('').should.be.equal(false);
});
it('should return false if too short number provided', function() {
normalizePhone('+7-222-22-22').should.be.equal(false);
});
it('should return false if too long number provided', function() {
normalizePhone('+7-222-22-22-22-22-22').should.be.equal(false);
});
});
require('should');
#!/usr/bin/env node
var newrelic = require('newrelic');
/**
* Module dependencies.
*/
var app = require('../app'),
debug = require('debug')('udacha72:server'),
http = require('http'),
config = require('../lib/config');
process.title = config.get('title');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(config.get('PORT'));
app.set('port', port);
app.locals.newrelic = newrelic;
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
module.exports = server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
process.on('message', function(message) {
if(message == 'shutdown') {
server.close(function() {});
process.exit(0);
}
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
if (process.send) process.send('online');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment