/** Cases for testing the Parsoid API through HTTP */
/* global describe, it, before */
'use strict';
const { action, assert, REST, utils } = require( 'api-testing' );
var domino = require('domino');
var should = require('chai').should();
var semver = require('semver');
var url = require('url');
const { DOMUtils } = require( "../../lib/utils/DOMUtils" );
var Util = require('../../lib/utils/Util.js').Util;
var JSUtils = require('../../lib/utils/jsutils').JSUtils;
const parsoidOptions = {
limits: {
wt2html: { maxWikitextSize: 20000 },
html2wt: { maxHTMLSize: 10000 },
},
};
// FIXME(T283875): These should all be re-enabled
var skipForNow = true;
var defaultContentVersion = '2.8.0';
// section wrappers are a distraction from the main business of
// this file which is to verify functionality of API end points
// independent of what they are returning and computing.
//
// Verifying the correctness of content is actually the job of
// parser tests and other tests.
//
// So, hide most of that that distraction in a helper.
//
// Right now, all uses of this helper have empty lead sections.
// But, maybe in the future, this may change. So, retain the option.
function validateDoc(doc, nodeName, emptyLead) {
var leadSection = doc.body.firstChild;
leadSection.nodeName.should.equal('SECTION');
if (emptyLead) {
// Could have whitespace and comments
leadSection.childElementCount.should.equal(0);
}
var nonEmptySection = emptyLead ? leadSection.nextSibling : leadSection;
nonEmptySection.firstChild.nodeName.should.equal(nodeName);
}
function status200( res ) {
assert.strictEqual( res.status, 200, res.text );
}
// Return a matcher function that checks whether a content type matches the given parameters.
function contentTypeMatcher( expectedMime, expectedSpec, expectedVersion ) {
const pattern = /^([-\w]+\/[-\w]+);(?: charset=utf-8;)? profile="https:\/\/www.mediawiki.org\/wiki\/Specs\/([-\w]+)\/(\d+\.\d+\.\d+)"$/;
return ( actual ) => {
const parts = pattern.exec( actual );
if ( !parts ) {
return false;
}
const [ , mime, spec, version ] = parts;
// match version using caret semantics
if ( !semver.satisfies( version, `^${ expectedVersion || defaultContentVersion }` ) ) {
return false;
}
if ( mime !== expectedMime || spec !== expectedSpec ) {
return false;
}
return true;
};
}
describe('Parsoid API', function() {
const client = new REST();
const parsedUrl = new url.URL(client.req.app);
const hostname = parsedUrl.hostname;
const mockDomain = client.pathPrefix = `rest.php/${ hostname }`;
const page = utils.title( 'Lint Page ' );
const pageEncoded = encodeURIComponent( page );
let revid, oldrevid;
before(async function() {
this.timeout(30000);
const alice = await action.alice();
// Create pages
const oldEdit = await alice.edit(page, { text: 'First Revision Content' });
oldEdit.result.should.equal('Success');
oldrevid = oldEdit.newrevid;
let edit = await alice.edit(page, { text: '{|\n|hi\n|ho\n|}' });
edit.result.should.equal('Success');
revid = edit.newrevid;
edit = await alice.edit('JSON Page', {
text: '[1]', contentmodel: 'json'
});
edit.result.should.equal('Success');
});
const acceptableHtmlResponse = function(contentVersion, expectFunc) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.headers.should.have.property('content-type');
res.headers['content-type'].should.equal(
'text/html; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"'
);
res.text.should.not.equal('');
if (expectFunc) {
return expectFunc(res.text);
}
};
};
const acceptablePageBundleResponse = function(contentVersion, expectFunc) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.headers.should.have.property('content-type');
res.headers['content-type'].should.satisfy( contentTypeMatcher( 'application/json', 'pagebundle', contentVersion ) );
res.body.should.have.property('html');
res.body.html.should.have.property('headers');
res.body.html.headers.should.have.property('content-type');
res.body.html.headers['content-type'].should.satisfy( contentTypeMatcher( 'text/html', 'HTML', contentVersion ) );
res.body.html.should.have.property('body');
res.body.should.have.property('data-parsoid');
res.body['data-parsoid'].should.have.property('headers');
res.body['data-parsoid'].headers.should.have.property('content-type');
res.body['data-parsoid'].headers['content-type'].should.satisfy( contentTypeMatcher( 'application/json', 'data-parsoid', contentVersion ) );
res.body['data-parsoid'].should.have.property('body');
if (semver.gte(contentVersion, '999.0.0')) {
res.body.should.have.property('data-mw');
res.body['data-mw'].should.have.property('headers');
res.body['data-mw'].headers.should.have.property('content-type');
res.body['data-mw'].headers['content-type'].should.satisfy( contentTypeMatcher( 'application/json', 'data-mw', contentVersion ) );
res.body['data-mw'].should.have.property('body');
}
if (expectFunc) {
return expectFunc(res.body.html.body);
}
};
};
describe('domain check', function() {
it('should apply to /v3/transform endpoint', function(done) {
client.req
.get('rest.php/the.wrong.domain/v3/page/wikitext/' + pageEncoded )
.expect(function(res) {
res.status.should.equal(400, res.text);
res.body.error.should.equal('parameter-validation-failed');
res.body.failureCode.should.equal('invalid-domain');
})
.end(done);
});
it('should apply to /v3/transform endpoint', function(done) {
client.req
.post('rest.php/the.wrong.domain/v3/transform/wikitext/to/html/')
.send({ wikitext: '== h2 ==' })
.expect(function(res) {
res.status.should.equal(400, res.text);
res.body.error.should.equal('parameter-validation-failed');
res.body.failureCode.should.equal('invalid-domain');
})
.end(done);
});
});
describe('formats', function() {
it('should accept application/x-www-form-urlencoded', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.type('form')
.send({
wikitext: '== h2 ==',
})
.expect(status200)
.expect(function(res) {
validateDoc(domino.createDocument(res.text), 'H2', true);
})
.end(done);
});
it('should accept application/json', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.type('json')
.send({
wikitext: '== h2 ==',
})
.expect(status200)
.expect(function(res) {
validateDoc(domino.createDocument(res.text), 'H2', true);
})
.end(done);
});
it('should accept multipart/form-data', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.field('wikitext', '== h2 ==')
.expect(status200)
.expect(function(res) {
validateDoc(domino.createDocument(res.text), 'H2', true);
})
.end(done);
});
// Skipped because all errors are returned as JSON in Parsoid/PHP
it.skip('should return a plaintext error', function(done) {
client.req
.get(mockDomain + '/v3/page/wikitext/Doesnotexist')
.expect(404)
.expect(function(res) {
res.headers['content-type'].should.equal(
'text/plain; charset=utf-8'
);
res.text.should.equal('Did not find page revisions for Doesnotexist');
})
.end(done);
});
it('should return a json error', function(done) {
client.req
.get(mockDomain + '/v3/page/pagebundle/Doesnotexist')
.expect(404)
.expect(function(res) {
res.headers['content-type'].should.equal(
'application/json'
);
res.body.errorKey.should.equal('rest-specified-revision-unavailable');
})
.end(done);
});
// Skipped because all errors are returned as JSON in Parsoid/PHP
it.skip('should return an html error', function(done) {
client.req
.get('/v3/page/html/XSS')
.expect(404)
.expect(function(res) {
res.headers['content-type'].should.equal(
'text/html; charset=utf-8'
);
res.text.should.equal('Invalid domain: <img src=x onerror="javascript:alert('hi')">');
})
.end(done);
});
}); // formats
describe('accepts', function() {
it('should not accept requests for older content versions (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"')
.send({ wikitext: '== h2 ==' })
.expect(406)
.expect(function(res) {
// FIXME: See skipped html error test above
JSON.parse(res.error.text).errorKey.should.equal(
'rest-unsupported-target-format'
);
})
.end(done);
});
it('should not accept requests for older content versions (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"')
.send({ wikitext: '== h2 ==' })
.expect(406)
.expect(function(res) {
JSON.parse(res.error.text).errorKey.should.equal(
'rest-unsupported-target-format'
);
})
.end(done);
});
it('should not accept requests for other profiles (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', 'text/html; profile="something different"')
.send({ wikitext: '== h2 ==' })
.expect(406)
.end(done);
});
it('should not accept requests for other profiles (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="something different"')
.send({ wikitext: '== h2 ==' })
.expect(406)
.end(done);
});
it('should accept wildcards (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', '*/*')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptableHtmlResponse(defaultContentVersion))
.end(done);
});
it('should accept wildcards (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', '*/*')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptablePageBundleResponse(defaultContentVersion))
.end(done);
});
// T347426: Support for non-default major HTML versions has been disabled
it.skip('should prefer higher quality (html)', function(done) {
var contentVersion = '999.0.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept',
'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"; q=0.5,' +
'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"; q=0.8')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptableHtmlResponse(contentVersion))
.end(done);
});
// T347426: Support for non-default major HTML versions has been disabled
it.skip('should prefer higher quality (pagebundle)', function(done) {
var contentVersion = '999.0.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept',
'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/2.8.0"; q=0.5,' +
'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/999.0.0"; q=0.8')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptablePageBundleResponse(contentVersion))
.end(done);
});
it('should accept requests for the latest content version (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptableHtmlResponse(defaultContentVersion))
.end(done);
});
it('should accept requests for the latest content version (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({ wikitext: '== h2 ==' })
.expect(status200)
.expect(acceptablePageBundleResponse(defaultContentVersion))
.end(done);
});
it('should accept requests for content version 2.x (html)', function(done) {
var contentVersion = '2.8.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"')
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptableHtmlResponse(contentVersion))
.end(done);
});
it('should accept requests for content version 2.x (pagebundle)', function(done) {
var contentVersion = '2.8.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"')
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptablePageBundleResponse(contentVersion, function(html) {
// In < 999.x, data-mw is still inline.
html.should.match(/\s+data-mw\s*=\s*['"]/);
}))
.end(done);
});
// Note that these tests aren't that useful directly after a major version bump
it('should accept requests for older content version 2.x (html)', function(done) {
var contentVersion = '2.8.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.0.0"') // Keep this on the older version
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptableHtmlResponse(contentVersion))
.end(done);
});
it('should accept requests for older content version 2.x (pagebundle)', function(done) {
var contentVersion = '2.8.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/2.0.0"') // Keep this on the older version
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptablePageBundleResponse(contentVersion, function(html) {
// In < 999.x, data-mw is still inline.
html.should.match(/\s+data-mw\s*=\s*['"]/);
}))
.end(done);
});
it('should sanity check 2.x content (pagebundle)', function(done) {
if (skipForNow) {
return this.skip();
} // Missing files in wiki
var contentVersion = '2.8.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"')
.send({ wikitext: '[[File:Audio.oga]]' })
.expect(status200)
.expect(acceptablePageBundleResponse(contentVersion, function(html) {
var doc = domino.createDocument(html);
doc.querySelectorAll('audio').length.should.equal(1);
doc.querySelectorAll('video').length.should.equal(0);
}))
.end(done);
});
// T347426: Support for non-default major HTML versions has been disabled
it.skip('should accept requests for content version 999.x (html)', function(done) {
var contentVersion = '999.0.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.set('Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"')
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptableHtmlResponse(contentVersion))
.end(done);
});
// T347426: Support for non-default major HTML versions has been disabled
it.skip('should accept requests for content version 999.x (pagebundle)', function(done) {
var contentVersion = '999.0.0';
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"')
.send({ wikitext: '{{1x|hi}}' })
.expect(status200)
.expect(acceptablePageBundleResponse(contentVersion, function(html) {
// In 999.x, data-mw is in the pagebundle.
html.should.not.match(/\s+data-mw\s*=\s*['"]/);
}))
.end(done);
});
}); // accepts
const validWikitextResponse = function(expected) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.headers.should.have.property('content-type');
res.headers['content-type'].should.satisfy( contentTypeMatcher( 'text/plain', 'wikitext', '1.0.0' ) );
if (expected !== undefined) {
res.text.should.equal(expected);
} else {
res.text.should.not.equal('');
}
};
};
const validHtmlResponse = function(expectFunc) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.headers.should.have.property('content-type');
res.headers['content-type'].should.satisfy( contentTypeMatcher( 'text/html', 'HTML' ) );
var doc = domino.createDocument(res.text);
if (expectFunc) {
return expectFunc(doc);
} else {
res.text.should.not.equal('');
}
};
};
const validJsonResponse = function(expected) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.headers.should.have.property('content-type');
res.headers['content-type'].should.equal( 'application/json' );
if (expected !== undefined) {
res.text.should.equal(expected);
} else {
res.text.should.not.equal('');
}
};
};
const validPageBundleResponse = function(expectFunc) {
return function(res) {
res.statusCode.should.equal(200, res.text);
res.body.should.have.property('html');
res.body.html.should.have.property('headers');
res.body.html.headers.should.have.property('content-type');
res.body.html.headers['content-type'].should.satisfy( contentTypeMatcher( 'text/html', 'HTML' ) );
res.body.html.should.have.property('body');
res.body.should.have.property('data-parsoid');
res.body['data-parsoid'].should.have.property('headers');
res.body['data-parsoid'].headers.should.have.property('content-type');
res.body['data-parsoid'].headers['content-type'].should.satisfy( contentTypeMatcher( 'application/json', 'data-parsoid' ) );
res.body['data-parsoid'].should.have.property('body');
// TODO: Check data-mw when 999.x is the default.
console.assert(!semver.gte(defaultContentVersion, '999.0.0'));
var doc = domino.createDocument(res.body.html.body);
if (expectFunc) {
return expectFunc(doc, res.body['data-parsoid'].body);
}
};
};
describe('wt2lint', function() {
it('should lint the given page', function(done) {
if (skipForNow) {
return this.skip();
} // Enable linting config
client.req
.get(mockDomain + '/v3/page/lint/' + page + '/' + revid )
.expect(status200)
.expect(function(res) {
res.body.should.be.instanceof(Array);
res.body.length.should.equal(1);
res.body[0].type.should.equal('fostered');
})
.end(done);
});
it('should lint the given wikitext', function(done) {
if (skipForNow) {
return this.skip();
} // Enable linting config
client.req
.post(mockDomain + '/v3/transform/wikitext/to/lint/')
.send({
wikitext: {
headers: {
'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"',
},
body: "{|\nhi\n|ho\n|}",
},
})
.expect(status200)
.expect(function(res) {
res.body.should.be.instanceof(Array);
res.body.length.should.equal(1);
res.body[0].type.should.equal('fostered');
})
.end(done);
});
it('should lint the given page, transform', function(done) {
if (skipForNow) {
return this.skip();
} // Enable linting config
client.req
.post(mockDomain + '/v3/transform/wikitext/to/lint/' + page + '/' + revid)
.send({})
.expect(status200)
.expect(function(res) {
res.body.should.be.instanceof(Array);
res.body.length.should.equal(1);
res.body[0].type.should.equal('fostered');
})
.end(done);
});
it('should redirect title to latest revision (lint)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/lint/')
.send({
// no revid or wikitext source provided
original: {
title: page,
},
})
.expect( validJsonResponse() )
.end(done);
});
});
describe("wt2html", function() {
it('should get from a title and old revision (html)', function(done) {
client.req
.get(mockDomain + `/v3/page/html/${ page }/${ oldrevid }`)
.redirects( 0 )
.expect(validHtmlResponse(function(doc) {
doc.body.firstChild.firstChild.textContent.should.contain('First Revision Content');
}))
.end(done);
});
it('should get from a title and current revision (html)', function(done) {
client.req
.get(mockDomain + `/v3/page/html/${ page }`)
.redirects( 1 )
.expect(validHtmlResponse(function(doc) {
doc.body.firstChild.firstChild.textContent.should.contain('hi');
}))
.end(done);
});
// Disabled for T349098
it.skip('should get from a title and current revision (html, json content)', function(done) {
client.req
.get(mockDomain + '/v3/page/html/JSON_Page')
.redirects(1)
.expect(validHtmlResponse(function(doc) {
doc.body.firstChild.nodeName.should.equal('TABLE');
}))
.end(done);
});
it('should get from a title and revision (pagebundle)', function(done) {
client.req
.get(mockDomain + `/v3/page/pagebundle/${ page }/${ revid }`)
.redirects( 0 )
.expect(validPageBundleResponse())
.end(done);
});
// Disabled for T349098
it.skip('should get from a title and revision (pagebundle, json content)', function(done) {
client.req
.get(mockDomain + '/v3/page/pagebundle/JSON_Page')
.redirects(1)
.expect(validPageBundleResponse(function(doc) {
doc.body.firstChild.nodeName.should.equal('TABLE');
}))
.end(done);
});
it('should get from a title and revision (wikitext)', function(done) {
client.req
.get(mockDomain + `/v3/page/wikitext/${ page }/${ oldrevid }`)
.redirects( 0 )
.expect(validWikitextResponse('First Revision Content'))
.end(done);
});
it.skip('should set a custom etag for get requests (html)', function(done) {
const etagPrefixExp = new RegExp( '^"' + revid + '/' );
client.req
.get(mockDomain + `/v3/page/html/${ page }/${ revid }`)
.expect(validHtmlResponse())
.expect((res) => {
res.headers.should.have.property('etag');
res.headers.etag.should.match( etagPrefixExp );
})
.end(done);
});
it.skip('should set a custom etag for get requests (pagebundle)', function(done) {
const etagPrefixExp = new RegExp( '^"' + revid + '/' );
client.req
.get(mockDomain + `/v3/page/pagebundle/${ page }/${ revid }`)
.expect(validPageBundleResponse())
.expect((res) => {
res.headers.should.have.property('etag');
res.headers.etag.should.match( etagPrefixExp );
})
.end(done);
});
it('should accept wikitext as a string for html', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: "== h2 ==",
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should accept json contentmodel as a string for html', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: '{"1":2}',
contentmodel: 'json',
})
.expect(validHtmlResponse(function(doc) {
doc.body.firstChild.nodeName.should.equal('TABLE');
}))
.end(done);
});
it('should accept wikitext as a string for pagebundle', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({
wikitext: "== h2 ==",
})
.expect(validPageBundleResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should accept json contentmodel as a string for pagebundle', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({
wikitext: '{"1":2}',
contentmodel: 'json',
})
.expect(validPageBundleResponse(function(doc) {
doc.body.firstChild.nodeName.should.equal('TABLE');
should.not.exist(doc.querySelector('*[typeof="mw:Error"]'));
}))
.end(done);
});
it('should accept wikitext with headers', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: {
headers: {
'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"',
},
body: "== h2 ==",
},
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should require a title when no wikitext is provided (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({})
.expect(400)
.end(done);
});
it('should require a title when no wikitext is provided (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({})
.expect(400)
.end(done);
});
it('should error when revision not found (page, html)', function(done) {
client.req
.get(mockDomain + '/v3/page/html/Doesnotexist')
.expect(404)
.end(done);
});
it('should error when revision not found (page, pagebundle)', function(done) {
client.req
.get(mockDomain + '/v3/page/pagebundle/Doesnotexist')
.expect(404)
.end(done);
});
it('should error when revision not found (transform, wt2html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/Doesnotexist')
.send({})
.expect(404)
.end(done);
});
it('should error when revision not found (transform, wt2pb)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/Doesnotexist')
.send({})
.expect(404)
.end(done);
});
it('should accept an original title (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
// no revid or wikitext source provided
original: {
title: page,
},
})
.expect(validHtmlResponse())
.end(done);
});
it('should accept an original title (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({
// no revid or wikitext source provided
original: {
title: page,
},
})
.expect(validPageBundleResponse())
.end(done);
});
it('should accept an original title, other than main', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
// no revid or wikitext source provided
original: {
title: page,
},
})
.expect(validHtmlResponse())
.end(done);
});
it('should not require a title when empty wikitext is provided (html)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: '',
})
.expect(validHtmlResponse(function(doc) {
doc.body.children.length.should.equal(1); // empty lead section
doc.body.firstChild.nodeName.should.equal('SECTION');
doc.body.firstChild.children.length.should.equal(0);
}))
.end(done);
});
it('should not require a title when empty wikitext is provided (pagebundle)', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/pagebundle/')
.send({
wikitext: '',
})
.expect(validPageBundleResponse())
.end(done);
});
it('should not require a title when wikitext is provided', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: "== h2 ==",
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should not require a rev id when wikitext and a title is provided', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/Main_Page')
.send({
wikitext: "== h2 ==",
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should accept the wikitext source as original data', function(done) {
client.req
.post(mockDomain + `/v3/transform/wikitext/to/html/${ page }/${ revid }`)
.send({
original: {
wikitext: {
headers: {
'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"',
},
body: "== h2 ==",
},
},
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it('should use the proper source text', function(done) {
if (skipForNow) {
return this.skip();
} // Missing template 1x
client.req
.post(mockDomain + `/v3/transform/wikitext/to/html/${ page }/${ revid }`)
.send({
original: {
wikitext: {
headers: {
'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"',
},
body: "{{1x|foo|bar=bat}}",
},
},
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'P', false);
var span = doc.querySelector('span[typeof="mw:Transclusion"]');
var dmw = JSON.parse(span.getAttribute('data-mw'));
var template = dmw.parts[0].template;
template.target.wt.should.equal('1x');
template.params[1].wt.should.equal('foo');
template.params.bar.wt.should.equal('bat');
}))
.end(done);
});
it('should accept the wikitext source as original without a title or revision', function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
original: {
wikitext: {
headers: {
'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"',
},
body: "== h2 ==",
},
},
})
.expect(validHtmlResponse(function(doc) {
validateDoc(doc, 'H2', true);
}))
.end(done);
});
it("should respect body parameter in wikitext->html (body_only)", function(done) {
client.req
.post(mockDomain + '/v3/transform/wikitext/to/html/')
.send({
wikitext: "''foo''",
body_only: 1,
})
.expect(validHtmlResponse())
.expect(function(res) {
// v3 only returns children of
hi ho' }) .expect(404) .end(done); }); it('should error when if-match doesn\'t match', function(done) { client.req .post(mockDomain + '/v3/transform/html/to/wikitext/') .set( 'if-match', '"1219844647/deadbeef"' ) .send({ html: '
hi ho' }) .expect(412) .end(done); }); it('should not error when oldid not supplied (transform, html2wt)', function(done) { client.req .post(mockDomain + '/v3/transform/html/to/wikitext/Doesnotexist') .send({ html: '
hi ho' }) .expect(validWikitextResponse(' hi ho\n')) .end(done); }); it('should accept html as a string', function(done) { client.req .post(mockDomain + '/v3/transform/html/to/wikitext/') .send({ html: '\n
MediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\n| a | 4 |
|---|---|
| b | 3 |
MediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User's Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User's Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User\'s Guide for information on using the wiki software.
\n\nMediaWiki has been successfully installed.
\n\nConsult the User's Guide for information on using the wiki software.
\n\nhi
', original: { html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'ho
', }, 'data-parsoid': { headers: { 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"', }, body: {}, }, }, }) .expect(400) .end(done); }); // FIXME: Pagebundle validation in general is needed it.skip('should return http 400 if supplied data-parsoid is a string', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', original: { html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'ho
', }, 'data-parsoid': { headers: { 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"', }, body: 'Garbled text from RESTBase.', }, }, }) .expect(400) .end(done); }); // The following three tests should all serialize as: // "hi
', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"', }, body: 'ho
', }, }, }) .expect(400) .end(done); }); it('should return a 400 for not supplying data-mw', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: 'ho
', }, }, }) .expect(400) .end(done); }); it('should apply original data-mw', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, 'data-mw': { body: { ids: { "mwAQ": { "parts": [{ "template": { "target": { "wt": "1x", "href": "./Template:1x" }, "params": { "1": { "wt": "hi" } }, "i": 0 } }] } }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: 'ho
', }, }, }) .expect(validWikitextResponse('{{1x|hi}}')) .end(done); }); // Sanity check data-mw was applied in the previous test it('should return a 400 for missing modified data-mw', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, 'data-mw': { body: { ids: { "mwAQ": { } }, // Missing data-mw.parts! }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: 'ho
', }, }, }) .expect(400) .end(done); }); it('should give precedence to inline data-mw over original', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, 'data-mw': { body: { ids: { "mwAQ": { } }, // Missing data-mw.parts! }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: 'ho
', }, }, }) .expect(validWikitextResponse('{{1x|hi}}')) .end(done); }); it('should not apply original data-mw if modified is supplied', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: 'hi
', 'data-mw': { body: { ids: { "mwAQ": { "parts": [{ "template": { "target": { "wt": "1x", "href": "./Template:1x" }, "params": { "1": { "wt": "hi" } }, "i": 0 } }] } }, }, }, original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, 'data-mw': { body: { ids: { "mwAQ": { } }, // Missing data-mw.parts! }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: 'ho
', }, }, }) .expect(validWikitextResponse('{{1x|hi}}')) .end(done); }); // The next three tests, although redundant with the above precedence // tests, are an attempt to show clients the semantics of separate // data-mw in the API. The main idea is, // // non-inline-data-mw = modified || original // inline-data-mw > non-inline-data-mw it('should apply original data-mw when modified is absent (captions 1)', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: '', original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAg": { "optList": [{ "ck": "caption", "ak": "Testing 123" }] }, "mwAw": { "a": { "href": "./File:Foobar.jpg" }, "sa": {} }, "mwBA": { "a": { "resource": "./File:Foobar.jpg", "height": "28", "width": "240" },"sa": { "resource": "File:Foobar.jpg" } }, }, }, }, 'data-mw': { body: { ids: { "mwAg": { "caption": "Testing 123" }, }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: '', }, }, }) .expect(validWikitextResponse('[[File:Foobar.jpg|Testing 123]]')) .end(done); }); it('should give precedence to inline data-mw over modified (captions 2)', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: '', 'data-mw': { body: { ids: { "mwAg": { "caption": "Testing 123" }, }, }, }, original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAg": { "optList": [{ "ck": "caption", "ak": "Testing 123" }] }, "mwAw": { "a": { "href": "./File:Foobar.jpg" }, "sa": {} }, "mwBA": { "a": { "resource": "./File:Foobar.jpg", "height": "28", "width": "240" },"sa": { "resource": "File:Foobar.jpg" } }, }, }, }, 'data-mw': { body: { ids: { "mwAg": { "caption": "Testing 123" }, }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: '', }, }, }) .expect(validWikitextResponse('[[File:Foobar.jpg]]')) .end(done); }); it('should give precedence to modified data-mw over original (captions 3)', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: '', 'data-mw': { body: { ids: { "mwAg": {}, }, }, }, original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAg": { "optList": [{ "ck": "caption", "ak": "Testing 123" }] }, "mwAw": { "a": { "href": "./File:Foobar.jpg" }, "sa": {} }, "mwBA": { "a": { "resource": "./File:Foobar.jpg", "height": "28", "width": "240" },"sa": { "resource": "File:Foobar.jpg" } }, }, }, }, 'data-mw': { body: { ids: { "mwAg": { "caption": "Testing 123" }, }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: '', }, }, }) .expect(validWikitextResponse('[[File:Foobar.jpg]]')) .end(done); }); it('should apply extra normalizations', function(done) { client.req .post(mockDomain + '/v3/transform/html/to/wikitext/') .send({ html: '', original: { title: 'Doesnotexist' }, }) .expect(validWikitextResponse( '' )) .end(done); }); it('should return a request too large error', function(done) { if (skipForNow) { return this.skip(); } // Set limits in config client.req .post(mockDomain + '/v3/transform/html/to/wikitext/') .send({ original: { title: 'Large_Page', }, html: "a".repeat(parsoidOptions.limits.html2wt.maxHTMLSize + 1), }) .expect(413) .end(done); }); it('should fail to downgrade the original version for an unknown transition', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/wikitext/') .send({ html: '\n123', original: { title: 'Doesnotexist', 'data-parsoid': { body: { "ids": {} } }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2222.0.0"', }, body: '\n123', }, }, }) .expect(400) .end(done); }); }); // end html2wt describe('pb2pb', function() { it('should require an original or previous version', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/Reuse_Page/100') .send({}) .expect(400) .end(done); }); var previousRevHTML = { revid: 99, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'pink
', }, "data-parsoid": { headers: { 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"', }, body: { 'counter': 2, 'ids': { 'mwAg': { 'pi': [[]], 'src': '{{colours of the rainbow}}' }, // artificially added src }, }, }, }; it('should error when revision not found (transform, pb2pb)', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/Doesnotexist') .send({ previous: previousRevHTML, }) .expect(404) .end(done); }); // FIXME: Expansion reuse wasn't ported, see T98995 it.skip('should accept the previous revision to reuse expansions', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/Reuse_Page/100') .send({ previous: previousRevHTML, }) .expect(validPageBundleResponse(function(doc) { doc.body.firstChild.textContent.should.match(/pink/); })) .end(done); }); var origHTML = Util.clone(previousRevHTML); origHTML.revid = 100; // FIXME: Expansion reuse wasn't ported, see T98995 it.skip('should accept the original and reuse certain expansions', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/Reuse_Page/100') .send({ updates: { transclusions: true, }, original: origHTML, }) .expect(validPageBundleResponse(function(doc) { doc.body.firstChild.textContent.should.match(/purple/); })) .end(done); }); it('should refuse an unknown conversion (2.x -> 999.x)', function(done) { previousRevHTML.html.headers['content-type'].should.equal('text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2.8.0"'); client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/Reuse_Page/100') .set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/999.0.0"') .send({ previous: previousRevHTML, }) .expect(415) .end(done); }); it('should downgrade 999.x content to 2.x', function(done) { var contentVersion = '2.8.0'; client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/') .set('Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"') .send({ original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: { "mwAQ": { "pi": [[{ "k": "1" }]] } }, }, }, 'data-mw': { body: { ids: { "mwAQ": { "parts": [{ "template": { "target": { "wt": "1x", "href": "./Template:1x" }, "params": { "1": { "wt": "hi" } }, "i": 0 } }] } }, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"', }, body: '\nho
', }, }, }) .expect(status200) .expect(acceptablePageBundleResponse(contentVersion, function(html) { // In < 999.x, data-mw is still inline. html.should.match(/\s+data-mw\s*=\s*['"]/); html.should.not.match(/\s+data-parsoid\s*=\s*['"]/); var doc = domino.createDocument(html); var meta = doc.querySelector('meta[property="mw:html:version"], meta[property="mw:htmlVersion"]'); meta.getAttribute('content').should.equal(contentVersion); })) .end(done); }); it('should accept the original and update the redlinks', function(done) { if (skipForNow) { return this.skip(); } // Create pages to fix redlinks count // NOTE: Keep this on an older version to show that it's preserved // through the transformation. var contentVersion = '2.0.0'; client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/') .send({ updates: { redlinks: true, }, original: { title: 'Doesnotexist', 'data-parsoid': { body: { ids: {}, }, }, html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"', }, body: 'Special:Version Doesnotexist Redirected
', }, }, }) .expect(acceptablePageBundleResponse(contentVersion, function(html) { var doc = domino.createDocument(html); doc.body.querySelectorAll('a').length.should.equal(3); var redLinks = doc.body.querySelectorAll('.new'); redLinks.length.should.equal(1); redLinks[0].getAttribute('title').should.equal('Doesnotexist'); var redirects = doc.body.querySelectorAll('.mw-redirect'); redirects.length.should.equal(1); redirects[0].getAttribute('title').should.equal('Redirected'); })) .end(done); }); describe('Variant conversion', function() { it('should return unconverted nl page if sr variant conversion is requested on nl page', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/MediaWiki:ok%2Fnl') .send({ updates: { variant: { target: 'sr-el' }, }, original: { revid, html: { headers: { 'content-type': 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'абвг abcd
', }, }, }) .expect(status200) .expect(validPageBundleResponse(function(doc) { doc.body.textContent.should.equal('абвг abcd'); })) .end(done); }); it('should accept the original and do variant conversion (given oldid)', function(done) { if (skipForNow) { return this.skip(); } // Enabled language conversion client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/') .send({ updates: { variant: { target: 'sr-el' }, }, original: { revid: 104, /* sets the pagelanguage */ html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'абвг abcd x
', }, }, }) .expect(status200) .expect((res) => { // We don't actually require the result to have data-parsoid // if the input didn't have data-parsoid; hack the result // in order to make validPageBundleResponse() pass. res.body['data-parsoid'].body = {}; }) .expect(validPageBundleResponse(function(doc) { doc.body.textContent.should.equal('abvg abcd x'); })) .expect((res) => { const headers = res.body.html.headers; headers.should.have.property('content-language'); headers['content-language'].should.equal('sr-el'); headers.should.have.property('vary'); headers.vary.should.match(/\bAccept-Language\b/i); }) .end(done); }); it('should accept the original and do variant conversion (given pagelanguage)', function(done) { if (skipForNow) { return this.skip(); } // Enabled language conversion client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/') .set('Content-Language', 'sr') .set('Accept-Language', 'sr-el') .send({ updates: { variant: { /* target implicit from accept-language */ }, }, original: { html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'абвг abcd
', }, }, }) .expect(status200) .expect((res) => { // We don't actually require the result to have data-parsoid // if the input didn't have data-parsoid; hack the result // in order to make validPageBundleResponse() pass. res.body['data-parsoid'].body = {}; }) .expect(validPageBundleResponse(function(doc) { doc.body.textContent.should.equal('abvg abcd'); })) .expect((res) => { const headers = res.body.html.headers; headers.should.have.property('content-language'); headers['content-language'].should.equal('sr-el'); headers.should.have.property('vary'); headers.vary.should.match(/\bAccept-Language\b/i); }) .end(done); }); it('should not perform variant conversion w/ invalid variant (given pagelanguage)', function(done) { client.req .post(mockDomain + '/v3/transform/pagebundle/to/pagebundle/') .set('Content-Language', 'sr') .set('Accept-Language', 'sr-BOGUS') .send({ updates: { variant: { /* target implicit from accept-language */ }, }, original: { html: { headers: { 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"', }, body: 'абвг abcd
', }, }, }) .expect(status200) .expect((res) => { // We don't actually require the result to have data-parsoid // if the input didn't have data-parsoid; hack the result // in order to make validPageBundleResponse() pass. res.body['data-parsoid'].body = {}; }) .expect(validPageBundleResponse(function(doc) { doc.body.textContent.should.equal('абвг abcd'); })) .expect((res) => { const headers = res.body.html.headers; headers.should.have.property('content-language'); headers['content-language'].should.equal('sr'); headers.should.have.property('vary'); headers.vary.should.match(/\bAccept-Language\b/i); }) .end(done); }); }); }); // end pb2pb describe( 'roundtrip-test', function () { // This mirrors the behavior of the runTests function in roundtrip-test.js const transformOptions = { // For compatibility with Parsoid/PHP service, per roundtrip-test.js offsetType: 'ucs2' }; it( 'Roundtrip should convert wikitext to HTML and back, with selser working', async () => { // get the wikitext of the page const { text: oldWt } = await client.req .get( mockDomain + `/v3/page/wikitext/${ pageEncoded }` ); // First, fetch the HTML for the requested page's wikitext const wt2html = await client.req .post( mockDomain + `/v3/transform/wikitext/to/pagebundle/${ pageEncoded }` ) .send( Object.assign( { wikitext: oldWt, }, transformOptions ) ); assert.equal( 200, wt2html.statusCode, wt2html.text ); // Now, request the wikitext for the obtained HTML, without providing the original HTML const pb2wt1 = await client.req .post( mockDomain + `/v3/transform/pagebundle/to/wikitext/${ pageEncoded }` ) .send( Object.assign( { html: wt2html.body.html, }, transformOptions ) ); assert.equal( 200, pb2wt1.statusCode, pb2wt1.text ); // This might fail, since we are not applying selser. // We are trusting that the conversion back to wikitext will be clean. assert.equal( pb2wt1.text, oldWt ); // Now, request the wikitext for the obtained HTML, with Selser var newDocument = DOMUtils.parseHTML( wt2html.body.html.body ); var newNode = newDocument.createComment('rtSelserEditTestComment'); newDocument.body.appendChild(newNode); const pb2wt2 = await client.req .post( mockDomain + `/v3/transform/pagebundle/to/wikitext/${ pageEncoded }` ) .send( { html: newDocument.outerHTML, oldid: revid, original: { 'data-parsoid': wt2html.body['data-parsoid'], 'data-mw': wt2html.body['data-mw'], wikitext: { body: oldWt }, html: wt2html.body.html, }, ...transformOptions } ); assert.equal( 200, pb2wt2.statusCode, pb2wt2.text ); // Check that the comment was included in the wikitext assert.include( pb2wt2.text, 'rtSelserEditTestComment' ); // Remove the selser trigger comment const actual2 = pb2wt2.text.replace(/\n*$/, ''); assert.equal( actual2, oldWt ); } ); } ); describe( 'ETags', function () { it( '/transform/html must accept the ETag in the If-Match header, if one is returned by /page/html (T238849)', async () => { const { statusCode: status1, headers: headers1, text: text1 } = await client.req .get( mockDomain + `/v3/page/html/${ page }/${ revid }` ) .query( { stash: 'yes' } ); assert.deepEqual( status1, 200, text1 ); if ( !headers1.etag ) { // This is a structure test that is asserting one of two acceptable situations: // Either, the page endpoint doesn't return an ETag. This implies that the // HTML wasn't stored anywhere, so it can't be re-used. // Or the transform endpoint accepts the ETag returned by the page endpoint. // This ensures that clients can loop through any ETags they receive. return; } // The request above should have stashed a rendering associated with the ETag it // returned. Pass the ETag in the If-Match header. const { statusCode: status2, text: text2 } = await client.req .post( mockDomain + `/v3/transform/html/to/wikitext/${ page }/${ revid }` ) .set( 'If-Match', headers1.etag ) .send( { html: text1 } ); // Just check we got a 200. The returned wikitext may or may not be "dirty". assert.deepEqual( status2, 200, text2 ); } ); } ); });