Created
February 24, 2012 09:35
-
-
Save fwielstra/1899746 to your computer and use it in GitHub Desktop.
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 util = require("util"), | |
http = require("http"), | |
url = require("url"), | |
fileSystem = require('fs'), | |
path = require('path'); | |
var responseMap = [ | |
{pattern:/\btest\?test\=true$/, response:"test-ok.xml"}, | |
{pattern:/\btest\?test=true&henk=liev$/, response:"test-ok.xml"}, | |
// 'begins with' test | |
{pattern:/\btest\?test=true&klaas=stom/, response:"test-ok.xml"}, | |
// partial match / ignore datetime test | |
{pattern:/\btest\?yearCard=false&dateTime=(.*)&test=true/, response:"test-ok.xml"}, | |
// real life match | |
{pattern:/\bmobile-api-planner\?yearCard=false&dateTime=(.*)&previousAdvices=0&hslAllowed=false&toStation=bd&nextAdvices=1&fromStation=rtd&departure=true/, | |
response:"test-ok.xml"}, | |
{pattern:/\bmobile-api-planner\?(?=.*hslAllowed=true)(?=.*fromStation=ASD)(?=.*toStation=RTD)/, response:"ams-rtd-fyra.xml"}, | |
{pattern:/\bmobile-api-planner\?(?=.*fromCity=Amsterdam)(?=.*fromStreet=Nieuwezijds Achterburgwal)(?=.*toStation=RTD)/, response:"ams-nwz-rtd-fyra.xml"} | |
]; | |
function findResponseFor(path) { | |
var result = null; | |
responseMap.forEach(function(candidate) { | |
if (path.match(candidate.pattern)) { | |
result = candidate.response; | |
return; | |
} | |
}) | |
return result; | |
} | |
function writeFileToResponse(responseFile, response) { | |
var filePath = path.join(__dirname, 'response', responseFile); | |
var stat = fileSystem.statSync(filePath); | |
response.writeHead(200, {"Content-Type": "application/xml", 'Content-Length': stat.size}); | |
var readStream = fileSystem.createReadStream(filePath); | |
readStream.on('data', function(data) { | |
response.write(data); | |
}); | |
readStream.on('end', function() { | |
response.end(); | |
}); | |
} | |
function writeWebserviceToResponse(request, response) { | |
var params = url.parse(request.url); | |
var options = { | |
host: 'webservices.dinges.nl', | |
path: params.path, | |
headers: {'Authorization' : request.headers.authorization} | |
}; | |
var req = http.get(options, function(res) { | |
res.setEncoding('utf8'); | |
response.writeHead(res.statusCode, res.headers); | |
res.on('data', function (chunk) { | |
response.write(chunk); | |
}); | |
res.on('end', function() { | |
response.end(); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
} | |
http.createServer(function(request, response) { | |
var params = url.parse(request.url); | |
console.log("request params: " + util.inspect(params)); | |
var responseFile = findResponseFor(params.path); | |
if (responseFile != null) { | |
console.log("Response found: " + responseFile); | |
writeFileToResponse(responseFile, response); | |
} else { | |
console.log("Response not found, calling NS API"); | |
writeWebserviceToResponse(request, response); | |
} | |
}).listen(1337); | |
util.puts("Server running at http://localhost:1337/"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment