-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
/* | ||
* Simple proxy to route requests to the client-requested | ||
* Parsoid variant (JS or PHP) during the transition | ||
* period. Parsoid/JS is being phased out and replaced by | ||
* Parsoid/PHP. | ||
*/ | ||
|
||
const HyperSwitch = require('hyperswitch'); | ||
const URI = HyperSwitch.URI; | ||
const mwUtil = require('../lib/mwUtil'); | ||
const VARIANT_HDR_NAME = 'x-parsoid-variant'; | ||
|
||
module.exports = () => { | ||
return { | ||
spec: { | ||
paths: { | ||
'/{+path}': { | ||
all: { | ||
operationId: 'proxy_parsoid_variant' | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
proxy_parsoid_variant: (hyper, req) => { | ||
const rootReqHeaders = hyper._rootReq.headers || {}; | ||
rootReqHeaders[VARIANT_HDR_NAME] = rootReqHeaders[VARIANT_HDR_NAME] || 'JS'; | ||
const isPhpVariant = /PHP/i.test(rootReqHeaders[VARIANT_HDR_NAME]); | ||
|
||
return hyper.request({ | ||
method: req.method, | ||
uri: new URI(req.uri.toString().replace('/parsoid/', | ||
isPhpVariant ? '/parsoidphp/' : '/parsoidjs/')), | ||
headers: req.headers, | ||
body: req.body, | ||
query: req.query | ||
}) | ||
.tap((res) => { | ||
res.headers = res.headers || {}; | ||
res.headers[VARIANT_HDR_NAME] = rootReqHeaders[VARIANT_HDR_NAME]; | ||
mwUtil.addVaryHeader(res, VARIANT_HDR_NAME); | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
const assert = require('../../utils/assert.js'); | ||
const Server = require('../../utils/server.js'); | ||
const preq = require('preq'); | ||
|
||
const testPage = { | ||
title: 'User:Pchelolo%2fRestbase_Test', | ||
revision: '275854', | ||
wikitext: '<div id=bar>Selser test' | ||
// html is fetched dynamically | ||
}; | ||
|
||
describe('php parsoid variant', function() { | ||
this.timeout(20000); | ||
let contentTypes; | ||
const server = new Server(); | ||
before(() => { | ||
return server.start() | ||
.then(() => { | ||
contentTypes = server.config.conf.test.content_types; | ||
return preq.get({ | ||
uri: `${server.config.bucketURL('en.wikipedia.beta.wmflabs.org')}/html/${testPage.title}/${testPage.revision}` | ||
}); | ||
}) | ||
.then((res) => { | ||
testPage.html = res.body; | ||
}); | ||
}); | ||
after(() => server.stop()); | ||
|
||
it('wt2html default variant', () => { | ||
return preq.post({ | ||
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`, | ||
body: { | ||
wikitext: '== Heading ==' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.contentType(res, contentTypes.html); | ||
assert.deepEqual(res.headers['x-parsoid-variant'], 'JS'); | ||
const pattern = /<h2.*>Heading<\/h2>/; | ||
if (!pattern.test(res.body)) { | ||
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`); | ||
} | ||
}); | ||
}); | ||
|
||
it('wt2html php variant', () => { | ||
return preq.post({ | ||
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`, | ||
body: { | ||
wikitext: '== Heading ==' | ||
}, | ||
headers: { | ||
'x-parsoid-variant': 'PHP' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.contentType(res, contentTypes.html); | ||
assert.deepEqual(res.headers['x-parsoid-variant'], 'PHP'); | ||
const pattern = /<h2.*>Heading<\/h2>/; | ||
if (!pattern.test(res.body)) { | ||
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`); | ||
} | ||
}); | ||
}); | ||
}); | ||
|