Debian Codegraph @

tests/api-testing/REST/Transform.js

raw symbols SCIP
1 /** Cases for testing the Parsoid API through HTTP */
2
3 'use strict';
4
5 const { action, assert, REST, utils } = require( 'api-testing' );
6
7 const domino = require( 'domino' );
8 const should = require( 'chai' ).should();
9 const semver = require( 'semver' );
10 const fs = require( 'fs' );
11
12 const parsoidOptions = {
13 // Limits from DevelopmentSettings.php
14 limits: {
15 // Measured in bytes, per ParserOptions::getMaxIncludeSize.
16 wt2html: { maxWikitextSize: 20 * 1024 },
17
18 // Measured in characters.
19 html2wt: { maxHTMLSize: 100 * 1024 }
20 }
21 };
22
23 // FIXME(T283875): These should all be re-enabled
24 const skipForNow = true;
25
26 // Minimal version required, caret semantic applies
27 const defaultContentVersion = '2.4.0';
28
29 // section wrappers are a distraction from the main business of
30 // this file which is to verify functionality of API end points
31 // independent of what they are returning and computing.
32 //
33 // Verifying the correctness of content is actually the job of
34 // parser tests and other tests.
35 //
36 // So, hide most of that that distraction in a helper.
37 //
38 // Right now, all uses of this helper have empty lead sections.
39 // But, maybe in the future, this may change. So, retain the option.
40 function validateDoc( doc, nodeName, emptyLead ) {
41 const leadSection = doc.body.firstChild;
42 leadSection.nodeName.should.equal( 'SECTION' );
43 if ( emptyLead ) {
44 // Could have whitespace and comments
45 leadSection.childElementCount.should.equal( 0 );
46 }
47 const nonEmptySection = emptyLead ? leadSection.nextSibling : leadSection;
48 nonEmptySection.firstChild.nodeName.should.equal( nodeName );
49 }
50
51 // Matcher checking that the result status is 200, including the actual response
52 // text in the output if it isn't.
53 function status200( res ) {
54 assert.strictEqual( res.status, 200, res.text );
55 }
56
57 // Return a matcher function that checks whether a content type matches the given parameters.
58 function contentTypeMatcher( expectedMime, expectedSpec, expectedVersion ) {
59 const pattern = /^([-\w]+\/[-\w]+); charset=utf-8; profile="https:\/\/www.mediawiki.org\/wiki\/Specs\/([-\w]+)\/(\d+\.\d+\.\d+)"$/;
60
61 return ( actual ) => {
62 const parts = pattern.exec( actual );
63 if ( !parts ) {
64 return false;
65 }
66
67 const [ , mime, spec, version ] = parts;
68
69 // match version using caret semantics
70 if ( !semver.satisfies( version, `^${ expectedVersion || defaultContentVersion }` ) ) {
71 return false;
72 }
73
74 if ( mime !== expectedMime || spec !== expectedSpec ) {
75 return false;
76 }
77
78 return true;
79 };
80 }
81
82 // TODO: Replace all occurrences of (Lint Page/Lint_Page) with `page`.
83 describe( '/transform/ endpoint', () => {
84 const client = new REST();
85 const endpointPrefix = client.pathPrefix = 'rest.php/v1';
86 const page = utils.title( 'TransformSource_' );
87 const pageEncoded = encodeURIComponent( page );
88 const pageContent = '{|\nhi\n|ho\n|}';
89 let revid;
90
91 before( async function () {
92 this.timeout( 30000 );
93
94 const alice = await action.alice();
95
96 // Create pages
97 let edit = await alice.edit( page, { text: pageContent } );
98 edit.result.should.equal( 'Success' );
99 revid = edit.newrevid;
100
101 edit = await alice.edit( 'JSON Page', {
102 text: '[1]', contentmodel: 'json'
103 } );
104 edit.result.should.equal( 'Success' );
105 } );
106
107 describe( 'formats', () => {
108
109 it( 'should accept application/x-www-form-urlencoded', ( done ) => {
110 client.req
111 .post( endpointPrefix + '/transform/wikitext/to/html/' )
112 .type( 'form' )
113 .send( {
114 wikitext: '== h2 =='
115 } )
116 .expect( status200 )
117 .expect( ( res ) => {
118 validateDoc( domino.createDocument( res.text ), 'H2', true );
119 } )
120 .end( done );
121 } );
122
123 it( 'should accept application/json', ( done ) => {
124 client.req
125 .post( endpointPrefix + '/transform/wikitext/to/html/' )
126 .type( 'json' )
127 .send( {
128 wikitext: '== h2 =='
129 } )
130 .expect( status200 )
131 .expect( ( res ) => {
132 validateDoc( domino.createDocument( res.text ), 'H2', true );
133 } )
134 .end( done );
135 } );
136
137 it( 'should accept multipart/form-data', ( done ) => {
138 client.req
139 .post( endpointPrefix + '/transform/wikitext/to/html/' )
140 .field( 'wikitext', '== h2 ==' )
141 .expect( status200 )
142 .expect( ( res ) => {
143 validateDoc( domino.createDocument( res.text ), 'H2', true );
144 } )
145 .end( done );
146 } );
147 } ); // formats
148
149 const acceptableHtmlResponse = function ( contentVersion, expectFunc ) {
150 return function ( res ) {
151 res.statusCode.should.equal( 200, res.txt );
152 res.headers.should.have.property( 'content-type' );
153 res.headers[ 'content-type' ].should.satisfy(
154 contentTypeMatcher( 'text/html', 'HTML', contentVersion )
155 );
156 res.text.should.not.equal( '' );
157 if ( expectFunc ) {
158 return expectFunc( res.text );
159 }
160 };
161 };
162
163 const acceptablePageBundleResponse = function ( contentVersion, expectFunc ) {
164 return function ( res ) {
165 res.statusCode.should.equal( 200, res.txt );
166 res.headers.should.have.property( 'content-type' );
167 res.headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'application/json', 'pagebundle', contentVersion ) );
168 res.body.should.have.property( 'html' );
169 res.body.html.should.have.property( 'headers' );
170 res.body.html.headers.should.have.property( 'content-type' );
171 res.body.html.headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'text/html', 'HTML', contentVersion ) );
172 res.body.html.should.have.property( 'body' );
173 res.body.should.have.property( 'data-parsoid' );
174 res.body[ 'data-parsoid' ].should.have.property( 'headers' );
175 res.body[ 'data-parsoid' ].headers.should.have.property( 'content-type' );
176 res.body[ 'data-parsoid' ].headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'application/json', 'data-parsoid', contentVersion ) );
177 res.body[ 'data-parsoid' ].should.have.property( 'body' );
178
179 if ( semver.gte( contentVersion, '999.0.0' ) ) {
180 res.body.should.have.property( 'data-mw' );
181 res.body[ 'data-mw' ].should.have.property( 'headers' );
182 res.body[ 'data-mw' ].headers.should.have.property( 'content-type' );
183 res.body[ 'data-mw' ].headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'application/json', 'data-mw', contentVersion ) );
184 res.body[ 'data-mw' ].should.have.property( 'body' );
185 }
186 if ( expectFunc ) {
187 return expectFunc( res.body.html.body );
188 }
189 };
190 };
191
192 describe( 'accepts', () => {
193
194 it( 'should not accept requests for older content versions (html)', ( done ) => {
195 client.req
196 .post( endpointPrefix + '/transform/wikitext/to/html/' )
197 .set( 'Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"' )
198 .send( { wikitext: '== h2 ==' } )
199 .expect( 406 )
200 .expect( ( res ) => {
201 // FIXME: See skipped html error test above
202 JSON.parse( res.error.text ).errorKey.should.equal(
203 'rest-unsupported-target-format'
204 );
205 } )
206 .end( done );
207 } );
208
209 // Support for transforming from/to pagebundle is disabled in production.
210 it.skip( 'should not accept requests for older content versions (pagebundle)', ( done ) => {
211 client.req
212 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
213 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"' )
214 .send( { wikitext: '== h2 ==' } )
215 .expect( 406 )
216 .expect( ( res ) => {
217 JSON.parse( res.error.text ).message.should.equal(
218 'Not acceptable'
219 );
220 } )
221 .end( done );
222 } );
223
224 it( 'should not accept requests for other profiles (html)', ( done ) => {
225 client.req
226 .post( endpointPrefix + '/transform/wikitext/to/html/' )
227 .set( 'Accept', 'text/html; profile="something different"' )
228 .send( { wikitext: '== h2 ==' } )
229 .expect( 406 )
230 .end( done );
231 } );
232
233 // Support for transforming from/to pagebundle is disabled in production.
234 it.skip( 'should not accept requests for other profiles (pagebundle)', ( done ) => {
235 client.req
236 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
237 .set( 'Accept', 'application/json; profile="something different"' )
238 .send( { wikitext: '== h2 ==' } )
239 .expect( 406 )
240 .end( done );
241 } );
242
243 it( 'should accept wildcards (html)', ( done ) => {
244 client.req
245 .post( endpointPrefix + '/transform/wikitext/to/html/' )
246 .set( 'Accept', '*/*' )
247 .send( { wikitext: '== h2 ==' } )
248 .expect( status200 )
249 .expect( acceptableHtmlResponse( defaultContentVersion ) )
250 .end( done );
251 } );
252
253 // Support for transforming from/to pagebundle is disabled in production.
254 it.skip( 'should accept wildcards (pagebundle)', ( done ) => {
255 client.req
256 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
257 .set( 'Accept', '*/*' )
258 .send( { wikitext: '== h2 ==' } )
259 .expect( status200 )
260 .expect( acceptablePageBundleResponse( defaultContentVersion ) )
261 .end( done );
262 } );
263
264 // T347426: Support for non-default major HTML versions has been disabled
265 it.skip( 'should prefer higher quality (html)', ( done ) => {
266 const contentVersion = '999.0.0';
267 client.req
268 .post( endpointPrefix + '/transform/wikitext/to/html/' )
269 .set( 'Accept',
270 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.4.0"; q=0.5,' +
271 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"; q=0.8' )
272 .send( { wikitext: '== h2 ==' } )
273 .expect( status200 )
274 .expect( acceptableHtmlResponse( contentVersion ) )
275 .end( done );
276 } );
277
278 // Support for transforming from/to pagebundle is disabled in production.
279 it.skip( 'should prefer higher quality (pagebundle)', ( done ) => {
280 const contentVersion = '999.0.0';
281 client.req
282 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
283 .set( 'Accept',
284 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/2.4.0"; q=0.5,' +
285 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/999.0.0"; q=0.8' )
286 .send( { wikitext: '== h2 ==' } )
287 .expect( status200 )
288 .expect( acceptablePageBundleResponse( contentVersion ) )
289 .end( done );
290 } );
291
292 it( 'should accept requests for the latest content version (html)', ( done ) => {
293 client.req
294 .post( endpointPrefix + '/transform/wikitext/to/html/' )
295 .send( { wikitext: '== h2 ==' } )
296 .expect( status200 )
297 .expect( acceptableHtmlResponse( defaultContentVersion ) )
298 .end( done );
299 } );
300
301 // Support for transforming from/to pagebundle is disabled in production.
302 it.skip( 'should accept requests for the latest content version (pagebundle)', ( done ) => {
303 client.req
304 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
305 .send( { wikitext: '== h2 ==' } )
306 .expect( status200 )
307 .expect( acceptablePageBundleResponse( defaultContentVersion ) )
308 .end( done );
309 } );
310
311 it( 'should accept requests for content version 2.x (html)', ( done ) => {
312 const contentVersion = '2.4.0';
313 client.req
314 .post( endpointPrefix + '/transform/wikitext/to/html/' )
315 .set( 'Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"' )
316 .send( { wikitext: '{{1x|hi}}' } )
317 .expect( status200 )
318 .expect( acceptableHtmlResponse( contentVersion ) )
319 .end( done );
320 } );
321
322 // Support for transforming from/to pagebundle is disabled in production.
323 it.skip( 'should accept requests for content version 2.x (pagebundle)', ( done ) => {
324 const contentVersion = '2.4.0';
325 client.req
326 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
327 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"' )
328 .send( { wikitext: '{{1x|hi}}' } )
329 .expect( status200 )
330 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
331 // In < 999.x, data-mw is still inline.
332 html.should.match( /\s+data-mw\s*=\s*['"]/ );
333 } ) )
334 .end( done );
335 } );
336
337 // Note that these tests aren't that useful directly after a major version bump
338
339 it( 'should accept requests for older content version 2.x (html)', ( done ) => {
340 const contentVersion = '2.4.0';
341 client.req
342 .post( endpointPrefix + '/transform/wikitext/to/html/' )
343 .set( 'Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/2.0.0"' ) // Keep this on the older version
344 .send( { wikitext: '{{1x|hi}}' } )
345 .expect( status200 )
346 .expect( acceptableHtmlResponse( contentVersion ) )
347 .end( done );
348 } );
349
350 // Support for transforming from/to pagebundle is disabled in production.
351 it.skip( 'should accept requests for older content version 2.x (pagebundle)', ( done ) => {
352 const contentVersion = '2.4.0';
353 client.req
354 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
355 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/2.0.0"' ) // Keep this on the older version
356 .send( { wikitext: '{{1x|hi}}' } )
357 .expect( status200 )
358 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
359 // In < 999.x, data-mw is still inline.
360 html.should.match( /\s+data-mw\s*=\s*['"]/ );
361 } ) )
362 .end( done );
363 } );
364
365 // Support for transforming from/to pagebundle is disabled in production.
366 it.skip( 'should sanity check 2.x content (pagebundle)', ( done ) => {
367 // Missing files in wiki
368 const contentVersion = '2.4.0';
369 client.req
370 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
371 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"' )
372 .send( { wikitext: '[[File:Audio.oga]]' } )
373 .expect( status200 )
374 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
375 const doc = domino.createDocument( html );
376 doc.querySelectorAll( 'audio' ).length.should.equal( 1 );
377 doc.querySelectorAll( 'video' ).length.should.equal( 0 );
378 } ) )
379 .end( done );
380 } );
381
382 // T347426: Support for non-default major HTML versions has been disabled
383 it.skip( 'should accept requests for content version 999.x (html)', ( done ) => {
384 const contentVersion = '999.0.0';
385 client.req
386 .post( endpointPrefix + '/transform/wikitext/to/html/' )
387 .set( 'Accept', 'text/html; profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"' )
388 .send( { wikitext: '{{1x|hi}}' } )
389 .expect( status200 )
390 .expect( acceptableHtmlResponse( contentVersion ) )
391 .end( done );
392 } );
393
394 // Support for transforming from/to pagebundle is disabled in production.
395 it.skip( 'should accept requests for content version 999.x (pagebundle)', ( done ) => {
396 const contentVersion = '999.0.0';
397 client.req
398 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
399 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"' )
400 .send( { wikitext: '{{1x|hi}}' } )
401 .expect( status200 )
402 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
403 // In 999.x, data-mw is in the pagebundle.
404 html.should.not.match( /\s+data-mw\s*=\s*['"]/ );
405 } ) )
406 .end( done );
407 } );
408
409 } ); // accepts
410
411 const validWikitextResponse = function ( expected ) {
412 return function ( res ) {
413 res.statusCode.should.equal( 200 );
414 res.headers.should.have.property( 'content-type' );
415 res.headers[ 'content-type' ].should.equal(
416 // note that express does some reordering
417 'text/plain; charset=utf-8; profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
418 );
419 if ( expected !== undefined ) {
420 res.text.should.equal( expected );
421 } else {
422 res.text.should.not.equal( '' );
423 }
424 };
425 };
426
427 const validHtmlResponse = function ( expectFunc ) {
428 return function ( res ) {
429 res.statusCode.should.equal( 200 );
430 res.headers.should.have.property( 'content-type' );
431 res.headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'text/html', 'HTML' ) );
432
433 const doc = domino.createDocument( res.text );
434 if ( expectFunc ) {
435 return expectFunc( doc );
436 } else {
437 res.text.should.not.equal( '' );
438 }
439 };
440 };
441
442 const validPageBundleResponse = function ( expectFunc ) {
443 return function ( res ) {
444 res.statusCode.should.equal( 200 );
445 res.body.should.have.property( 'html' );
446 res.body.html.should.have.property( 'headers' );
447 res.body.html.headers.should.have.property( 'content-type' );
448 res.body.html.headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'text/html', 'HTML' ) );
449 res.body.html.should.have.property( 'body' );
450 res.body.should.have.property( 'data-parsoid' );
451 res.body[ 'data-parsoid' ].should.have.property( 'headers' );
452 res.body[ 'data-parsoid' ].headers.should.have.property( 'content-type' );
453 res.body[ 'data-parsoid' ].headers[ 'content-type' ].should.satisfy( contentTypeMatcher( 'application/json', 'data-parsoid' ) );
454 res.body[ 'data-parsoid' ].should.have.property( 'body' );
455 // TODO: Check data-mw when 999.x is the default.
456 console.assert( !semver.gte( defaultContentVersion, '999.0.0' ) );
457 const doc = domino.createDocument( res.body.html.body );
458 if ( expectFunc ) {
459 return expectFunc( doc, res.body[ 'data-parsoid' ].body );
460 }
461 };
462 };
463
464 describe( 'wt2lint', () => {
465
466 it( 'should lint the given wikitext', function ( done ) {
467 if ( skipForNow ) {
468 return this.skip();
469 } // Enable linting config
470 client.req
471 .post( endpointPrefix + '/transform/wikitext/to/lint/' )
472 .send( {
473 wikitext: {
474 headers: {
475 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
476 },
477 body: '{|\nhi\n|ho\n|}'
478 }
479 } )
480 .expect( status200 )
481 .expect( ( res ) => {
482 res.body.should.be.instanceof( Array );
483 res.body.length.should.equal( 1 );
484 res.body[ 0 ].type.should.equal( 'fostered' );
485 } )
486 .end( done );
487 } );
488
489 it( 'should lint the given revision, transform', function ( done ) {
490 if ( skipForNow ) {
491 return this.skip();
492 } // Enable linting config
493 client.req
494 .post( endpointPrefix + '/transform/wikitext/to/lint/Lint_Page/102' )
495 .send( {} )
496 .expect( status200 )
497 .expect( ( res ) => {
498 res.body.should.be.instanceof( Array );
499 res.body.length.should.equal( 1 );
500 res.body[ 0 ].type.should.equal( 'fostered' );
501 } )
502 .end( done );
503 } );
504
505 it( 'should lint the given page, transform', function ( done ) {
506 if ( skipForNow ) {
507 return this.skip();
508 } // Enable linting config
509 client.req
510 .post( endpointPrefix + '/transform/wikitext/to/lint/Lint_Page' )
511 .send( {} )
512 .expect( status200 )
513 .expect( ( res ) => {
514 res.body.should.be.instanceof( Array );
515 res.body.length.should.equal( 1 );
516 res.body[ 0 ].type.should.equal( 'fostered' );
517 } )
518 .end( done );
519 } );
520
521 it( 'should lint multibyte wikitext', function ( done ) {
522 if ( skipForNow ) {
523 return this.skip();
524 } // Enable linting config
525 client.req
526 .post( endpointPrefix + '/transform/wikitext/to/lint/' )
527 .send( {
528 wikitext: {
529 headers: {
530 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
531 },
532 body: "ăăă ''test"
533 }
534 } )
535 .expect( status200 )
536 .expect( ( res ) => {
537 res.body.should.be.instanceof( Array );
538 res.body.length.should.equal( 1 );
539 res.body[ 0 ].type.should.equal( 'missing-end-tag' );
540 // res.body[ 0 ].dsr.should.eql( [ 7, 13, 2, 0 ] ); // 'byte' offsets
541 res.body[ 0 ].dsr.should.eql( [ 4, 10, 2, 0 ] ); // 'ucs2' offsets
542 } )
543 .end( done );
544 } );
545
546 } );
547
548 describe( 'wt2html', () => {
549
550 it( 'should accept wikitext as a string for html', ( done ) => {
551 client.req
552 .post( endpointPrefix + '/transform/wikitext/to/html/' )
553 .send( {
554 wikitext: '== h2 =='
555 } )
556 .expect( validHtmlResponse( ( doc ) => {
557 validateDoc( doc, 'H2', true );
558 } ) )
559 .end( done );
560 } );
561
562 it( 'should accept json contentmodel as a string for html', ( done ) => {
563 client.req
564 .post( endpointPrefix + '/transform/wikitext/to/html/' )
565 .send( {
566 wikitext: '{"1":2}',
567 contentmodel: 'json'
568 } )
569 .expect( validHtmlResponse( ( doc ) => {
570 doc.body.firstChild.nodeName.should.equal( 'TABLE' );
571 } ) )
572 .end( done );
573 } );
574
575 // Support for transforming from/to pagebundle is disabled in production.
576 it.skip( 'should accept wikitext as a string for pagebundle', ( done ) => {
577 client.req
578 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
579 .send( {
580 wikitext: '== h2 =='
581 } )
582 .expect( validPageBundleResponse( ( doc ) => {
583 validateDoc( doc, 'H2', true );
584 } ) )
585 .end( done );
586 } );
587
588 // Support for transforming from/to pagebundle is disabled in production.
589 it.skip( 'should accept json contentmodel as a string for pagebundle', ( done ) => {
590 client.req
591 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
592 .send( {
593 wikitext: '{"1":2}',
594 contentmodel: 'json'
595 } )
596 .expect( validPageBundleResponse( ( doc ) => {
597 doc.body.firstChild.nodeName.should.equal( 'TABLE' );
598 should.not.exist( doc.querySelector( '*[typeof="mw:Error"]' ) );
599 } ) )
600 .end( done );
601 } );
602
603 it( 'should accept wikitext with headers', ( done ) => {
604 client.req
605 .post( endpointPrefix + '/transform/wikitext/to/html/' )
606 .send( {
607 wikitext: {
608 headers: {
609 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
610 },
611 body: '== h2 =='
612 }
613 } )
614 .expect( validHtmlResponse( ( doc ) => {
615 validateDoc( doc, 'H2', true );
616 } ) )
617 .end( done );
618 } );
619
620 it( 'should require a title when no wikitext is provided (html)', ( done ) => {
621 client.req
622 .post( endpointPrefix + '/transform/wikitext/to/html/' )
623 .send( {} )
624 .expect( 400 )
625 .end( done );
626 } );
627
628 // Support for transforming from/to pagebundle is disabled in production.
629 it.skip( 'should require a title when no wikitext is provided (pagebundle)', ( done ) => {
630 client.req
631 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
632 .send( {} )
633 .expect( 400 )
634 .end( done );
635 } );
636
637 it( 'should error when revision not found (transform, wt2html)', ( done ) => {
638 client.req
639 .post( endpointPrefix + '/transform/wikitext/to/html/Doesnotexist' )
640 .send( {} )
641 .expect( 404 )
642 .end( done );
643 } );
644
645 // Support for transforming from/to pagebundle is disabled in production.
646 it.skip( 'should error when revision not found (transform, wt2pb)', ( done ) => {
647 client.req
648 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/Doesnotexist' )
649 .send( {} )
650 .expect( 404 )
651 .end( done );
652 } );
653
654 it( 'should accept an original title (html)', ( done ) => {
655 client.req
656 .post( endpointPrefix + '/transform/wikitext/to/html/' )
657 .send( {
658 // no revid or wikitext source provided
659 original: {
660 title: page
661 }
662 } )
663 .expect( validHtmlResponse() )
664 .end( done );
665 } );
666
667 // Support for transforming from/to pagebundle is disabled in production.
668 it.skip( 'should accept an original title (pagebundle)', ( done ) => {
669 client.req
670 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
671 .send( {
672 // no revid or wikitext source provided
673 original: {
674 title: page
675 }
676 } )
677 .expect( validPageBundleResponse() )
678 .end( done );
679 } );
680
681 it( 'should accept an original title, other than main', ( done ) => {
682 client.req
683 .post( endpointPrefix + '/transform/wikitext/to/html/' )
684 .send( {
685 original: {
686 title: page
687 }
688 } )
689 .expect( validHtmlResponse() )
690 .end( done );
691 } );
692
693 it( 'should not require a title when empty wikitext is provided (html)', ( done ) => {
694 client.req
695 .post( endpointPrefix + '/transform/wikitext/to/html/' )
696 .send( {
697 wikitext: ''
698 } )
699 .expect( validHtmlResponse( ( doc ) => {
700 doc.body.children.length.should.equal( 1 ); // empty lead section
701 doc.body.firstChild.nodeName.should.equal( 'SECTION' );
702 doc.body.firstChild.children.length.should.equal( 0 );
703 } ) )
704 .end( done );
705 } );
706
707 // Support for transforming from/to pagebundle is disabled in production.
708 it.skip( 'should not require a title when empty wikitext is provided (pagebundle)', ( done ) => {
709 client.req
710 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
711 .send( {
712 wikitext: ''
713 } )
714 .expect( validPageBundleResponse() )
715 .end( done );
716 } );
717
718 it( 'should not require a title when wikitext is provided', ( done ) => {
719 client.req
720 .post( endpointPrefix + '/transform/wikitext/to/html/' )
721 .send( {
722 wikitext: '== h2 =='
723 } )
724 .expect( validHtmlResponse( ( doc ) => {
725 validateDoc( doc, 'H2', true );
726 } ) )
727 .end( done );
728 } );
729
730 it( 'should not require a rev id when wikitext and a title is provided', ( done ) => {
731 client.req
732 .post( endpointPrefix + '/transform/wikitext/to/html/Main_Page' )
733 .send( {
734 wikitext: '== h2 =='
735 } )
736 .expect( validHtmlResponse( ( doc ) => {
737 validateDoc( doc, 'H2', true );
738 } ) )
739 .end( done );
740 } );
741
742 it( 'should accept the wikitext source as original data', ( done ) => {
743 client.req
744 .post( endpointPrefix + '/transform/wikitext/to/html/Main_Page/1' )
745 .send( {
746 original: {
747 wikitext: {
748 headers: {
749 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
750 },
751 body: '== h2 =='
752 }
753 }
754 } )
755 .expect( validHtmlResponse( ( doc ) => {
756 validateDoc( doc, 'H2', true );
757 } ) )
758 .end( done );
759 } );
760
761 it( 'should use the proper source text', function ( done ) {
762 if ( skipForNow ) {
763 return this.skip();
764 } // Missing template 1x
765 client.req
766 .post( endpointPrefix + '/transform/wikitext/to/html/Main_Page/1' )
767 .send( {
768 original: {
769 wikitext: {
770 headers: {
771 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
772 },
773 body: '{{1x|foo|bar=bat}}'
774 }
775 }
776 } )
777 .expect( validHtmlResponse( ( doc ) => {
778 validateDoc( doc, 'P', false );
779 const span = doc.querySelector( 'span[typeof="mw:Transclusion"]' );
780 const dmw = JSON.parse( span.getAttribute( 'data-mw' ) );
781 const template = dmw.parts[ 0 ].template;
782 template.target.wt.should.equal( '1x' );
783 template.params[ 1 ].wt.should.equal( 'foo' );
784 template.params.bar.wt.should.equal( 'bat' );
785 } ) )
786 .end( done );
787 } );
788
789 it( 'should accept the wikitext source as original without a title or revision', ( done ) => {
790 client.req
791 .post( endpointPrefix + '/transform/wikitext/to/html/' )
792 .send( {
793 original: {
794 wikitext: {
795 headers: {
796 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
797 },
798 body: '== h2 =='
799 }
800 }
801 } )
802 .expect( validHtmlResponse( ( doc ) => {
803 validateDoc( doc, 'H2', true );
804 } ) )
805 .end( done );
806 } );
807
808 it( 'should respect body parameter in wikitext->html (body_only)', ( done ) => {
809 client.req
810 .post( endpointPrefix + '/transform/wikitext/to/html/' )
811 .send( {
812 wikitext: "''foo''",
813 body_only: 1
814 } )
815 .expect( validHtmlResponse() )
816 .expect( ( res ) => {
817 // v3 only returns children of <body>
818 res.text.should.not.match( /<body/ );
819 res.text.should.match( /<p/ );
820 } )
821 .end( done );
822 } );
823
824 // Support for transforming from/to pagebundle is disabled in production.
825 it.skip( 'should respect body parameter in wikitext->pagebundle requests (body_only)', ( done ) => {
826 client.req
827 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
828 .send( {
829 wikitext: "''foo''",
830 body_only: 1
831 } )
832 .expect( validPageBundleResponse() )
833 .expect( ( res ) => {
834 // v3 only returns children of <body>
835 res.body.html.body.should.not.match( /<body/ );
836 res.body.html.body.should.match( /<p/ );
837 // No section wrapping in body-only mode
838 res.body.html.body.should.not.match( /<section/ );
839 } )
840 .end( done );
841 } );
842
843 it( 'should implement subst - simple', function ( done ) {
844 if ( skipForNow ) {
845 return this.skip();
846 } // Missing template 1x
847 client.req
848 .post( endpointPrefix + '/transform/wikitext/to/html/' )
849 .send( { wikitext: '{{1x|foo}}', subst: 'true' } )
850 .expect( validHtmlResponse( ( doc ) => {
851 const body = doc.body;
852 // <body> should have one child, <section>, the lead section
853 body.childElementCount.should.equal( 1 );
854 const p = body.firstChild.firstChild;
855 p.nodeName.should.equal( 'P' );
856 p.innerHTML.should.equal( 'foo' );
857 // The <p> shouldn't be a template expansion, just a plain ol' one
858 p.hasAttribute( 'typeof' ).should.equal( false );
859 // and it shouldn't have any data-parsoid in it
860 p.hasAttribute( 'data-parsoid' ).should.equal( false );
861 } ) )
862 .end( done );
863 } );
864
865 it( 'should implement subst - internal tranclusion', function ( done ) {
866 if ( skipForNow ) {
867 return this.skip();
868 } // Missing template 1x
869 client.req
870 .post( endpointPrefix + '/transform/wikitext/to/html/' )
871 .send( { wikitext: '{{1x|foo {{1x|bar}} baz}}', subst: 'true' } )
872 .expect( validHtmlResponse( ( doc ) => {
873 const body = doc.body;
874 // <body> should have one child, <section>, the lead section
875 body.childElementCount.should.equal( 1 );
876 const p = body.firstChild.firstChild;
877 p.nodeName.should.equal( 'P' );
878 // The <p> shouldn't be a template expansion, just a plain ol' one
879 p.hasAttribute( 'typeof' ).should.equal( false );
880 // and it shouldn't have any data-parsoid in it
881 p.hasAttribute( 'data-parsoid' ).should.equal( false );
882 // The internal tranclusion should be presented as such
883 const tplp = p.firstChild.nextSibling;
884 tplp.nodeName.should.equal( 'SPAN' );
885 tplp.getAttribute( 'typeof' ).should.equal( 'mw:Transclusion' );
886 // And not have data-parsoid, so it's used as new content
887 tplp.hasAttribute( 'data-parsoid' ).should.equal( false );
888 } ) )
889 .end( done );
890 } );
891
892 // Support for transforming from/to pagebundle is disabled in production.
893 it.skip( 'should not allow subst with pagebundle', ( done ) => {
894 client.req
895 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
896 .send( { wikitext: '{{1x|foo}}', subst: 'true' } )
897 .expect( 501 )
898 .end( done );
899 } );
900
901 it( 'should return a request too large error when just over limit (post wt)', ( done ) => {
902 client.req
903 .post( endpointPrefix + '/transform/wikitext/to/html/' )
904 .send( {
905 // One over limit.
906 // Use single-byte characters, since the limit is in byte.
907 wikitext: 'a'.repeat( parsoidOptions.limits.wt2html.maxWikitextSize + 1 )
908 } )
909 .expect( 413 )
910 .end( done );
911 } );
912
913 it( 'should not return a request too large error when just under limit (post wt)', ( done ) => {
914 client.req
915 .post( endpointPrefix + '/transform/wikitext/to/html/' )
916 .send( {
917 // One under limit.
918 // Use single-byte characters, since the limit is in byte.
919 wikitext: 'a'.repeat( parsoidOptions.limits.wt2html.maxWikitextSize - 1 )
920 } )
921 .expect( 200 )
922 .end( done );
923 } );
924
925 it( 'should add redlinks for transform (html)', function ( done ) {
926 if ( skipForNow ) {
927 return this.skip();
928 } // Fix redlinks count, by creating pages
929 client.req
930 .post( endpointPrefix + '/transform/wikitext/to/html/' )
931 .send( {
932 wikitext: '[[Special:Version]] [[Doesnotexist]] [[Redirected]]'
933 } )
934 .expect( validHtmlResponse( ( doc ) => {
935 doc.body.querySelectorAll( 'a' ).length.should.equal( 3 );
936 const redLinks = doc.body.querySelectorAll( '.new' );
937 redLinks.length.should.equal( 1 );
938 redLinks[ 0 ].getAttribute( 'title' ).should.equal( 'Doesnotexist' );
939 const redirects = doc.body.querySelectorAll( '.mw-redirect' );
940 redirects.length.should.equal( 1 );
941 redirects[ 0 ].getAttribute( 'title' ).should.equal( 'Redirected' );
942 } ) )
943 .end( done );
944 } );
945
946 it( 'should add redlinks for transform (pagebundle)', function ( done ) {
947 if ( skipForNow ) {
948 return this.skip();
949 } // Fix redlinks count, by creating pages
950 client.req
951 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
952 .send( {
953 wikitext: '[[Special:Version]] [[Doesnotexist]] [[Redirected]]'
954 } )
955 .expect( validPageBundleResponse( ( doc ) => {
956 doc.body.querySelectorAll( 'a' ).length.should.equal( 3 );
957 const redLinks = doc.body.querySelectorAll( '.new' );
958 redLinks.length.should.equal( 1 );
959 redLinks[ 0 ].getAttribute( 'title' ).should.equal( 'Doesnotexist' );
960 const redirects = doc.body.querySelectorAll( '.mw-redirect' );
961 redirects.length.should.equal( 1 );
962 redirects[ 0 ].getAttribute( 'title' ).should.equal( 'Redirected' );
963 } ) )
964 .end( done );
965 } );
966
967 // Continue to accept sr-el for a while in headers, to remain compatible
968 // with apps which might still be sending the old codes
969 [ 'sr-Latn', 'sr-el' ].forEach( ( srLatn ) => {
970 describe( 'Variant conversion ' + srLatn, () => {
971 it( 'should perform variant conversion for transform given pagelanguage in HTTP header (html)', ( done ) => {
972 client.req
973 .post( endpointPrefix + '/transform/wikitext/to/html/' )
974 .set( 'Accept-Language', srLatn )
975 .set( 'Content-Language', 'sr' )
976 .send( {
977 wikitext: 'абвг abcd x'
978 } )
979 .expect( 'Content-Language', 'sr-Latn' )
980 .expect( 'Vary', /\bAccept-Language\b/i )
981 .expect( validHtmlResponse( ( doc ) => {
982 doc.body.textContent.should.equal( 'abvg abcd x' );
983 } ) )
984 .end( done );
985 } );
986
987 it( 'should perform variant conversion for transform given pagelanguage in HTTP header (pagebundle)', function ( done ) {
988 if ( skipForNow ) {
989 return this.skip();
990 } // page bundle not supported
991 client.req
992 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
993 .set( 'Accept-Language', srLatn )
994 .set( 'Content-Language', 'sr' )
995 .send( {
996 wikitext: 'абвг abcd x'
997 } )
998 .expect( validPageBundleResponse( ( doc ) => {
999 doc.body.textContent.should.equal( 'abvg abcd x' );
1000 } ) )
1001 .expect( ( res ) => {
1002 const headers = res.body.html.headers;
1003 headers.should.have.property( 'content-language' );
1004 headers.should.have.property( 'vary' );
1005 headers[ 'content-language' ].should.equal( 'sr-Latn' );
1006 headers.vary.should.match( /\bAccept-Language\b/i );
1007 } )
1008 .end( done );
1009 } );
1010
1011 it( 'should perform variant conversion for transform given pagelanguage in JSON header (html)', ( done ) => {
1012 client.req
1013 .post( endpointPrefix + '/transform/wikitext/to/html/' )
1014 .set( 'Accept-Language', srLatn )
1015 .send( {
1016 wikitext: {
1017 headers: {
1018 'content-language': 'sr'
1019 },
1020 body: 'абвг abcd x'
1021 }
1022 } )
1023 .expect( 'Content-Language', 'sr-Latn' )
1024 .expect( 'Vary', /\bAccept-Language\b/i )
1025 .expect( validHtmlResponse( ( doc ) => {
1026 doc.body.textContent.should.equal( 'abvg abcd x' );
1027 } ) )
1028 .end( done );
1029 } );
1030
1031 it( 'should perform variant conversion for transform given pagelanguage in JSON header (pagebundle)', function ( done ) {
1032 if ( skipForNow ) {
1033 return this.skip();
1034 } // page bundle not supported
1035 client.req
1036 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
1037 .set( 'Accept-Language', srLatn )
1038 .send( {
1039 wikitext: {
1040 headers: {
1041 'content-language': 'sr'
1042 },
1043 body: 'абвг abcd'
1044 }
1045 } )
1046 .expect( validPageBundleResponse( ( doc ) => {
1047 doc.body.textContent.should.equal( 'abvg abcd' );
1048 } ) )
1049 .expect( ( res ) => {
1050 const headers = res.body.html.headers;
1051 headers.should.have.property( 'content-language' );
1052 headers.should.have.property( 'vary' );
1053 headers[ 'content-language' ].should.equal( 'sr-Latn' );
1054 headers.vary.should.match( /\bAccept-Language\b/i );
1055 } )
1056 .end( done );
1057 } );
1058
1059 it( 'should perform variant conversion for transform given pagelanguage from oldid (html)', ( done ) => {
1060 client.req
1061 .post( endpointPrefix + '/transform/wikitext/to/html/' )
1062 .set( 'Accept-Language', srLatn )
1063 .set( 'Content-Language', 'sr' )
1064 .send( {
1065 original: { revid: 104 },
1066 wikitext: {
1067 body: 'абвг abcd x'
1068 }
1069 } )
1070 .expect( 'Content-Language', 'sr-Latn' )
1071 .expect( 'Vary', /\bAccept-Language\b/i )
1072 .expect( validHtmlResponse( ( doc ) => {
1073 doc.body.textContent.should.equal( 'abvg abcd x' );
1074 } ) )
1075 .end( done );
1076 } );
1077
1078 it( 'should perform variant conversion for transform given pagelanguage from oldid (pagebundle)', function ( done ) {
1079 if ( skipForNow ) {
1080 return this.skip();
1081 } // page bundle not supported
1082 client.req
1083 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
1084 .set( 'Accept-Language', srLatn )
1085 .send( {
1086 original: { revid: 104 },
1087 wikitext: 'абвг abcd'
1088 } )
1089 .expect( validPageBundleResponse( ( doc ) => {
1090 doc.body.textContent.should.equal( 'abvg abcd' );
1091 } ) )
1092 .expect( ( res ) => {
1093 const headers = res.body.html.headers;
1094 headers.should.have.property( 'content-language' );
1095 headers.should.have.property( 'vary' );
1096 headers[ 'content-language' ].should.equal( 'sr-Latn' );
1097 headers.vary.should.match( /\bAccept-Language\b/i );
1098 } )
1099 .end( done );
1100 } );
1101
1102 } );
1103 } );
1104
1105 } ); // end wt2html
1106
1107 const getTextFromFile = function ( name ) {
1108 // eslint-disable-next-line security/detect-non-literal-fs-filename
1109 return fs.readFileSync( __dirname + '/../data/Transform/' + name, 'utf-8' ).trim();
1110 };
1111
1112 describe( 'html2wt', () => {
1113 const htmlOfMainPageWithDataParsoid = getTextFromFile( 'MainPage-data-parsoid.html' );
1114 const htmlOfMainPageWithDataParsoid_1_1_1 = getTextFromFile( 'MainPage-data-parsoid-1.1.1.html' );
1115 const htmlOfMainPageOriginal = getTextFromFile( 'MainPage-original.html' );
1116 const dataParsoidOfMainPageOriginal = JSON.parse( getTextFromFile( 'MainPage-original.data-parsoid' ) );
1117 const htmlOfImage = getTextFromFile( 'Image.html' );
1118 const htmlOfImageWithDataMW = getTextFromFile( 'Image-data-mw.html' );
1119
1120 it( 'should require html when serializing', ( done ) => {
1121 client.req
1122 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1123 .send( {} )
1124 .expect( 400 )
1125 .end( done );
1126 } );
1127
1128 it( 'should error when revision not found (transform, html2wt)', ( done ) => {
1129 client.req
1130 .post( endpointPrefix + '/transform/html/to/wikitext/Doesnotexist/2020' )
1131 .send( {
1132 html: '<pre>hi ho</pre>'
1133 } )
1134 .expect( 404 )
1135 .end( done );
1136 } );
1137
1138 it( 'should not error when oldid not supplied (transform, html2wt)', ( done ) => {
1139 client.req
1140 .post( endpointPrefix + '/transform/html/to/wikitext/Doesnotexist' )
1141 .send( {
1142 html: '<pre>hi ho</pre>'
1143 } )
1144 .expect( validWikitextResponse( ' hi ho\n' ) )
1145 .end( done );
1146 } );
1147
1148 it( 'should accept html as a string', ( done ) => {
1149 client.req
1150 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1151 .send( {
1152 html: htmlOfMainPageWithDataParsoid
1153 } )
1154 .expect( validWikitextResponse() )
1155 .end( done );
1156 } );
1157
1158 const htmlOfJsonConfig = getTextFromFile( 'JsonConfig.html' );
1159 it( 'should accept html for json contentmodel as a string', ( done ) => {
1160 client.req
1161 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1162 .send( {
1163 html: htmlOfJsonConfig,
1164 contentmodel: 'json'
1165 } )
1166 .expect( ( res ) => {
1167 res.statusCode.should.equal( 200 );
1168 res.headers.should.have.property( 'content-type' );
1169 res.headers[ 'content-type' ].should.equal( 'application/json' );
1170 res.text.should.equal( '{"a":4,"b":3}' );
1171 } )
1172 .end( done );
1173 } );
1174
1175 it( 'should accept html with headers', ( done ) => {
1176 client.req
1177 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1178 .send( {
1179 html: {
1180 headers: {
1181 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1182 },
1183 body: htmlOfMainPageWithDataParsoid
1184 }
1185 } )
1186 .expect( validWikitextResponse() )
1187 .end( done );
1188 } );
1189
1190 it( 'should allow a title in the url', ( done ) => {
1191 client.req
1192 .post( endpointPrefix + '/transform/html/to/wikitext/Main_Page' )
1193 .send( {
1194 html: htmlOfMainPageWithDataParsoid
1195 } )
1196 .expect( validWikitextResponse() )
1197 .end( done );
1198 } );
1199
1200 it( 'should allow a title in the original data', ( done ) => {
1201 client.req
1202 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1203 .send( {
1204 html: htmlOfMainPageWithDataParsoid,
1205 original: {
1206 title: 'Main_Page'
1207 }
1208 } )
1209 .expect( validWikitextResponse() )
1210 .end( done );
1211 } );
1212
1213 it( 'should allow a revision id in the url', ( done ) => {
1214 client.req
1215 .post( endpointPrefix + '/transform/html/to/wikitext/Main_Page/1' )
1216 .send( {
1217 html: htmlOfMainPageWithDataParsoid
1218 } )
1219 .expect( status200 )
1220 .expect( validWikitextResponse() )
1221 .end( done );
1222 } );
1223
1224 it( 'should allow a revision id in the original data', ( done ) => {
1225 client.req
1226 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1227 .send( {
1228 html: htmlOfMainPageWithDataParsoid,
1229 original: {
1230 revid: 1
1231 }
1232 } )
1233 .expect( status200 )
1234 .expect( validWikitextResponse() )
1235 .end( done );
1236 } );
1237
1238 it( 'should accept original wikitext as src', ( done ) => {
1239 client.req
1240 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1241 .send( {
1242 html: htmlOfMainPageWithDataParsoid,
1243 original: {
1244 wikitext: {
1245 headers: {
1246 'content-type': 'text/plain;profile="https://www.mediawiki.org/wiki/Specs/wikitext/1.0.0"'
1247 },
1248 body: '<strong>MediaWiki has been successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User\'s Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]\n'
1249 }
1250 }
1251 } )
1252 .expect( validWikitextResponse() )
1253 .end( done );
1254 } );
1255
1256 // Support for transforming from/to pagebundle is disabled in production.
1257 it.skip( 'should accept original html for selser (default)', ( done ) => {
1258 client.req
1259 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1260 .send( {
1261 html: htmlOfMainPageWithDataParsoid,
1262 original: {
1263 html: {
1264 headers: {
1265 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1266 },
1267 body: htmlOfMainPageOriginal
1268 },
1269 'data-parsoid': {
1270 headers: {
1271 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"'
1272 },
1273 body: dataParsoidOfMainPageOriginal
1274 }
1275 }
1276 } )
1277 .expect( validWikitextResponse() )
1278 .end( done );
1279 } );
1280
1281 // Support for transforming from/to pagebundle is disabled in production.
1282 it.skip( 'should accept original html for selser (1.1.1, meta)', ( done ) => {
1283 client.req
1284 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1285 .send( {
1286 html: htmlOfMainPageWithDataParsoid_1_1_1,
1287 original: {
1288 html: {
1289 headers: {
1290 'content-type': 'text/html; profile="mediawiki.org/specs/html/1.1.1"'
1291 },
1292 body: htmlOfMainPageOriginal
1293 },
1294 'data-parsoid': {
1295 headers: {
1296 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/0.0.1"'
1297 },
1298 body: dataParsoidOfMainPageOriginal
1299 }
1300 }
1301 } )
1302 .expect( validWikitextResponse() )
1303 .end( done );
1304 } );
1305
1306 // Support for transforming from/to pagebundle is disabled in production.
1307 it.skip( 'should accept original html for selser (1.1.1, headers)', ( done ) => {
1308 client.req
1309 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1310 .send( {
1311 // Don't set the mw:html:version so that we get it from the original/headers
1312 html: htmlOfMainPageWithDataParsoid,
1313 original: {
1314 html: {
1315 headers: {
1316 'content-type': 'text/html; profile="mediawiki.org/specs/html/1.1.1"'
1317 },
1318 body: htmlOfMainPageOriginal
1319 },
1320 'data-parsoid': {
1321 headers: {
1322 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/0.0.1"'
1323 },
1324 body: dataParsoidOfMainPageOriginal
1325 }
1326 }
1327 } )
1328 .expect( validWikitextResponse() )
1329 .end( done );
1330 } );
1331
1332 // Support for transforming from/to pagebundle is disabled in production.
1333 it.skip( 'should return http 400 if supplied data-parsoid is empty', ( done ) => {
1334 client.req
1335 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1336 .send( {
1337 html: '<html><head></head><body><p>hi</p></body></html>',
1338 original: {
1339 html: {
1340 headers: {
1341 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1342 },
1343 body: '<html><head></head><body><p>ho</p></body></html>'
1344 },
1345 'data-parsoid': {
1346 headers: {
1347 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"'
1348 },
1349 body: {}
1350 }
1351 }
1352 } )
1353 .expect( 400 )
1354 .end( done );
1355 } );
1356
1357 // FIXME: This test never passed. Pagebundle validation in general is needed
1358 // Support for transforming from/to pagebundle is disabled in production.
1359 it.skip( 'should return http 400 if supplied data-parsoid is a string', ( done ) => {
1360 client.req
1361 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1362 .send( {
1363 html: '<html><head></head><body><p>hi</p></body></html>',
1364 original: {
1365 html: {
1366 headers: {
1367 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1368 },
1369 body: '<html><head></head><body><p>ho</p></body></html>'
1370 },
1371 'data-parsoid': {
1372 headers: {
1373 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"'
1374 },
1375 body: 'Garbled text from RESTBase.'
1376 }
1377 }
1378 } )
1379 .expect( 400 )
1380 .end( done );
1381 } );
1382
1383 // The following three tests should all serialize as:
1384 // "<div>Selser test"
1385 // However, we're deliberately setting the original wikitext in
1386 // the first two to garbage so that when selser doesn't detect any
1387 // difference between the new and old html, it'll just reuse that
1388 // string and we have a reliable way of determining that selser
1389 // was used.
1390
1391 // Support for transforming from/to pagebundle is disabled in production.
1392 it.skip( 'should use selser with supplied wikitext', ( done ) => {
1393 // Create Junk Page
1394 // New and old html are identical, which should produce no diffs
1395 // and reuse the original wikitext.
1396 client.req
1397 // Need to provide an oldid so that selser mode is enabled
1398 // Without an oldid, serialization falls back to non-selser wts.
1399 // The oldid is used to fetch wikitext, but if wikitext is provided
1400 // (as in this test), it is not used. So, for testing purposes,
1401 // we can use any old random id, as long as something is present.
1402 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1403 .send( {
1404 html: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1405 original: {
1406 title: 'Junk Page',
1407 revid: 1234,
1408 wikitext: {
1409 body: '1. This is just some junk. See the comment above.'
1410 },
1411 html: {
1412 body: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1413 headers: {
1414 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1415 }
1416 },
1417 'data-parsoid': {
1418 body: {
1419 ids: {
1420 mwAA: {},
1421 mwBB: { autoInsertedEnd: true, stx: 'html' }
1422 }
1423 }
1424 }
1425 }
1426 } )
1427 .expect( validWikitextResponse(
1428 '1. This is just some junk. See the comment above.'
1429 ) )
1430 .end( done );
1431 } );
1432
1433 // Support for transforming from/to pagebundle is disabled in production.
1434 it.skip( 'should use selser with wikitext fetched from the mw api', ( done ) => {
1435 // Creat Junk Page
1436 // New and old html are identical, which should produce no diffs
1437 // and reuse the original wikitext.
1438 client.req
1439 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1440 .send( {
1441 html: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1442 original: {
1443 revid: 2,
1444 title: 'Junk Page',
1445 html: {
1446 body: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1447 headers: {
1448 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1449 }
1450 },
1451 'data-parsoid': {
1452 body: {
1453 ids: {
1454 mwAA: {},
1455 mwBB: { autoInsertedEnd: true, stx: 'html' }
1456 }
1457 }
1458 }
1459 }
1460 } )
1461 .expect( validWikitextResponse(
1462 '2. This is just some junk. See the comment above.'
1463 ) )
1464 .end( done );
1465 } );
1466
1467 // Support for transforming from/to pagebundle is disabled in production.
1468 it.skip( 'should fallback to non-selective serialization', ( done ) => {
1469 // Without the original wikitext and an unavailable
1470 // TemplateFetch for the source (no revision id provided),
1471 // it should fallback to non-selective serialization.
1472 client.req
1473 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1474 .send( {
1475 html: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1476 original: {
1477 title: 'Junk Page',
1478 html: {
1479 body: '<html><body id="mwAA"><div id="mwBB">Selser test</div></body></html>',
1480 headers: {
1481 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1482 }
1483 },
1484 'data-parsoid': {
1485 body: {
1486 ids: {
1487 mwAA: {},
1488 mwBB: { autoInsertedEnd: true, stx: 'html' }
1489 }
1490 }
1491 }
1492 }
1493 } )
1494 .expect( validWikitextResponse(
1495 '<div>Selser test'
1496 ) )
1497 .end( done );
1498 } );
1499
1500 // Support for transforming from/to pagebundle is disabled in production.
1501 it.skip( 'should apply data-parsoid to duplicated ids', ( done ) => {
1502 client.req
1503 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1504 .send( {
1505 html: '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div><div id="mwBB">data-parsoid test</div></body></html>',
1506 original: {
1507 title: 'Doesnotexist',
1508 html: {
1509 body: '<html><body id="mwAA"><div id="mwBB">data-parsoid test</div></body></html>',
1510 headers: {
1511 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1512 }
1513 },
1514 'data-parsoid': {
1515 body: {
1516 ids: {
1517 mwAA: {},
1518 mwBB: { autoInsertedEnd: true, stx: 'html' }
1519 }
1520 }
1521 }
1522 }
1523 } )
1524 .expect( validWikitextResponse(
1525 '<div>data-parsoid test<div>data-parsoid test'
1526 ) )
1527 .end( done );
1528 } );
1529
1530 // Support for transforming from/to pagebundle is disabled in production.
1531 it.skip( 'should return a 400 for missing inline data-mw (2.x)', ( done ) => {
1532 client.req
1533 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1534 .send( {
1535 html: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>',
1536 original: {
1537 title: 'Doesnotexist',
1538 'data-parsoid': {
1539 body: {
1540 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1541 }
1542 },
1543 html: {
1544 headers: {
1545 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2.4.0"'
1546 },
1547 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1548 }
1549 }
1550 } )
1551 .expect( 400 )
1552 .end( done );
1553 } );
1554
1555 // Support for transforming from/to pagebundle is disabled in production.
1556 it.skip( 'should return a 400 for not supplying data-mw', ( done ) => {
1557 client.req
1558 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1559 .send( {
1560 html: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>',
1561 original: {
1562 title: 'Doesnotexist',
1563 'data-parsoid': {
1564 body: {
1565 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1566 }
1567 },
1568 html: {
1569 headers: {
1570 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1571 },
1572 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1573 }
1574 }
1575 } )
1576 .expect( 400 )
1577 .end( done );
1578 } );
1579
1580 // Support for transforming from/to pagebundle is disabled in production.
1581 it.skip( 'should apply original data-mw', ( done ) => {
1582 client.req
1583 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1584 .send( {
1585 html: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>',
1586 original: {
1587 title: 'Doesnotexist',
1588 'data-parsoid': {
1589 body: {
1590 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1591 }
1592 },
1593 'data-mw': {
1594 body: {
1595 ids: {
1596 mwAQ: {
1597 parts: [ {
1598 template: {
1599 target: { wt: '1x', href: './Template:1x' },
1600 params: { 1: { wt: 'hi' } },
1601 i: 0
1602 }
1603 } ]
1604 }
1605 }
1606 }
1607 },
1608 html: {
1609 headers: {
1610 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1611 },
1612 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1613 }
1614 }
1615 } )
1616 .expect( validWikitextResponse( '{{1x|hi}}' ) )
1617 .end( done );
1618 } );
1619
1620 // Sanity check data-mw was applied in the previous test
1621 // Support for transforming from/to pagebundle is disabled in production.
1622 it.skip( 'should return a 400 for missing modified data-mw', ( done ) => {
1623 client.req
1624 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1625 .send( {
1626 html: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>',
1627 original: {
1628 title: 'Doesnotexist',
1629 'data-parsoid': {
1630 body: {
1631 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1632 }
1633 },
1634 'data-mw': {
1635 body: {
1636 ids: { mwAQ: {} } // Missing data-mw.parts!
1637 }
1638 },
1639 html: {
1640 headers: {
1641 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1642 },
1643 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1644 }
1645 }
1646 } )
1647 .expect( 400 )
1648 .end( done );
1649 } );
1650
1651 // Support for transforming from/to pagebundle is disabled in production.
1652 it.skip( 'should give precedence to inline data-mw over original', ( done ) => {
1653 client.req
1654 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1655 .send( {
1656 html: '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":{"target":{"wt":"1x","href":"./Template:1x"},"params":{"1":{"wt":"hi"}},"i":0}}]}\' id="mwAQ">hi</p>',
1657 original: {
1658 title: 'Doesnotexist',
1659 'data-parsoid': {
1660 body: {
1661 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1662 }
1663 },
1664 'data-mw': {
1665 body: {
1666 ids: { mwAQ: {} } // Missing data-mw.parts!
1667 }
1668 },
1669 html: {
1670 headers: {
1671 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1672 },
1673 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1674 }
1675 }
1676 } )
1677 .expect( validWikitextResponse( '{{1x|hi}}' ) )
1678 .end( done );
1679 } );
1680
1681 // Support for transforming from/to pagebundle is disabled in production.
1682 it.skip( 'should not apply original data-mw if modified is supplied', ( done ) => {
1683 client.req
1684 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1685 .send( {
1686 html: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">hi</p>',
1687 'data-mw': {
1688 body: {
1689 ids: {
1690 mwAQ: {
1691 parts: [ {
1692 template: {
1693 target: { wt: '1x', href: './Template:1x' },
1694 params: { 1: { wt: 'hi' } },
1695 i: 0
1696 }
1697 } ]
1698 }
1699 }
1700 }
1701 },
1702 original: {
1703 title: 'Doesnotexist',
1704 'data-parsoid': {
1705 body: {
1706 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
1707 }
1708 },
1709 'data-mw': {
1710 body: {
1711 ids: { mwAQ: {} } // Missing data-mw.parts!
1712 }
1713 },
1714 html: {
1715 headers: {
1716 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1717 },
1718 body: '<p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p>'
1719 }
1720 }
1721 } )
1722 .expect( validWikitextResponse( '{{1x|hi}}' ) )
1723 .end( done );
1724 } );
1725
1726 // The next three tests, although redundant with the above precedence
1727 // tests, are an attempt to show clients the semantics of separate
1728 // data-mw in the API. The main idea is,
1729 //
1730 // non-inline-data-mw = modified || original
1731 // inline-data-mw > non-inline-data-mw
1732
1733 // Support for transforming from/to pagebundle is disabled in production.
1734 it.skip( 'should apply original data-mw when modified is absent (captions 1)', ( done ) => {
1735 client.req
1736 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1737 .send( {
1738 html: htmlOfImage,
1739 original: {
1740 title: 'Doesnotexist',
1741 'data-parsoid': {
1742 body: {
1743 ids: {
1744 mwAg: { optList: [ { ck: 'caption', ak: 'Testing 123' } ] },
1745 mwAw: { a: { href: './File:Foobar.jpg' }, sa: {} },
1746 mwBA: {
1747 a: { resource: './File:Foobar.jpg', height: '28', width: '240' },
1748 sa: { resource: 'File:Foobar.jpg' }
1749 }
1750 }
1751 }
1752 },
1753 'data-mw': {
1754 body: {
1755 ids: {
1756 mwAg: { caption: 'Testing 123' }
1757 }
1758 }
1759 },
1760 html: {
1761 headers: {
1762 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1763 },
1764 body: htmlOfImage
1765 }
1766 }
1767 } )
1768 .expect( validWikitextResponse( '[[File:Foobar.jpg|Testing 123]]' ) )
1769 .end( done );
1770 } );
1771
1772 // Support for transforming from/to pagebundle is disabled in production.
1773 it.skip( 'should give precedence to inline data-mw over modified (captions 2)', ( done ) => {
1774 client.req
1775 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1776 .send( {
1777 html: htmlOfImageWithDataMW,
1778 'data-mw': {
1779 body: {
1780 ids: {
1781 mwAg: { caption: 'Testing 123' }
1782 }
1783 }
1784 },
1785 original: {
1786 title: 'Doesnotexist',
1787 'data-parsoid': {
1788 body: {
1789 ids: {
1790 mwAg: { optList: [ { ck: 'caption', ak: 'Testing 123' } ] },
1791 mwAw: { a: { href: './File:Foobar.jpg' }, sa: {} },
1792 mwBA: {
1793 a: { resource: './File:Foobar.jpg', height: '28', width: '240' },
1794 sa: { resource: 'File:Foobar.jpg' }
1795 }
1796 }
1797 }
1798 },
1799 'data-mw': {
1800 body: {
1801 ids: {
1802 mwAg: { caption: 'Testing 123' }
1803 }
1804 }
1805 },
1806 html: {
1807 headers: {
1808 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1809 },
1810 body: htmlOfImage
1811 }
1812 }
1813 } )
1814 .expect( validWikitextResponse( '[[File:Foobar.jpg]]' ) )
1815 .end( done );
1816 } );
1817
1818 // Support for transforming from/to pagebundle is disabled in production.
1819 it.skip( 'should give precedence to modified data-mw over original (captions 3)', ( done ) => {
1820 client.req
1821 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1822 .send( {
1823 html: htmlOfImage,
1824 'data-mw': {
1825 body: {
1826 ids: {
1827 mwAg: {}
1828 }
1829 }
1830 },
1831 original: {
1832 title: 'Doesnotexist',
1833 'data-parsoid': {
1834 body: {
1835 ids: {
1836 mwAg: { optList: [ { ck: 'caption', ak: 'Testing 123' } ] },
1837 mwAw: { a: { href: './File:Foobar.jpg' }, sa: {} },
1838 mwBA: {
1839 a: { resource: './File:Foobar.jpg', height: '28', width: '240' },
1840 sa: { resource: 'File:Foobar.jpg' }
1841 }
1842 }
1843 }
1844 },
1845 'data-mw': {
1846 body: {
1847 ids: {
1848 mwAg: { caption: 'Testing 123' }
1849 }
1850 }
1851 },
1852 html: {
1853 headers: {
1854 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
1855 },
1856 body: htmlOfImage
1857 }
1858 }
1859 } )
1860 .expect( validWikitextResponse( '[[File:Foobar.jpg]]' ) )
1861 .end( done );
1862 } );
1863
1864 it( 'should apply extra normalizations', ( done ) => {
1865 client.req
1866 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1867 .send( {
1868 html: '<h2></h2>',
1869 original: { title: 'Doesnotexist' }
1870 } )
1871 .expect( validWikitextResponse(
1872 ''
1873 ) )
1874 .end( done );
1875 } );
1876
1877 it( 'should return a request too large error when just over limit', ( done ) => {
1878 client.req
1879 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1880 .send( {
1881 // One over limit.
1882 // Use multi-byte characters, since the limit is in characters.
1883 html: 'ä'.repeat( parsoidOptions.limits.html2wt.maxHTMLSize + 1 )
1884 } )
1885 .expect( 413 )
1886 .end( done );
1887 } );
1888
1889 it( 'should not return a request too large error when just under limit', ( done ) => {
1890 client.req
1891 .post( endpointPrefix + '/transform/html/to/wikitext/' )
1892 .send( {
1893 // One under limit.
1894 // Use multi-byte characters, since the limit is in characters.
1895 html: 'ä'.repeat( parsoidOptions.limits.html2wt.maxHTMLSize - 1 )
1896 } )
1897 .expect( 200 )
1898 .end( done );
1899 } );
1900
1901 // Support for transforming from/to pagebundle is disabled in production.
1902 it.skip( 'should fail to downgrade the original version for an unknown transition', ( done ) => {
1903 const htmlOfMinimal = getTextFromFile( 'Minimal.html' );
1904 const htmlOfMinimal2222 = getTextFromFile( 'Minimal-2222.html' );
1905 client.req
1906 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
1907 .send( {
1908 html: htmlOfMinimal,
1909 original: {
1910 title: 'Doesnotexist',
1911 'data-parsoid': { body: { ids: {} } },
1912 html: {
1913 headers: {
1914 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/2222.0.0"'
1915 },
1916 body: htmlOfMinimal2222
1917 }
1918 }
1919 } )
1920 .expect( 400 )
1921 .end( done );
1922 } );
1923
1924 } ); // end html2wt
1925
1926 // Support for transforming from/to pagebundle is disabled in production.
1927 describe.skip( 'pb2pb', () => {
1928
1929 it( 'should require an original or previous version', ( done ) => {
1930 client.req
1931 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Reuse_Page/100' )
1932 .send( {} )
1933 .expect( 400 )
1934 .end( done );
1935 } );
1936
1937 const previousRevHTML = {
1938 revid: 99,
1939 html: {
1940 headers: {
1941 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
1942 },
1943 body: '<p about="#mwt1" typeof="mw:Transclusion" data-mw=\'{"parts":[{"template":{"target":{"wt":"colours of the rainbow","href":"./Template:Colours_of_the_rainbow"},"params":{},"i":0}}]}\' id="mwAg">pink</p>'
1944 },
1945 'data-parsoid': {
1946 headers: {
1947 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"'
1948 },
1949 body: {
1950 counter: 2,
1951 ids: {
1952 mwAg: { pi: [ [] ], src: '{{colours of the rainbow}}' } // artificially added src
1953 }
1954 }
1955 }
1956 };
1957
1958 it( 'should error when revision not found (transform, pb2pb)', ( done ) => {
1959 client.req
1960 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Doesnotexist' )
1961 .send( {
1962 previous: previousRevHTML
1963 } )
1964 .expect( 404 )
1965 .end( done );
1966 } );
1967
1968 // FIXME: Expansion reuse wasn't ported, see T98995
1969 it.skip( 'should accept the previous revision to reuse expansions', ( done ) => {
1970 client.req
1971 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Reuse_Page/100' )
1972 .send( {
1973 previous: previousRevHTML
1974 } )
1975 .expect( validPageBundleResponse( ( doc ) => {
1976 doc.body.firstChild.textContent.should.match( /pink/ );
1977 } ) )
1978 .end( done );
1979 } );
1980
1981 const origHTML = JSON.parse( JSON.stringify( previousRevHTML ) );
1982 origHTML.revid = 100;
1983
1984 // FIXME: Expansion reuse wasn't ported, see T98995
1985 it.skip( 'should accept the original and reuse certain expansions', ( done ) => {
1986 client.req
1987 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Reuse_Page/100' )
1988 .send( {
1989 updates: {
1990 transclusions: true
1991 },
1992 original: origHTML
1993 } )
1994 .expect( validPageBundleResponse( ( doc ) => {
1995 doc.body.firstChild.textContent.should.match( /purple/ );
1996 } ) )
1997 .end( done );
1998 } );
1999
2000 it( 'should refuse an unknown conversion (2.x -> 999.x)', ( done ) => {
2001 client.req
2002 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Reuse_Page/100' )
2003 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/999.0.0"' )
2004 .send( {
2005 previous: previousRevHTML
2006 } )
2007 .expect( 415 )
2008 .end( done );
2009 } );
2010
2011 it( 'should downgrade 999.x content to 2.x', ( done ) => {
2012 const contentVersion = '2.4.0';
2013 client.req
2014 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2015 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/pagebundle/' + contentVersion + '"' )
2016 .send( {
2017 original: {
2018 title: 'Doesnotexist',
2019 'data-parsoid': {
2020 body: {
2021 ids: { mwAQ: { pi: [ [ { k: '1' } ] ] } }
2022 }
2023 },
2024 'data-mw': {
2025 body: {
2026 ids: {
2027 mwAQ: {
2028 parts: [ {
2029 template: {
2030 target: { wt: '1x', href: './Template:1x' },
2031 params: { 1: { wt: 'hi' } },
2032 i: 0
2033 }
2034 } ]
2035 }
2036 }
2037 }
2038 },
2039 html: {
2040 headers: {
2041 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/999.0.0"'
2042 },
2043 body: '<!DOCTYPE html>\n<html><head><meta charset="utf-8"/><meta property="mw:htmlVersion" content="999.0.0"/></head><body><p about="#mwt1" typeof="mw:Transclusion" id="mwAQ">ho</p></body></html>'
2044 }
2045 }
2046 } )
2047 .expect( status200 )
2048 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
2049 // In < 999.x, data-mw is still inline.
2050 html.should.match( /\s+data-mw\s*=\s*['"]/ );
2051 html.should.not.match( /\s+data-parsoid\s*=\s*['"]/ );
2052 const doc = domino.createDocument( html );
2053 const meta = doc.querySelector( 'meta[property="mw:html:version"], meta[property="mw:htmlVersion"]' );
2054 meta.getAttribute( 'content' ).should.satisfy(
2055 ( version ) => semver.satisfies( version, `^${ contentVersion }` )
2056 );
2057 } ) )
2058 .end( done );
2059 } );
2060
2061 it( 'should accept the original and update the redlinks', function ( done ) {
2062 if ( skipForNow ) {
2063 return this.skip();
2064 } // Create pages to fix redlinks count
2065 // NOTE: Keep this on an older version to show that it's preserved
2066 // through the transformation.
2067 const contentVersion = '2.0.0';
2068 client.req
2069 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2070 .send( {
2071 updates: {
2072 redlinks: true
2073 },
2074 original: {
2075 title: 'Doesnotexist',
2076 'data-parsoid': {
2077 body: {
2078 ids: {}
2079 }
2080 },
2081 html: {
2082 headers: {
2083 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + contentVersion + '"'
2084 },
2085 body: '<p><a rel="mw:WikiLink" href="./Special:Version" title="Special:Version">Special:Version</a> <a rel="mw:WikiLink" href="./Doesnotexist" title="Doesnotexist">Doesnotexist</a> <a rel="mw:WikiLink" href="./Redirected" title="Redirected">Redirected</a></p>'
2086 }
2087 }
2088 } )
2089 .expect( acceptablePageBundleResponse( contentVersion, ( html ) => {
2090 const doc = domino.createDocument( html );
2091 doc.body.querySelectorAll( 'a' ).length.should.equal( 3 );
2092 const redLinks = doc.body.querySelectorAll( '.new' );
2093 redLinks.length.should.equal( 1 );
2094 redLinks[ 0 ].getAttribute( 'title' ).should.equal( 'Doesnotexist' );
2095 const redirects = doc.body.querySelectorAll( '.mw-redirect' );
2096 redirects.length.should.equal( 1 );
2097 redirects[ 0 ].getAttribute( 'title' ).should.equal( 'Redirected' );
2098 } ) )
2099 .end( done );
2100 } );
2101
2102 ( skipForNow ? describe.skip : describe )( 'Variant conversion', () => {
2103
2104 it( 'should refuse variant conversion on en page', ( done ) => {
2105 client.req
2106 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2107 .send( {
2108 updates: {
2109 variant: { target: 'sr-el' }
2110 },
2111 original: {
2112 revid: 1,
2113 html: {
2114 headers: {
2115 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
2116 },
2117 body: '<p>абвг abcd</p>'
2118 }
2119 }
2120 } )
2121 .expect( 400 )
2122 .end( done );
2123 } );
2124
2125 it( 'should accept the original and do variant conversion (given oldid)', ( done ) => {
2126 client.req
2127 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2128 .send( {
2129 updates: {
2130 variant: { target: 'sr-el' }
2131 },
2132 original: {
2133 revid: 104, /* sets the pagelanguage */
2134 html: {
2135 headers: {
2136 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
2137 },
2138 body: '<p>абвг abcd x</p>'
2139 }
2140 }
2141 } )
2142 .expect( status200 )
2143 .expect( ( res ) => {
2144 // We don't actually require the result to have data-parsoid
2145 // if the input didn't have data-parsoid; hack the result
2146 // in order to make validPageBundleResponse() pass.
2147 res.body[ 'data-parsoid' ].body = {};
2148 } )
2149 .expect( validPageBundleResponse( ( doc ) => {
2150 doc.body.textContent.should.equal( 'abvg abcd x' );
2151 } ) )
2152 .expect( ( res ) => {
2153 const headers = res.body.html.headers;
2154 headers.should.have.property( 'content-language' );
2155 headers[ 'content-language' ].should.equal( 'sr-el' );
2156 headers.should.have.property( 'vary' );
2157 headers.vary.should.match( /\bAccept-Language\b/i );
2158 } )
2159 .end( done );
2160 } );
2161
2162 it( 'should accept the original and do variant conversion (given pagelanguage)', ( done ) => {
2163 client.req
2164 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2165 .set( 'Content-Language', 'sr' )
2166 .set( 'Accept-Language', 'sr-el' )
2167 .send( {
2168 updates: {
2169 variant: { /* target implicit from accept-language */}
2170 },
2171 original: {
2172 html: {
2173 headers: {
2174 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
2175 },
2176 body: '<p>абвг abcd</p>'
2177 }
2178 }
2179 } )
2180 .expect( status200 )
2181 .expect( ( res ) => {
2182 // We don't actually require the result to have data-parsoid
2183 // if the input didn't have data-parsoid; hack the result
2184 // in order to make validPageBundleResponse() pass.
2185 res.body[ 'data-parsoid' ].body = {};
2186 } )
2187 .expect( validPageBundleResponse( ( doc ) => {
2188 doc.body.textContent.should.equal( 'abvg abcd' );
2189 } ) )
2190 .expect( ( res ) => {
2191 const headers = res.body.html.headers;
2192 headers.should.have.property( 'content-language' );
2193 headers[ 'content-language' ].should.equal( 'sr-el' );
2194 headers.should.have.property( 'vary' );
2195 headers.vary.should.match( /\bAccept-Language\b/i );
2196 } )
2197 .end( done );
2198 } );
2199
2200 it( 'should not perform variant conversion w/ invalid variant (given pagelanguage)', ( done ) => {
2201 client.req
2202 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/' )
2203 .set( 'Content-Language', 'sr' )
2204 .set( 'Accept-Language', 'sr-BOGUS' )
2205 .send( {
2206 updates: {
2207 variant: { /* target implicit from accept-language */}
2208 },
2209 original: {
2210 html: {
2211 headers: {
2212 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
2213 },
2214 body: '<p>абвг abcd</p>'
2215 }
2216 }
2217 } )
2218 .expect( status200 )
2219 .expect( ( res ) => {
2220 // We don't actually require the result to have data-parsoid
2221 // if the input didn't have data-parsoid; hack the result
2222 // in order to make validPageBundleResponse() pass.
2223 res.body[ 'data-parsoid' ].body = {};
2224 } )
2225 .expect( validPageBundleResponse( ( doc ) => {
2226 doc.body.textContent.should.equal( 'абвг abcd' );
2227 } ) )
2228 .expect( ( res ) => {
2229 const headers = res.body.html.headers;
2230 headers.should.have.property( 'content-language' );
2231 headers[ 'content-language' ].should.equal( 'sr' );
2232 headers.should.have.property( 'vary' );
2233 headers.vary.should.match( /\bAccept-Language\b/i );
2234 } )
2235 .end( done );
2236 } );
2237
2238 } );
2239
2240 } ); // end pb2pb
2241
2242 // Since we're disabling the pagebundle transform, let's make sure that trying to
2243 // send a request to the endpoint returns a 404 error.
2244 describe( 'Pagebundle transform (from/format) disabled, should return 404 as response code', () => {
2245 it( '/transform/pagebundle/to/pagebundle/Reuse_Page/100', ( done ) => {
2246 client.req
2247 .post( endpointPrefix + '/transform/pagebundle/to/pagebundle/Reuse_Page/100' )
2248 .send( {} )
2249 .expect( 404 )
2250 .end( done );
2251 } );
2252
2253 it( '/transform/pagebundle/to/wikitext/', ( done ) => {
2254 client.req
2255 .post( endpointPrefix + '/transform/pagebundle/to/wikitext/' )
2256 .send( {
2257 html: '<!DOCTYPE html>\n<html prefix="dc: http://purl.org/dc/terms/ mw: http://mediawiki.org/rdf/" about="http://localhost/index.php/Special:Redirect/revision/1"><head prefix="mwr: http://localhost/index.php/Special:Redirect/"><meta property="mw:articleNamespace" content="0"/><link rel="dc:replaces" resource="mwr:revision/0"/><meta property="dc:modified" content="2014-09-12T22:46:59.000Z"/><meta about="mwr:user/0" property="dc:title" content="MediaWiki default"/><link rel="dc:contributor" resource="mwr:user/0"/><meta property="mw:revisionSHA1" content="8e0aa2f2a7829587801db67d0424d9b447e09867"/><meta property="dc:description" content=""/><link rel="dc:isVersionOf" href="http://localhost/index.php/Main_Page"/><title>Main_Page</title><base href="http://localhost/index.php/"/><link rel="stylesheet" href="//localhost/load.php?modules=mediawiki.legacy.commonPrint,shared|mediawiki.skinning.elements|mediawiki.skinning.content|mediawiki.skinning.interface|skins.vector.styles|site|mediawiki.skinning.content.parsoid&amp;only=styles&amp;debug=true&amp;skin=vector"/></head><body data-parsoid=\'{"dsr":[0,592,0,0]}\' lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body mw-body-content mediawiki" dir="ltr"><p data-parsoid=\'{"dsr":[0,59,0,0]}\'><strong data-parsoid=\'{"stx":"html","dsr":[0,59,8,9]}\'>MediaWiki has been successfully installed.</strong></p>\n\n<p data-parsoid=\'{"dsr":[61,171,0,0]}\'>Consult the <a rel="mw:ExtLink" href="//meta.wikimedia.org/wiki/Help:Contents" data-parsoid=\'{"dsr":[73,127,41,1]}\'>User\'s Guide</a> for information on using the wiki software.</p>\n\n<h2 data-parsoid=\'{"dsr":[173,194,2,2]}\'> Getting started </h2>\n<ul data-parsoid=\'{"dsr":[195,592,0,0]}\'><li data-parsoid=\'{"dsr":[195,300,1,0]}\'> <a rel="mw:ExtLink" href="//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings" data-parsoid=\'{"dsr":[197,300,75,1]}\'>Configuration settings list</a></li>\n<li data-parsoid=\'{"dsr":[301,373,1,0]}\'> <a rel="mw:ExtLink" href="//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ" data-parsoid=\'{"dsr":[303,373,56,1]}\'>MediaWiki FAQ</a></li>\n<li data-parsoid=\'{"dsr":[374,472,1,0]}\'> <a rel="mw:ExtLink" href="https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce" data-parsoid=\'{"dsr":[376,472,65,1]}\'>MediaWiki release mailing list</a></li>\n<li data-parsoid=\'{"dsr":[473,592,1,0]}\'> <a rel="mw:ExtLink" href="//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources" data-parsoid=\'{"dsr":[475,592,80,1]}\'>Localise MediaWiki for your language</a></li></ul></body></html>',
2258 original: {
2259 html: {
2260 headers: {
2261 'content-type': 'text/html;profile="https://www.mediawiki.org/wiki/Specs/HTML/' + defaultContentVersion + '"'
2262 },
2263 body: "<!DOCTYPE html>\n<html prefix=\"dc: http://purl.org/dc/terms/ mw: http://mediawiki.org/rdf/\" about=\"http://localhost/index.php/Special:Redirect/revision/1\"><head prefix=\"mwr: http://localhost/index.php/Special:Redirect/\"><meta property=\"mw:articleNamespace\" content=\"0\"/><link rel=\"dc:replaces\" resource=\"mwr:revision/0\"/><meta property=\"dc:modified\" content=\"2014-09-12T22:46:59.000Z\"/><meta about=\"mwr:user/0\" property=\"dc:title\" content=\"MediaWiki default\"/><link rel=\"dc:contributor\" resource=\"mwr:user/0\"/><meta property=\"mw:revisionSHA1\" content=\"8e0aa2f2a7829587801db67d0424d9b447e09867\"/><meta property=\"dc:description\" content=\"\"/><link rel=\"dc:isVersionOf\" href=\"http://localhost/index.php/Main_Page\"/><title>Main_Page</title><base href=\"http://localhost/index.php/\"/><link rel=\"stylesheet\" href=\"//localhost/load.php?modules=mediawiki.legacy.commonPrint,shared|mediawiki.skinning.elements|mediawiki.skinning.content|mediawiki.skinning.interface|skins.vector.styles|site|mediawiki.skinning.content.parsoid&amp;only=styles&amp;debug=true&amp;skin=vector\"/></head><body id=\"mwAA\" lang=\"en\" class=\"mw-content-ltr sitedir-ltr ltr mw-body mw-body-content mediawiki\" dir=\"ltr\"><p id=\"mwAQ\"><strong id=\"mwAg\">MediaWiki has been successfully installed.</strong></p>\n\n<p id=\"mwAw\">Consult the <a rel=\"mw:ExtLink\" href=\"//meta.wikimedia.org/wiki/Help:Contents\" id=\"mwBA\">User's Guide</a> for information on using the wiki software.</p>\n\n<h2 id=\"mwBQ\"> Getting started </h2>\n<ul id=\"mwBg\"><li id=\"mwBw\"> <a rel=\"mw:ExtLink\" href=\"//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings\" id=\"mwCA\">Configuration settings list</a></li>\n<li id=\"mwCQ\"> <a rel=\"mw:ExtLink\" href=\"//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ\" id=\"mwCg\">MediaWiki FAQ</a></li>\n<li id=\"mwCw\"> <a rel=\"mw:ExtLink\" href=\"https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce\" id=\"mwDA\">MediaWiki release mailing list</a></li>\n<li id=\"mwDQ\"> <a rel=\"mw:ExtLink\" href=\"//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources\" id=\"mwDg\">Localise MediaWiki for your language</a></li></ul></body></html>"
2264 },
2265 'data-parsoid': {
2266 headers: {
2267 'content-type': 'application/json;profile="https://www.mediawiki.org/wiki/Specs/data-parsoid/' + defaultContentVersion + '"'
2268 },
2269 body: {
2270 counter: 14,
2271 ids: {
2272 mwAA: { dsr: [ 0, 592, 0, 0 ] },
2273 mwAQ: { dsr: [ 0, 59, 0, 0 ] },
2274 mwAg: { stx: 'html', dsr: [ 0, 59, 8, 9 ] },
2275 mwAw: { dsr: [ 61, 171, 0, 0 ] },
2276 mwBA: { dsr: [ 73, 127, 41, 1 ] },
2277 mwBQ: { dsr: [ 173, 194, 2, 2 ] },
2278 mwBg: { dsr: [ 195, 592, 0, 0 ] },
2279 mwBw: { dsr: [ 195, 300, 1, 0 ] },
2280 mwCA: { dsr: [ 197, 300, 75, 1 ] },
2281 mwCQ: { dsr: [ 301, 373, 1, 0 ] },
2282 mwCg: { dsr: [ 303, 373, 56, 1 ] },
2283 mwCw: { dsr: [ 374, 472, 1, 0 ] },
2284 mwDA: { dsr: [ 376, 472, 65, 1 ] },
2285 mwDQ: { dsr: [ 473, 592, 1, 0 ] },
2286 mwDg: { dsr: [ 475, 592, 80, 1 ] }
2287 }
2288 }
2289 }
2290 }
2291 } )
2292 .expect( 404 )
2293 .end( done );
2294 } );
2295
2296 it( '/transform/wikitext/to/pagebundle/', ( done ) => {
2297 client.req
2298 .post( endpointPrefix + '/transform/wikitext/to/pagebundle/' )
2299 .set( 'Accept', 'application/json; profile="https://www.mediawiki.org/wiki/Specs/HTML/0.0.0"' )
2300 .send( { wikitext: '== h2 ==' } )
2301 .expect( 404 )
2302 .expect( ( res ) => {
2303 JSON.parse( res.error.text ).errorKey.should.equal(
2304 'rest-invalid-transform'
2305 );
2306 } )
2307 .end( done );
2308 } );
2309 } );
2310
2311 describe( 'ETags', () => {
2312 it( '/transform/ should use ETag from If-Match header', async () => {
2313 const { statusCode: status1, headers: headers1, text: text1 } = await client.req
2314 .get( `rest.php/v1/revision/${ revid }/html` )
2315 .query( { stash: 'yes' } );
2316
2317 assert.deepEqual( status1, 200, text1 );
2318 assert.ok( headers1.etag, 'ETag header' );
2319
2320 // The request above should have stashed a rendering associated with the ETag it
2321 // returned. Pass the ETag in the If-Match header.
2322 const { statusCode: status2, text: text2 } = await client.req
2323 .post( endpointPrefix + `/transform/html/to/wikitext/${ page }/${ revid }` )
2324 .set( 'If-Match', headers1.etag )
2325 .send( {
2326 html: text1
2327 } );
2328
2329 assert.deepEqual( status2, 200, text2 );
2330
2331 // pageContent is brittle against round trip conversion, we only get it back correctly
2332 // because the HTML is unmodified, and selser kicks in.
2333 assert.deepEqual( text2, pageContent );
2334 } );
2335
2336 it( '/transform/ should use ETag from body', async () => {
2337 const { statusCode: status1, headers: headers1, text: text1 } = await client.req
2338 .get( `rest.php/v1/revision/${ revid }/html` )
2339 .query( { stash: 'yes' } );
2340
2341 assert.deepEqual( status1, 200, text1 );
2342 assert.ok( headers1.etag, 'ETag header' );
2343
2344 // The request above should have stashed a rendering associated with the ETag it
2345 // returned. Submit it in the request body.
2346 // Don't put the revision ID into the path, to test that the one from the ETag is used.
2347 const { statusCode: status2, text: text2 } = await client.req
2348 .post( endpointPrefix + `/transform/html/to/wikitext/${ page }` )
2349 .send( {
2350 html: text1,
2351 original: { etag: headers1.etag }
2352 } );
2353
2354 assert.deepEqual( status2, 200, text2 );
2355
2356 // pageContent is brittle against round trip conversion, we only get it back correctly
2357 // because the HTML is unmodified, and selser kicks in.
2358 assert.deepEqual( text2, pageContent );
2359 } );
2360
2361 it( '/transform/ should refuse non-matching ETags in header', async () => {
2362 const { status, text } = await client.req
2363 .post( endpointPrefix + `/transform/html/to/wikitext/${ page }/${ revid }` )
2364 .set( 'If-Match', '"1219844647/deadbeef"' )
2365 .send( {
2366 html: '<p>hello</p>'
2367 } );
2368
2369 assert.deepEqual( status, 412, text );
2370 } );
2371
2372 it( '/transform/ should refuse non-matching ETags in the body', async () => {
2373 const { status, text } = await client.req
2374 .post( endpointPrefix + `/transform/html/to/wikitext/${ page }` )
2375 .send( {
2376 html: '<p>hello</p>',
2377 original: { etag: '"1219844647/deadbeef"' }
2378 } );
2379
2380 assert.deepEqual( status, 412, text );
2381 } );
2382 } );
2383
2384 describe( 'stashing with If-Match header', () => {
2385
2386 // TODO: The /transform/html endpoint should handle the If-Match header
2387 // by checking whether it has a rendering with the correct key
2388 // stashed or cached. If so, it should be used for selser.
2389 it.skip( 'should trigger on If-Match header', async () => {
2390 const pageResponse = await client.req
2391 .get( `rest.php/v1/page/${ pageEncoded }/html` )
2392 .query( { stash: 'yes' } );
2393
2394 pageResponse.headers.should.have.property( 'etag' );
2395 const eTag = pageResponse.headers.etag;
2396 const html = pageResponse.text;
2397
2398 const transformResponse = await client.req
2399 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2400 .set( 'If-Match', eTag )
2401 .send( {
2402 html
2403 } );
2404
2405 transformResponse.status.should.equal( 200, transformResponse.text );
2406
2407 // Since the HTML didn't change, we should get back the original wikitext unchanged.
2408 transformResponse.text.should.equal( pageContent );
2409 } );
2410
2411 it( 'should fail if eTag in If-Match header is unknown', async () => {
2412 // request page HTML, but do not set 'stash' parameter!
2413 const transformResponse = await client.req
2414 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2415 .set( 'If-Match', '"1219844647/dummy"' )
2416 .send( {
2417 html: '<p>test</p>'
2418 } );
2419
2420 transformResponse.status.should.equal( 412 );
2421 } );
2422 } );
2423
2424 describe( 'stashing with renderid in body', () => {
2425 it( 'should trigger on renderid field in the body', async () => {
2426 const pageResponse = await client.req
2427 .get( `rest.php/v1/page/${ pageEncoded }/html` )
2428 .query( { stash: 'yes' } );
2429
2430 pageResponse.headers.should.have.property( 'etag' );
2431 const eTag = pageResponse.headers.etag;
2432 const html = pageResponse.text;
2433
2434 const transformResponse = await client.req
2435 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2436 .send( {
2437 html,
2438 original: {
2439 renderid: eTag
2440 }
2441 } );
2442
2443 transformResponse.status.should.equal( 200, transformResponse.text );
2444
2445 // Since the HTML didn't change, we should get back the original wikitext unchanged.
2446 transformResponse.text.should.equal( pageContent );
2447 } );
2448
2449 it( 'should fail if stash key is unknown', async () => {
2450 // request page HTML, but do not set 'stash' parameter!
2451 const transformResponse = await client.req
2452 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2453 .send( {
2454 html: '<p>test</p>',
2455 original: {
2456 renderid: '"1219844647/dummy"'
2457 }
2458 } );
2459
2460 transformResponse.status.should.equal( 412 );
2461 } );
2462 } );
2463
2464 describe( 'selser using rendering based on revid', () => {
2465 it( 'should trigger on revid field in the body', async () => {
2466 const pageResponse = await client.req
2467 .get( `rest.php/v1/page/${ pageEncoded }/with_html` );
2468
2469 const transformResponse = await client.req
2470 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2471 .send( {
2472 html: pageResponse.body.html,
2473 original: {
2474 revid: pageResponse.body.latest.id
2475 }
2476 } );
2477
2478 transformResponse.status.should.equal( 200, transformResponse.text );
2479
2480 // Since the HTML didn't change, we should get back the original wikitext unchanged.
2481 transformResponse.text.should.equal( pageContent );
2482 } );
2483
2484 it( 'should fail if revid is unknown', async () => {
2485 // request page HTML, but do not set 'stash' parameter!
2486 const transformResponse = await client.req
2487 .post( endpointPrefix + '/transform/html/to/wikitext/' )
2488 .send( {
2489 html: '<p>test</p>',
2490 original: {
2491 revid: 45452232
2492 }
2493 } );
2494
2495 transformResponse.status.should.equal( 404 );
2496 } );
2497 } );
2498
2499 } );