From 3c2a3c996e12917d52af72fc07715807fba9573e Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 17 May 2016 18:58:38 -0700 Subject: [PATCH 1/7] basic server set up and working --- .gitignore | 1 + gulpfile.js | 0 http_server.js | 29 +++++++++++++++++++++++++++++ package.json | 25 +++++++++++++++++++++++++ test/http_test.js | 0 5 files changed, 55 insertions(+) create mode 100644 .gitignore create mode 100644 gulpfile.js create mode 100644 http_server.js create mode 100644 package.json create mode 100644 test/http_test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..e69de29 diff --git a/http_server.js b/http_server.js new file mode 100644 index 0000000..f24bf22 --- /dev/null +++ b/http_server.js @@ -0,0 +1,29 @@ +'use strict'; +const http = require('http'); +const names = []; + +http.createServer((req, res) => { + if (req.url === '/time') { + let date = new Date(); + res.write('Current date: ' + date.toString() + '\n'); + return res.end(); + } + + if (req.method === 'GET' && req.url.indexOf('/greet/') !== -1) { + let name = req.url.split('/').pop(); + res.write('Hello ' + name); + return res.end(); + } + + if (req.method === 'POST' && req.url.indexOf('/greet/') !== -1) { + let JSONname = JSON.stringify(req.url.split('/').pop()); + names.push(JSONname); + res.write('Successfully saved as JSON'); + console.log(names); + return res.end(); + } + res.write('not working'); + res.end(); +}).listen(3000, () => { + console.log('listening'); +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..acad1e1 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "stefanie-hansen", + "version": "1.0.0", + "description": "##Description", + "main": "gulpfile.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/stefuhnee/basic_http_server.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/stefuhnee/basic_http_server/issues" + }, + "homepage": "https://github.com/stefuhnee/basic_http_server#readme", + "dependencies": { + } +} diff --git a/test/http_test.js b/test/http_test.js new file mode 100644 index 0000000..e69de29 From 13dfcddfc6f42517ee9ba3d800f7a074432947d4 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 17 May 2016 19:04:46 -0700 Subject: [PATCH 2/7] added gulpfile contents and handling 404 --- gulpfile.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ http_server.js | 8 ++++--- package.json | 9 ++++++- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index e69de29..09a09b9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -0,0 +1,65 @@ +'use strict'; + +const gulp = require('gulp'); +const mocha = require('gulp-mocha'); +const lint = require('gulp-eslint'); +const opts = { + 'extends': 'eslint:recommended', + 'ecmaFeatures': { + 'modules': true + }, + 'rules': { + 'no-alert': 0, + 'no-bitwise': 0, + 'camelcase': 1, + 'no-console': 1, + 'curly': 1, + 'eqeqeq': 0, + 'no-eq-null': 0, + 'guard-for-in': 1, + 'no-empty': 1, + 'no-use-before-define': 0, + 'no-obj-calls': 2, + 'no-unused-vars': 0, + 'new-cap': 1, + 'no-shadow': 0, + 'strict': 1, + 'no-invalid-regexp': 2, + 'comma-dangle': 2, + 'no-undef': 1, + 'no-new': 1, + 'no-extra-semi': 1, + 'no-debugger': 2, + 'no-caller': 1, + 'semi': 1, + 'quotes': 0, + 'no-unreachable': 2 + }, + 'globals': { + '$': false + }, + 'env': { + 'node': true, + 'es6': true + } +}; + +gulp.task('linter' , () => { + return gulp.src('./**/*.js') + .pipe(lint(opts)) + .pipe(lint.format()); +}); + +gulp.task('tests', () => { + return gulp.src('./test/http-test.js', {read: false}) + .pipe(mocha({reporter: 'spec'})); +}); + +gulp.task('watch', () => { + gulp.watch('./test/chat-test.js', ['linter', 'tests']); + gulp.watch('./**/*.js', ['linter']); +}); + +gulp.task('default', ['watch', 'linter', 'tests'], () => { + console.log('All tasks completed successfully'); +}); diff --git a/http_server.js b/http_server.js index f24bf22..c8c4861 100644 --- a/http_server.js +++ b/http_server.js @@ -22,8 +22,10 @@ http.createServer((req, res) => { console.log(names); return res.end(); } - res.write('not working'); - res.end(); + res.write('NOT FOUND'); + res.writeHead(404, { + 'Content-Type': 'text/html' + }); }).listen(3000, () => { console.log('listening'); -}) +}); diff --git a/package.json b/package.json index acad1e1..9c85e3d 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,13 @@ "url": "https://github.com/stefuhnee/basic_http_server/issues" }, "homepage": "https://github.com/stefuhnee/basic_http_server#readme", - "dependencies": { + "dependencies": {}, + "devDependencies": { + "chai": "^3.5.0", + "chai-http": "^2.0.1", + "gulp": "^3.9.1", + "gulp-eslint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "mocha": "^2.4.5" } } From 85a2341d1d9f9e196dac02987f0e5ee989fe1401 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 17 May 2016 20:09:11 -0700 Subject: [PATCH 3/7] finished tests except catch all isn't working --- http_server.js | 7 ++++--- package.json | 3 ++- test/http_test.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/http_server.js b/http_server.js index c8c4861..c4cfc80 100644 --- a/http_server.js +++ b/http_server.js @@ -18,14 +18,15 @@ http.createServer((req, res) => { if (req.method === 'POST' && req.url.indexOf('/greet/') !== -1) { let JSONname = JSON.stringify(req.url.split('/').pop()); names.push(JSONname); + res.writeHead(200, {name: JSONname}); res.write('Successfully saved as JSON'); console.log(names); return res.end(); } + res.write('NOT FOUND'); - res.writeHead(404, { - 'Content-Type': 'text/html' - }); + res.writeHead(404, {'Content-Type': 'text/html'}); + res.end(); }).listen(3000, () => { console.log('listening'); }); diff --git a/package.json b/package.json index 9c85e3d..94d18d0 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "gulp": "^3.9.1", "gulp-eslint": "^2.0.0", "gulp-mocha": "^2.2.0", - "mocha": "^2.4.5" + "mocha": "^2.4.5", + "moment": "^2.13.0" } } diff --git a/test/http_test.js b/test/http_test.js index e69de29..2577ee6 100644 --- a/test/http_test.js +++ b/test/http_test.js @@ -0,0 +1,52 @@ +'use strict'; + +const chai = require('chai'); +const expect = require('chai').expect; +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const request = chai.request; +const moment = require('moment'); +require('../http_server.js'); + +describe('HTTP server tests', () => { + it('should respond to a request with a url ending in /date with the current date', (done) => { + request('localhost:3000') + .get('/time') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + let dateString = res.text.split(':').slice(1).join('').trim(); + expect(moment(dateString, 'ddd MMM DD YYYY HH:mm:ss').isValid()).to.eql(true); + done(); + }); + }); + it('should respond to a GET request to /greet/* with a greeting including the single-word string at the end of the path', (done) => { + request('localhost:3000') + .get('/greet/test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('Hello test'); + done(); + }); + }); + it('should respond to a POST request to /greet/* by turning the single-word string at the end of the path into JSON', (done) => { + request('localhost:3000') + .post('/greet/test') + .end((err, res) => { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res).to.have.header('name', '"test"'); + done(); + }); + }); + it('should respond to any other paths with an error', (done) => { + request('localhost:3000') + .get('/random') + .end((err, res) => { + expect(res).to.have.status(404); + expect(res.text).to.eql('NOT FOUND'); + done(); + }); + }); +}); From dff7b51da50617ce2a47587c7a378a3a410872bc Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Tue, 17 May 2016 20:11:03 -0700 Subject: [PATCH 4/7] fixed gulp path --- gulpfile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 09a09b9..25fa30d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -51,12 +51,12 @@ gulp.task('linter' , () => { }); gulp.task('tests', () => { - return gulp.src('./test/http-test.js', {read: false}) + return gulp.src('./test/http_test.js', {read: false}) .pipe(mocha({reporter: 'spec'})); }); gulp.task('watch', () => { - gulp.watch('./test/chat-test.js', ['linter', 'tests']); + gulp.watch('./test/http_test.js', ['linter', 'tests']); gulp.watch('./**/*.js', ['linter']); }); From 52bd0aad47ca728e7a02fc4a5365964e279e64fe Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 18 May 2016 09:34:07 -0700 Subject: [PATCH 5/7] fixed error with catch-all random route test --- http_server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http_server.js b/http_server.js index c4cfc80..e5634b8 100644 --- a/http_server.js +++ b/http_server.js @@ -24,8 +24,8 @@ http.createServer((req, res) => { return res.end(); } - res.write('NOT FOUND'); res.writeHead(404, {'Content-Type': 'text/html'}); + res.write('NOT FOUND'); res.end(); }).listen(3000, () => { console.log('listening'); From 4014f40a3c6e9d4ea07fd2200b1e1ee7a13eef59 Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 18 May 2016 12:34:59 -0700 Subject: [PATCH 6/7] working on dealing with the json object after clarification in post request --- http_server.js | 24 +++++++++++++----------- test/http_test.js | 9 ++++++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/http_server.js b/http_server.js index e5634b8..f5b7209 100644 --- a/http_server.js +++ b/http_server.js @@ -1,6 +1,5 @@ 'use strict'; const http = require('http'); -const names = []; http.createServer((req, res) => { if (req.url === '/time') { @@ -15,18 +14,21 @@ http.createServer((req, res) => { return res.end(); } - if (req.method === 'POST' && req.url.indexOf('/greet/') !== -1) { - let JSONname = JSON.stringify(req.url.split('/').pop()); - names.push(JSONname); - res.writeHead(200, {name: JSONname}); - res.write('Successfully saved as JSON'); - console.log(names); - return res.end(); + if (req.method === 'POST' && req.url === '/greet') { + let body = ''; + req.on('data', (data) => { + body += data.toString(); + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write('Name sent: ' + JSON.parse(body).name); + res.end(); + }); } - res.writeHead(404, {'Content-Type': 'text/html'}); - res.write('NOT FOUND'); - res.end(); + else { + res.writeHead(404, {'Content-Type': 'text/html'}); + res.write('NOT FOUND'); + res.end(); + } }).listen(3000, () => { console.log('listening'); }); diff --git a/test/http_test.js b/test/http_test.js index 2577ee6..9e47272 100644 --- a/test/http_test.js +++ b/test/http_test.js @@ -30,13 +30,16 @@ describe('HTTP server tests', () => { done(); }); }); - it('should respond to a POST request to /greet/* by turning the single-word string at the end of the path into JSON', (done) => { + it('should respond to a POST request to /greet by recognizing the JSON object and writing it back to the server', (done) => { request('localhost:3000') - .post('/greet/test') + .post('/greet') + .set('Content-Type', 'application/json') + .send({"name": "test"}) .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); - expect(res).to.have.header('name', '"test"'); + expect(res).to.have.header('Content-Type', 'application/json'); + expect(res).to.eql('Name sent: test'); done(); }); }); From 9a6ec0029990b790c4ed9f9583a6bbba5760976d Mon Sep 17 00:00:00 2001 From: Stefanie Hansen Date: Wed, 18 May 2016 12:51:45 -0700 Subject: [PATCH 7/7] OMG PASSING TESTS --- http_server.js | 9 ++++++--- test/http_test.js | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/http_server.js b/http_server.js index f5b7209..88e621a 100644 --- a/http_server.js +++ b/http_server.js @@ -18,9 +18,12 @@ http.createServer((req, res) => { let body = ''; req.on('data', (data) => { body += data.toString(); - res.writeHead(200, {'Content-Type': 'application/json'}); - res.write('Name sent: ' + JSON.parse(body).name); - res.end(); + req.on('end', () => { + res.writeHead(200, {'Content-Type': 'text/html'}); + let name = JSON.parse(body).name; + res.write('Name sent: ' + name); + return res.end(); + }) }); } diff --git a/test/http_test.js b/test/http_test.js index 9e47272..d91bac1 100644 --- a/test/http_test.js +++ b/test/http_test.js @@ -34,12 +34,12 @@ describe('HTTP server tests', () => { request('localhost:3000') .post('/greet') .set('Content-Type', 'application/json') - .send({"name": "test"}) + .send({"name":"test"}) .end((err, res) => { expect(err).to.eql(null); expect(res).to.have.status(200); - expect(res).to.have.header('Content-Type', 'application/json'); - expect(res).to.eql('Name sent: test'); + expect(res).to.have.header('Content-Type', 'text/html'); + expect(res.text).to.eql('Name sent: test'); done(); }); });