From f122e33cdf0d0b4205bccc0b81dda457c20e28de Mon Sep 17 00:00:00 2001 From: Dustin McBride Date: Wed, 18 May 2016 11:08:45 -0700 Subject: [PATCH] added more test and gulp --- .gitignore | 2 ++ dustin/gulpfile.js | 25 +++++++++++++ dustin/package.json | 20 +++++++++++ dustin/server.js | 73 ++++++++++++++++++++++++++++++++++++++ dustin/test/server_test.js | 63 ++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 .gitignore create mode 100644 dustin/gulpfile.js create mode 100644 dustin/package.json create mode 100644 dustin/server.js create mode 100644 dustin/test/server_test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e072c42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dustin/node_modules +.vscode diff --git a/dustin/gulpfile.js b/dustin/gulpfile.js new file mode 100644 index 0000000..7ec13c7 --- /dev/null +++ b/dustin/gulpfile.js @@ -0,0 +1,25 @@ +var gulp = require('gulp'); +var eslint = require('gulp-eslint'); +var mocha = require('gulp-mocha'); +var watch = require('gulp-watch'); + +gulp.task('watch', function () { + gulp.watch(['*.js', '../test/*.js'], ['lint', 'mocha']); +}); + +gulp.task('default', ['mocha', 'lint', 'watch'], function () {}); + +gulp.task('lint', function () { + gulp.src(['./*.js', './test/*.js', './lib/*.js']) + .pipe(eslint()) //{} pass in rules + .pipe(eslint.format()); +}); + +gulp.task('mocha', function () { + return gulp.src('./test/*.js', { + read: false + }) + .pipe(mocha({ + reporter: 'nyan' + })); +}); diff --git a/dustin/package.json b/dustin/package.json new file mode 100644 index 0000000..25f5c1f --- /dev/null +++ b/dustin/package.json @@ -0,0 +1,20 @@ +{ + "name": "basic_http_server", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^3.5.0", + "chai-http": "^2.0.1", + "gulp": "^3.9.1", + "gulp-eslint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "gulp-watch": "^4.3.5", + "mocha": "^2.4.5" + } +} diff --git a/dustin/server.js b/dustin/server.js new file mode 100644 index 0000000..ff5986a --- /dev/null +++ b/dustin/server.js @@ -0,0 +1,73 @@ +var http = require('http'); +savedJSON = []; + +http.createServer(function (req, res) { + + + res.processed = false; + url_array = req.url.split('/'); + if (req.method == 'GET' && url_array[1] == 'time') urlTime(res); + if (req.method == 'GET' && url_array[1] == 'greet' && (url_array[2])) urlName(res); + if (req.method == 'POST' && url_array[1] == 'greet') urlNamePost(res, req); + if (res.processed === false) url404(res); + +}).listen(3000); + +url404 = function (res) { + res.writeHead(404, { + 'Content-Type': 'text/html' + }); + res.write('Not found'); + res.end(); +}; + +urlTime = function (res) { + res.processed = true; + res.write((new Date).toString()); + res.end(); + return; +}; + +urlNamePost = function (res, req) { + res.processed = true; + var body = ''; + req.on('data', function (data) { + body += data; + }); + req.on('end', function () { + if (req.headers['content-type'] === 'application/json') { + parseJSON(body); + goodRequest(res); + } else { + badRequest(res); + } + }); +}; + +urlName = function (res) { + res.processed = true; + res.write('Hello, ' + url_array[2]); + res.end(); + return; +}; + +badRequest = function (res) { + res.processed = true; + res.writeHead(400, { + 'Content-Type': 'text/html' + }); + res.write('BAD REQUEST'); + res.end(); +}; + +goodRequest = function (res) { + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + res.write('request accepted'); + res.end(); +}; + +parseJSON = function (body) { + savedJSON.push(JSON.parse(body)); +}; diff --git a/dustin/test/server_test.js b/dustin/test/server_test.js new file mode 100644 index 0000000..42fd10a --- /dev/null +++ b/dustin/test/server_test.js @@ -0,0 +1,63 @@ +chai = require('chai'); +chaiHTTP = require('chai-http'); +expect = chai.expect; +chai.use(chaiHTTP); +request = chai.request; + +require('../server'); + +//The server should respond to a request to /time that will send back +//the current time of the server. +describe('HTTP server', function () { + it('/time should return the system time (within 1 second)', function (done) { + request('localhost:3000') + .get('/time') + .end(function (err, res) { + expect(Date.now() - Date.parse(res.text)).to.be.below(1000); + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + + //It should also respond to a get request to /greet/name where name is any + //single word string. It should send back a string that greets that name. + it('/greet/[name] should return a personal greet message', function (done) { + request('localhost:3000') + .get('/greet/jeffrey_lebowski') + .end(function (err, res) { + expect(res.text).to.eql('Hello, jeffrey_lebowski'); + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); + + //It should also have a separate post request to /greet that takes the name in JSON format. + it('/greet should take a JSON post', function (done) { + request('localhost:3000') + .post('/greet') + .set('Content-Type', 'application/json') + .send({ + 'name': 'walter sobchak' + }) + .end(function (err, res) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(savedJSON).to.eql([{ + 'name': 'walter sobchak' + }]); + done(); + }); + }); + + it('/greet should take not take a non JSON post', function (done) { + request('localhost:3000') + .post('/greet') + .set('Content-Type', 'text/html') + .end(function (err, res) { + expect(res).to.have.status(400); + done(); + }); + }); +});