-
Notifications
You must be signed in to change notification settings - Fork 80
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
8 changed files
with
245 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
|
||
const HyperSwitch = require('hyperswitch'); | ||
const URI = HyperSwitch.URI; | ||
const mwUtil = require('./mwUtil'); | ||
|
||
module.exports = (hyper, req, next, options, specInfo) => { | ||
const rp = req.params; | ||
const acceptLanguage = req.headers && req.headers['accept-language']; | ||
if (!acceptLanguage || mwUtil.isNoCacheRequest(req)) { | ||
delete req.headers['accept-language']; | ||
return next(hyper, req) | ||
// TODO: Store the Vary header so that if the page is possible | ||
// to be converted we return a proper Vary even when accept-language | ||
// is not specified. | ||
// For now we hacky check by domain. | ||
.then((res) => { | ||
const langCode = req.params.domain.substring(0, req.params.domain.indexOf('.')); | ||
if (mwUtil.canConvertLangVariant(hyper, req, langCode)) { | ||
res.headers.vary = 'accept-language'; | ||
} | ||
return res; | ||
}); | ||
} | ||
|
||
const revTableURI = [rp.domain, 'sys', 'page_revisions', 'page', rp.title]; | ||
if (rp.revision) { | ||
revTableURI.push(`${rp.revision}`); | ||
} | ||
return hyper.get({ uri: new URI(revTableURI) }) | ||
.then((res) => { | ||
const revision = res.body.items[0]; | ||
return mwUtil.shouldConvertLangVariant(hyper, req, revision.page_language, acceptLanguage) | ||
.then((shouldConvert) => { | ||
if (!shouldConvert) { | ||
delete req.headers['accept-language']; | ||
return next(hyper, req) | ||
// TODO: Eventually we want to store the Vary header we get from Parsoid! | ||
.then((res) => { | ||
if (mwUtil.canConvertLangVariant(hyper, req, revision.page_language)) { | ||
res.headers.vary = 'accept-language'; | ||
} | ||
return res; | ||
}); | ||
} | ||
|
||
if (/\/page\/html\//.test(specInfo.path)) { | ||
// It's HTML, hit Parsoid for conversion | ||
return next(hyper, req) | ||
.then(html => hyper.post({ | ||
uri: new URI([rp.domain, 'sys', 'parsoid', 'transform', 'html', 'to', 'html']), | ||
headers: { | ||
'content-type': 'application/json', | ||
'content-language': revision.page_language | ||
}, | ||
body: { | ||
original: { | ||
html: { | ||
headers: html.headers, | ||
body: html.body.toString() | ||
} | ||
}, | ||
updates: { | ||
variant: { | ||
source: null, | ||
target: acceptLanguage | ||
} | ||
} | ||
} | ||
})) | ||
// TODO: Now we fix up the Vary header cause Parsoid | ||
// sets content-type in there which is wrong. | ||
.then((res) => { | ||
res.headers.vary = 'accept-language'; | ||
return res; | ||
}); | ||
} else { | ||
// TODO: It's something else, so just forward to MCS | ||
return next(hyper, req); | ||
} | ||
}); | ||
}); | ||
}; |
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,87 @@ | ||
'use strict'; | ||
|
||
// mocha defines to avoid JSHint breakage | ||
/* global describe, it, before, beforeEach, after, afterEach */ | ||
|
||
const assert = require('../../utils/assert.js'); | ||
const preq = require('preq'); | ||
const server = require('../../utils/server.js'); | ||
const variantsPageTitle = 'RESTBase_Testing_Page'; | ||
|
||
|
||
describe('Language variants', function() { | ||
|
||
this.timeout(20000); | ||
|
||
before(() => server.start()); | ||
|
||
it('should request html with no variants', () => { | ||
return preq.get({ uri: `${server.config.variantsWikiBucketURL}/html/${variantsPageTitle}`}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.deepEqual(res.headers.vary.toLowerCase(), 'accept-language'); | ||
assert.deepEqual(/1\. Ово је тестна страница/.test(res.body), true); | ||
assert.deepEqual(/2\. Ovo je testna stranica/.test(res.body), true); | ||
}); | ||
}); | ||
|
||
it('should request html with default variant', () => { | ||
return preq.get({ | ||
uri: `${server.config.variantsWikiBucketURL}/html/${variantsPageTitle}`, | ||
headers: { | ||
'accept-language': 'sr' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.deepEqual(res.headers.vary.toLowerCase(), 'accept-language'); | ||
assert.deepEqual(/1\. Ово је тестна страница/.test(res.body), true); | ||
assert.deepEqual(/2\. Ovo je testna stranica/.test(res.body), true); | ||
}); | ||
}); | ||
|
||
it('should request html with wrong variant', () => { | ||
return preq.get({ | ||
uri: `${server.config.variantsWikiBucketURL}/html/${variantsPageTitle}`, | ||
headers: { | ||
'accept-language': 'sr-blablabla' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.deepEqual(res.headers.vary.toLowerCase(), 'accept-language'); | ||
assert.deepEqual(/1\. Ово је тестна страница/.test(res.body), true); | ||
assert.deepEqual(/2\. Ovo je testna stranica/.test(res.body), true); | ||
}); | ||
}); | ||
|
||
it('should request html with cyrillic variant', () => { | ||
return preq.get({ | ||
uri: `${server.config.variantsWikiBucketURL}/html/${variantsPageTitle}`, | ||
headers: { | ||
'accept-language': 'sr-ec' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.deepEqual(res.headers.vary.toLowerCase(), 'accept-language'); | ||
assert.deepEqual(/1\. Ово је тестна страница/.test(res.body), true); | ||
assert.deepEqual(/2\. Ово је тестна страница/.test(res.body), true); | ||
}); | ||
}); | ||
|
||
it('should request html with latin variant', () => { | ||
return preq.get({ | ||
uri: `${server.config.variantsWikiBucketURL}/html/${variantsPageTitle}`, | ||
headers: { | ||
'accept-language': 'sr-el' | ||
} | ||
}) | ||
.then((res) => { | ||
assert.deepEqual(res.status, 200); | ||
assert.deepEqual(res.headers.vary.toLowerCase(), 'accept-language'); | ||
assert.deepEqual(/1\. Ovo je testna stranica/.test(res.body), true); | ||
assert.deepEqual(/2\. Ovo je testna stranica/.test(res.body), true); | ||
}); | ||
}); | ||
}); |
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