2016-05-25 21:35:38 -04:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
var util = require('util');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
this.ShouldGetAResponse = () => {
|
|
|
|
assert.equal(this.response.statusCode, 200);
|
|
|
|
assert.ok(this.response.body);
|
|
|
|
assert.ok(this.response.body.length);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.ShouldBeValidJSON = (callback) => {
|
|
|
|
try {
|
|
|
|
this.json = JSON.parse(this.response.body);
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.ShouldBeWellFormed = () => {
|
|
|
|
assert.equal(typeof this.json.status, 'number');
|
|
|
|
};
|
|
|
|
|
|
|
|
this.WhenIRouteIShouldGet = (table, callback) => {
|
|
|
|
this.reprocessAndLoadData(() => {
|
|
|
|
var headers = new Set(table.raw()[0]);
|
|
|
|
|
|
|
|
var requestRow = (row, ri, cb) => {
|
2016-03-28 17:49:29 -04:00
|
|
|
var got;
|
2016-03-04 15:11:05 -05:00
|
|
|
|
|
|
|
var afterRequest = (err, res, body) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (body && body.length) {
|
2016-05-13 13:18:00 -04:00
|
|
|
let destinations, pronunciations, instructions, bearings, turns, modes, times, distances, summary, intersections, lanes;
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-05-25 21:35:38 -04:00
|
|
|
let json = JSON.parse(body);
|
2016-03-04 15:11:05 -05:00
|
|
|
|
2016-05-25 21:35:38 -04:00
|
|
|
let hasRoute = json.code === 'Ok';
|
2016-03-04 15:11:05 -05:00
|
|
|
|
|
|
|
if (hasRoute) {
|
2016-03-27 23:55:12 -04:00
|
|
|
instructions = this.wayList(json.routes[0]);
|
2016-05-25 21:35:38 -04:00
|
|
|
pronunciations = this.pronunciationList(json.routes[0]);
|
2016-05-26 18:47:46 -04:00
|
|
|
destinations = this.destinationsList(json.routes[0]);
|
2016-03-27 23:55:12 -04:00
|
|
|
bearings = this.bearingList(json.routes[0]);
|
|
|
|
turns = this.turnList(json.routes[0]);
|
2016-05-10 02:37:45 -04:00
|
|
|
intersections = this.intersectionList(json.routes[0]);
|
2016-03-27 23:55:12 -04:00
|
|
|
modes = this.modeList(json.routes[0]);
|
|
|
|
times = this.timeList(json.routes[0]);
|
|
|
|
distances = this.distanceList(json.routes[0]);
|
2016-05-13 13:18:00 -04:00
|
|
|
lanes = this.lanesList(json.routes[0]);
|
2016-04-28 19:48:44 -04:00
|
|
|
summary = this.summary(json.routes[0]);
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('status')) {
|
2016-03-29 21:30:37 -04:00
|
|
|
got.status = res.statusCode.toString();
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('message')) {
|
2016-03-29 21:30:37 -04:00
|
|
|
got.message = json.message || '';
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('#')) {
|
|
|
|
// comment column
|
|
|
|
got['#'] = row['#'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('geometry')) {
|
2016-03-30 21:47:11 -04:00
|
|
|
got.geometry = json.routes[0].geometry;
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('route')) {
|
|
|
|
got.route = (instructions || '').trim();
|
|
|
|
|
2016-04-28 19:48:44 -04:00
|
|
|
if (headers.has('summary')) {
|
|
|
|
got.summary = (summary || '').trim();
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
if (headers.has('alternative')) {
|
2016-03-29 15:17:46 -04:00
|
|
|
// TODO examine more than first alternative?
|
|
|
|
got.alternative ='';
|
|
|
|
if (json.routes && json.routes.length > 1)
|
|
|
|
got.alternative = this.wayList(json.routes[1]);
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 23:55:12 -04:00
|
|
|
var distance = hasRoute && json.routes[0].distance,
|
|
|
|
time = hasRoute && json.routes[0].duration;
|
2016-03-04 15:11:05 -05:00
|
|
|
|
|
|
|
if (headers.has('distance')) {
|
|
|
|
if (row.distance.length) {
|
|
|
|
if (!row.distance.match(/\d+m/))
|
|
|
|
throw new Error('*** Distance must be specified in meters. (ex: 250m)');
|
|
|
|
got.distance = instructions ? util.format('%dm', distance) : '';
|
|
|
|
} else {
|
|
|
|
got.distance = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (headers.has('time')) {
|
|
|
|
if (!row.time.match(/\d+s/))
|
|
|
|
throw new Error('*** Time must be specied in seconds. (ex: 60s)');
|
|
|
|
got.time = instructions ? util.format('%ds', time) : '';
|
|
|
|
}
|
|
|
|
|
2016-05-13 13:18:00 -04:00
|
|
|
|
|
|
|
if (headers.has('lanes')) {
|
|
|
|
got.lanes = (lanes || '').trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
if (headers.has('speed')) {
|
|
|
|
if (row.speed !== '' && instructions) {
|
|
|
|
if (!row.speed.match(/\d+ km\/h/))
|
|
|
|
throw new Error('*** Speed must be specied in km/h. (ex: 50 km/h)');
|
|
|
|
var speed = time > 0 ? Math.round(3.6*distance/time) : null;
|
|
|
|
got.speed = util.format('%d km/h', speed);
|
|
|
|
} else {
|
|
|
|
got.speed = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 02:37:45 -04:00
|
|
|
if (headers.has('intersections')) {
|
|
|
|
got.intersections = (intersections || '').trim();
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:11:05 -05:00
|
|
|
var putValue = (key, value) => {
|
|
|
|
if (headers.has(key)) got[key] = instructions ? value : '';
|
|
|
|
};
|
|
|
|
|
|
|
|
putValue('bearing', bearings);
|
|
|
|
putValue('turns', turns);
|
|
|
|
putValue('modes', modes);
|
|
|
|
putValue('times', times);
|
|
|
|
putValue('distances', distances);
|
2016-05-25 21:35:38 -04:00
|
|
|
putValue('pronunciations', pronunciations);
|
2016-05-26 18:47:46 -04:00
|
|
|
putValue('destinations', destinations);
|
2016-03-04 15:11:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok = true;
|
|
|
|
|
|
|
|
for (var key in row) {
|
|
|
|
if (this.FuzzyMatch.match(got[key], row[key])) {
|
|
|
|
got[key] = row[key];
|
|
|
|
} else {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
this.logFail(row, got, { route: { query: this.query, response: res }});
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, got);
|
|
|
|
} else {
|
|
|
|
cb(new Error('request failed to return valid body'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (headers.has('request')) {
|
|
|
|
got = { request: row.request };
|
|
|
|
this.requestUrl(row.request, afterRequest);
|
|
|
|
} else {
|
|
|
|
var defaultParams = this.queryParams;
|
|
|
|
var userParams = [];
|
|
|
|
got = {};
|
|
|
|
for (var k in row) {
|
|
|
|
var match = k.match(/param:(.*)/);
|
|
|
|
if (match) {
|
|
|
|
if (row[k] === '(nil)') {
|
|
|
|
userParams.push([match[1], null]);
|
|
|
|
} else if (row[k]) {
|
|
|
|
userParams.push([match[1], row[k]]);
|
|
|
|
}
|
|
|
|
got[k] = row[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var params = this.overwriteParams(defaultParams, userParams),
|
|
|
|
waypoints = [],
|
|
|
|
bearings = [];
|
|
|
|
|
|
|
|
if (row.bearings) {
|
|
|
|
got.bearings = row.bearings;
|
|
|
|
bearings = row.bearings.split(' ').filter(b => !!b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row.from && row.to) {
|
|
|
|
var fromNode = this.findNodeByName(row.from);
|
|
|
|
if (!fromNode) throw new Error(util.format('*** unknown from-node "%s"'), row.from);
|
|
|
|
waypoints.push(fromNode);
|
|
|
|
|
|
|
|
var toNode = this.findNodeByName(row.to);
|
|
|
|
if (!toNode) throw new Error(util.format('*** unknown to-node "%s"'), row.to);
|
|
|
|
waypoints.push(toNode);
|
|
|
|
|
|
|
|
got.from = row.from;
|
|
|
|
got.to = row.to;
|
|
|
|
this.requestRoute(waypoints, bearings, params, afterRequest);
|
|
|
|
} else if (row.waypoints) {
|
|
|
|
row.waypoints.split(',').forEach((n) => {
|
|
|
|
var node = this.findNodeByName(n.trim());
|
|
|
|
if (!node) throw new Error('*** unknown waypoint node "%s"', n.trim());
|
|
|
|
waypoints.push(node);
|
|
|
|
});
|
|
|
|
got.waypoints = row.waypoints;
|
|
|
|
this.requestRoute(waypoints, bearings, params, afterRequest);
|
|
|
|
} else {
|
|
|
|
throw new Error('*** no waypoints');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.processRowsAndDiff(table, requestRow, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|