Skip to content

Commit

Permalink
T115395: Mock the batch api
Browse files Browse the repository at this point in the history
 * Enabled in the mocha http api tests, which make at least one
   preprocessor request at the moment.

Change-Id: I0383c38cffb8b9cf37b72df87f60e216938906fb
  • Loading branch information
arlolra authored and jenkins-bot committed May 11, 2017
1 parent ae3e449 commit a30f094
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tests/mocha/api.js
Expand Up @@ -19,6 +19,8 @@ var optionsPath = path.resolve(__dirname, './test.config.yaml');
var optionsYaml = fs.readFileSync(optionsPath, 'utf8');
var parsoidOptions = yaml.load(optionsYaml).services[0].conf;

parsoidOptions.useBatchAPI = true;

var defaultContentVersion = '1.4.0';

describe('Parsoid API', function() {
Expand Down
44 changes: 38 additions & 6 deletions tests/mockAPI.js
Expand Up @@ -241,6 +241,17 @@ var formatters = {
},
};

var preProcess = function(text) {
var match = text.match(/{{echo\|(.*?)}}/);
if (match) {
return { wikitext: match[1] };
} else if (text === '{{colours of the rainbow}}') {
return { wikitext: 'purple' };
} else {
return null;
}
};

var availableActions = {
parse: function(body, cb) {
var resultText;
Expand Down Expand Up @@ -391,16 +402,37 @@ var availableActions = {
},

expandtemplates: function(body, cb) {
var match = body.text.match(/{{echo\|(.*?)}}/);
if (match) {
cb(null, { expandtemplates: { wikitext: match[1] } });
} else if (body.text === '{{colours of the rainbow}}') {
cb(null, { expandtemplates: { wikitext: 'purple' } });
} else {
var res = preProcess(body.text);
if (res === null) {
cb(new Error('Sorry!'));
} else {
cb(null, { expandtemplates: res });
}
},

'parsoid-batch': function(body, cb) {
var batch;
try {
batch = JSON.parse(body.batch);
console.assert(Array.isArray(batch));
} catch (e) {
return cb(e);
}
var errs = [];
var results = batch.map(function(b) {
var res = null;
switch (b.action) {
case 'preprocess':
res = preProcess(b.text);
break;
}
if (res === null) { errs.push(b); }
return res;
});
var err = (errs.length > 0) ? new Error(JSON.stringify(errs)) : null;
cb(err, { 'parsoid-batch': results });
},

// Return a dummy response
templatedata: function(body, cb) {
cb(null, {
Expand Down

0 comments on commit a30f094

Please sign in to comment.