2017-02-20 18:32:27 +00:00
|
|
|
import jq from 'jsonpath';
|
2017-05-17 19:02:09 +00:00
|
|
|
import {DOMParser} from 'xmldom';
|
|
|
|
import xpath from 'xpath';
|
2017-02-20 18:32:27 +00:00
|
|
|
import * as models from '../../models';
|
|
|
|
|
2017-03-08 05:52:17 +00:00
|
|
|
import BaseExtension from './base/base-extension';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
export default class ResponseExtension extends BaseExtension {
|
2017-02-20 18:32:27 +00:00
|
|
|
constructor () {
|
|
|
|
super();
|
2017-05-17 19:02:09 +00:00
|
|
|
this.tags = ['response'];
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
async run (context, field, id, query) {
|
2017-05-17 19:02:09 +00:00
|
|
|
if (field !== 'body') {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Invalid response field ${field}`);
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const request = await models.request.getById(id);
|
|
|
|
if (!request) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Could not find request ${id}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const response = await models.response.getLatestForRequest(id);
|
|
|
|
|
|
|
|
if (!response) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`No responses for request ${id}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const bodyBuffer = new Buffer(response.body, response.encoding);
|
|
|
|
const bodyStr = bodyBuffer.toString();
|
|
|
|
|
2017-05-17 19:02:09 +00:00
|
|
|
if (query.indexOf('$') === 0) {
|
|
|
|
return this.matchJSONPath(bodyStr, query);
|
|
|
|
} else if (query.indexOf('/') === 0) {
|
|
|
|
return this.matchXPath(bodyStr, query);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Invalid format for response query: ${query}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matchJSONPath (bodyStr, query) {
|
2017-02-20 18:32:27 +00:00
|
|
|
let body;
|
2017-05-17 19:02:09 +00:00
|
|
|
let results;
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
try {
|
|
|
|
body = JSON.parse(bodyStr);
|
|
|
|
} catch (err) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Invalid JSON: ${err.message}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
results = jq.query(body, query);
|
|
|
|
} catch (err) {
|
2017-03-03 20:09:08 +00:00
|
|
|
throw new Error(`Invalid JSONPath query: ${query}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
2017-03-03 20:09:08 +00:00
|
|
|
throw new Error(`Returned no results: ${query}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
} else if (results.length > 1) {
|
2017-03-03 20:09:08 +00:00
|
|
|
throw new Error(`Returned more than one result: ${query}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 19:02:09 +00:00
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
matchXPath (bodyStr, query) {
|
|
|
|
let results;
|
|
|
|
|
|
|
|
// This will never throw
|
|
|
|
const dom = new DOMParser().parseFromString(bodyStr);
|
|
|
|
|
|
|
|
try {
|
|
|
|
results = xpath.select(query, dom);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Invalid XPath query: ${query}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
throw new Error(`Returned no results: ${query}`);
|
|
|
|
} else if (results.length > 1) {
|
|
|
|
throw new Error(`Returned more than one result: ${query}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results[0].childNodes.toString();
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
}
|