MarkTraceur has uploaded a new change for review.
https://gerrit.wikimedia.org/r/52090
Change subject: WIP Use mock API to test parser extensions
......................................................................
WIP Use mock API to test parser extensions
This commit currently only has the mock API. parserTests will currently not use
it in any real sense, so don't expect fireworks. The next step is to start
using the API in parserTests.
Bug: 45440
Change-Id: Ie63cb796d098947443616c8bb02ebdea901815c0
---
A js/tests/mockAPI.js
M js/tests/parserTests.js
2 files changed, 158 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid
refs/changes/90/52090/1
diff --git a/js/tests/mockAPI.js b/js/tests/mockAPI.js
new file mode 100644
index 0000000..0d6cf63
--- /dev/null
+++ b/js/tests/mockAPI.js
@@ -0,0 +1,155 @@
+// This file is used to run a stub API that mimicks the MediaWiki interface
+// for the purposes of testing extension expansion.
+var express = require('express'),
+ path = require('path'),
+ fs = require('fs');
+
+var mp = '../lib/',
+ Util = require( mp + 'mediawiki.Util.js' ).Util,
+ libtr = require(mp + 'mediawiki.ApiRequest.js'),
+ DoesNotExistError = libtr.DoesNotExistError;
+
+/* -------------------- web app access points below --------------------- */
+
+var app = express.createServer();
+
+app.use( express.bodyParser() );
+
+var formatters = {
+ json: function ( data ) {
+ return JSON.stringify( data );
+ },
+ jsonfm: function ( data ) {
+ return JSON.stringify( data, null, 2 );
+ }
+ },
+
+ availableActions = {
+ expandtemplates: function ( body, cb ) {
+ var text = body.text;
+
+ cb( null, { content: text } );
+ }
+ },
+
+ actionDefinitions = {
+ expandtemplates: {
+ parameters: {
+ text: 'text',
+ title: 'text'
+ }
+ }
+ };
+
+function buildOptions( options ) {
+ var i, optStr = '';
+
+ for ( i = 0; i < options.length; i++ ) {
+ optStr += '<option value="' + options[i] + '">' + options[i] +
'</option>';
+ }
+
+ return optStr;
+}
+
+function buildFieldsets( actions ) {
+ var i, j, k, action, actionDef, param, params, paramList, title,
+ setStr = '';
+
+ for ( i = 0; i < actions.length; i++ ) {
+ action = actions[i];
+ title = 'action=' + action;
+ setStr += '<fieldset id="' + title + '">';
+ setStr += '<legend>' + title + '</legend>';
+ actionDef = actionDefinitions[action];
+ params = actionDef.parameters;
+ paramList = Object.keys( params );
+
+ for ( j = 0; j < paramList.length; j++ ) {
+ param = paramList[j];
+ if ( typeof params[param] === 'string' ) {
+ setStr += '<input type="' + params[param] + '"
name="' + param + '" />';
+ } else if ( params[param].length ) {
+ setStr += '<select name="' + param + '">';
+ for ( k = 0; k < params[param].length; k++ ) {
+ setStr += '<option value="' +
params[param][k] + '">' + params[param][k] + '</option>';
+ }
+ }
+ }
+ setStr += '</fieldset>';
+ }
+ return setStr;
+}
+
+// GET request to root....should probably just tell the client how to use the
service
+app.get( '/', function ( req, res ) {
+ var formats = buildOptions( Object.keys( formatters ) ),
+ actions = buildOptions( Object.keys( availableActions ) );
+ res.setHeader( 'Content-Type', 'text/html; charset=UTF-8' );
+ res.write(
+ '<html><body>' +
+ '<form id="service-form" method="GET"
action="api.php">' +
+ '<h2>GET form</h2>' +
+ '<select name="format">' +
+ formats +
+ '</select>' +
+ '<select name="action">' +
+ actions +
+ '</select>' +
+ buildFieldsets( Object.keys( availableActions )
) +
+ '<input type="submit" />' +
+ '</form>' +
+ '<form id="service-form" method="POST"
action="api.php">' +
+ '<h2>POST form</h2>' +
+ '<select name="format">' +
+ formats +
+ '</select>' +
+ '<select name="action">' +
+ actions +
+ '</select>' +
+ buildFieldsets( Object.keys( availableActions )
) +
+ '<input type="submit" />' +
+ '</form>' +
+ '</body></html>' );
+ res.end();
+} );
+
+function handleApiRequest( body, res ) {
+ var format = body.format,
+ action = body.action,
+ formatter = formatters[format];
+
+ availableActions[action]( body, function ( err, data ) {
+ if ( err === null ) {
+ res.setHeader( 'Content-Type', 'application/json' );
+ res.write( formatter( data ) );
+ res.end();
+ } else {
+ res.setHeader( 'Content-Type', 'text/plain' );
+
+ if ( err.code ) {
+ res.status( err.code );
+ } else {
+ res.status( 500 );
+ }
+
+ res.write( err.stack || err.toString() );
+ res.end();
+ }
+ } );
+}
+
+// GET request to api.php....actually perform an API request
+app.get( '/api.php', function ( req, res ) {
+ handleApiRequest( req.query, res );
+} );
+
+// POST request to api.php....actually perform an API request
+app.post( '/api.php', function ( req, res ) {
+ handleApiRequest( req.body, res );
+} );
+
+module.exports = app;
+
+console.log( 'Mock MediaWiki API starting....' );
+app.listen( 7001 );
+console.log( 'Started.' );
diff --git a/js/tests/parserTests.js b/js/tests/parserTests.js
index 7fdaa83..c95961d 100644
--- a/js/tests/parserTests.js
+++ b/js/tests/parserTests.js
@@ -12,6 +12,9 @@
jsDiff = require('diff'),
colors = require('colors'),
Util = require( '../lib/mediawiki.Util.js' ).Util,
+ childProc = require('child_process'),
+ spawn = childProc.spawn,
+ fork = childProc.fork,
DOMUtils = require( '../lib/mediawiki.DOMUtils.js' ).DOMUtils,
util = require( 'util' ),
async = require( 'async' ),
--
To view, visit https://gerrit.wikimedia.org/r/52090
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie63cb796d098947443616c8bb02ebdea901815c0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Parsoid
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits