diff --git a/package.json b/package.json index ead8089..b88b31f 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "shelly": "0.0.3" }, "devDependencies": { - "vows": ">=0.7.0" + "vows": ">=0.7.0", + "nock": "*" }, "main": "./index.js", "repository": { diff --git a/tests/test_parse_gpx.js b/tests/test_parse_gpx.js index 676e654..0258e98 100644 --- a/tests/test_parse_gpx.js +++ b/tests/test_parse_gpx.js @@ -2,50 +2,50 @@ var assert = require('assert'); var gpx = require('../lib/gpx-parser.js'); var vows = require('vows'); - +var nock = require('nock'); /* For testing url based gpx data */ var http = require('http'); var fs = require('fs'); -var PORT = 8080; -var HOST = '127.0.0.1'; -http.createServer(function(req, res) { - if(req.url == '/data.gpx') { - fs.readFile('./tests/data/data.gpx', function(err, data) { - if(err) { - throw err; - } - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end(data); - }); - } else if(req.url == '/bad.gpx') { - res.writeHead(404, { - 'Content-Type' : 'text/html' - }); - res.end('Hey bad request!'); - } else if(req.url == '/soft404.gpx') { - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end('This is a soft 404 which has 200 as status code'); - } else if(req.url == '/broken.gpx') { - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end(['', '', '', 'Garmin Connect', '', ' ', ' ', '', 'Untitled', ' ', '', '69.4000015258789', '', '', '', '69.5999984741211', ' ', '', ''].join()); - } else if (req.url == '/image.gpx') { - fs.readFile('./tests/data/loading.gif', function(err, data) { - if(err) { - throw err; - } - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end(data); - }); +var HOST = 'http://fakedomain.tld'; +fs.readFile('./tests/data/data.gpx', function(err, data) { + if(err) { + throw err; + } + nock(HOST) + .get('/data.gpx') + .reply(200, data, { + 'Content-Type' : 'application/xml' + }); +}); +nock(HOST) +.get('/bad.gpx') +.reply(404, 'Hey bad request!',{ + 'Content-Type' : 'text/html' +}); + +nock(HOST) +.get('/soft404.gpx') +.reply(200, 'This is a soft 404 which has 200 as status code',{ + 'Content-Type' : 'text/xml' +}); + +nock(HOST) +.get('/broken.tcx') +.reply(200, ['', '', '', 'Garmin Connect', '', ' ', ' ', '', 'Untitled', ' ', '', '69.4000015258789', '', '', '', '69.5999984741211', ' ', '', ''].join(),{ + 'Content-Type' : 'text/xml' +}); + + +fs.readFile('./tests/data/loading.gif', function(err, data) { + if(err) { + throw err; } -}).listen(PORT, HOST); + nock(HOST) + .get('/image.gpx') + .reply(200, data,{ + 'Content-Type' : 'application/xml' + }); +}); vows.describe('Test suite for parsing gpx').addBatch({ 'Parse broken gpx data' : { @@ -118,7 +118,7 @@ vows.describe('Test suite for parsing gpx').addBatch({ }, 'Parse gpx URL' : { 'topic' : function() { - gpx.gpxParseURL('http://' + HOST + ':' + PORT + '/data.gpx', this.callback); + gpx.gpxParseURL(HOST + '/data.gpx', this.callback); }, 'Should return an array of two tracking points' : function(err, result) { assert.deepEqual(result, [{ @@ -140,7 +140,7 @@ vows.describe('Test suite for parsing gpx').addBatch({ }, 'Parse bad gpx URL' : { 'topic' : function() { - gpx.gpxParseURL('http://' + HOST + ':' + PORT + '/bad.gpx', this.callback); + gpx.gpxParseURL(HOST + '/bad.gpx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -148,7 +148,7 @@ vows.describe('Test suite for parsing gpx').addBatch({ }, 'Parse bad gpx URL which returns a soft 404' : { 'topic' : function() { - gpx.gpxParseURL('http://' + HOST + ':' + PORT + '/soft404.gpx', this.callback); + gpx.gpxParseURL(HOST + '/soft404.gpx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -156,7 +156,7 @@ vows.describe('Test suite for parsing gpx').addBatch({ }, 'Parse gpx URL which returns a broken xml' : { 'topic' : function() { - gpx.gpxParseURL('http://' + HOST + ':' + PORT + '/broken.gpx', this.callback); + gpx.gpxParseURL(HOST + '/broken.gpx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -164,11 +164,11 @@ vows.describe('Test suite for parsing gpx').addBatch({ }, 'Parse image URL' : { 'topic' : function() { - gpx.gpxParseURL('http://' + HOST + ':' + PORT + '/image.gpx', this.callback); + gpx.gpxParseURL(HOST + '/image.gpx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); assert.equal(/Non-whitespace before first tag/.test(err.message), true); } } -}).export(module) \ No newline at end of file +}).export(module) diff --git a/tests/test_parse_tcx.js b/tests/test_parse_tcx.js index 0354a09..09b5fc9 100644 --- a/tests/test_parse_tcx.js +++ b/tests/test_parse_tcx.js @@ -2,50 +2,51 @@ var assert = require('assert'); var tcx = require('../lib/tcx-parser.js'); var vows = require('vows'); +var nock = require('nock'); /* For testing url based gpx data */ var http = require('http'); var fs = require('fs'); -var PORT = 8081; -var HOST = '127.0.0.1'; -http.createServer(function(req, res) { - if(req.url == '/data.tcx') { - fs.readFile('./tests/data/data.tcx', function(err, data) { - if(err) { - throw err; - } - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end(data); - }); - } else if(req.url == '/bad.tcx') { - res.writeHead(404, { - 'Content-Type' : 'text/html' - }); - res.end('Hey bad request!'); - } else if(req.url == '/soft404.tcx') { - res.writeHead(200, { - 'Content-Type' : 'text/xml' - }); - res.end('This is a soft 404 which has 200 as status code'); - } else if(req.url == '/broken.tcx') { - res.writeHead(200, { - 'Content-Type' : 'text/xml' - }); - res.end(''); - } else if (req.url == '/image.tcx') { - fs.readFile('./tests/data/loading.gif', function(err, data) { - if(err) { - throw err; - } - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }); - res.end(data); - }); +var HOST = 'http://fakedomain.tld'; +fs.readFile('./tests/data/data.tcx', function(err, data) { + if(err) { + throw err; } -}).listen(PORT, HOST); + nock(HOST) + .get('/data.tcx') + .reply(200, data, { + 'Content-Type' : 'application/xml' + }); +}); +nock(HOST) +.get('/bad.tcx') +.reply(404, 'Hey bad request!',{ + 'Content-Type' : 'text/html' +}); + +nock(HOST) +.get('/soft404.tcx') +.reply(200, 'This is a soft 404 which has 200 as status code',{ + 'Content-Type' : 'text/xml' +}); + +nock(HOST) +.get('/broken.tcx') +.reply(200, '',{ + 'Content-Type' : 'text/xml' +}); + + +fs.readFile('./tests/data/loading.gif', function(err, data) { + if(err) { + throw err; + } + nock(HOST) + .get('/image.tcx') + .reply(200, data,{ + 'Content-Type' : 'application/xml' + }); +}); vows.describe('Test suite for parsing tcx').addBatch({ 'Parse broken tcx data' : { @@ -137,7 +138,7 @@ vows.describe('Test suite for parsing tcx').addBatch({ }, 'Parse tcx URL' : { 'topic' : function() { - tcx.tcxParseURL('http://' + HOST + ':' + PORT + '/data.tcx', this.callback); + tcx.tcxParseURL(HOST + '/data.tcx', this.callback); }, 'Should return an array of two tracking points' : function(err, result) { assert.deepEqual(result, [{ @@ -166,7 +167,7 @@ vows.describe('Test suite for parsing tcx').addBatch({ }, 'Parse bad tcx URL' : { 'topic' : function() { - tcx.tcxParseURL('http://' + HOST + ':' + PORT + '/bad.tcx', this.callback); + tcx.tcxParseURL(HOST + '/bad.tcx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -174,7 +175,7 @@ vows.describe('Test suite for parsing tcx').addBatch({ }, 'Parse bad tcx URL which returns a soft 404' : { 'topic' : function() { - tcx.tcxParseURL('http://' + HOST + ':' + PORT + '/soft404.tcx', this.callback); + tcx.tcxParseURL(HOST + '/soft404.tcx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -182,7 +183,7 @@ vows.describe('Test suite for parsing tcx').addBatch({ }, 'Parse tcx URL which returns a broken xml' : { 'topic' : function() { - tcx.tcxParseURL('http://' + HOST + ':' + PORT + '/broken.tcx', this.callback); + tcx.tcxParseURL(HOST + '/broken.tcx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); @@ -190,11 +191,11 @@ vows.describe('Test suite for parsing tcx').addBatch({ }, 'Parse image URL' : { 'topic' : function() { - tcx.tcxParseURL('http://' + HOST + ':' + PORT + '/image.tcx', this.callback); + tcx.tcxParseURL(HOST + '/image.tcx', this.callback); }, 'Should return an error' : function(err, result) { assert.equal(err != null, true); assert.equal(/Non-whitespace before first tag/.test(err.message), true); } } -}).export(module) \ No newline at end of file +}).export(module)