Skip to content

Replace preq with api-testing REST client #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .api-testing.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"base_uri": ""
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"ajv": "^6.10.2",
"api-testing": "^1.4.2",
"bunyan": "^1.8.12",
"coveralls": "^3.1.0",
"eslint": "^5.16.0",
Expand Down
217 changes: 93 additions & 124 deletions test/features/parsoid/transform.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
'use strict';

const assert = require('../../utils/assert.js');
const Server = require('../../utils/server.js');
const preq = require('preq');
try {
// check whether api-testing can be loaded by the node 6 CI
const apiTesting = require('api-testing');
}
catch (e) {
// skip this whole test suite if api-testing is not loadable
return;
}
const { REST, assert } = require('api-testing');
const mwUtil = require('../../../lib/mwUtil');

const testPage = {
title: 'User:Pchelolo%2fRestbase_Test',
Expand All @@ -15,197 +23,167 @@ describe('transform api', function() {
this.timeout(20000);
let contentTypes;
const server = new Server();
const client = new REST('');
Copy link
Contributor

@Pchelolo Pchelolo Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to do here in this pull request, but it would've been sweet if we could inject the config into the clients @clarakosi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the current workaround for the JSON file would be to declare an environment variable for the base URI. Probably easiest to do it in the test command in package.json but that would complicate things when trying to run a single file. Will look into why we did it this way next week

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}`
});
return client.get(
`${server.config.bucketURL('en.wikipedia.beta.wmflabs.org')}/html/${testPage.title}/${testPage.revision}`
);
})
.then((res) => {
testPage.html = res.body;
testPage.html = res.text;
});
});
after(() => server.stop());

it('wt2html', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
body: {
wikitext: '== Heading =='
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
{ wikitext: '== Heading ==' }
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, contentTypes.html);
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.html]));
const pattern = /<h2.*>Heading<\/h2>/;
if (!pattern.test(res.body)) {
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`);
if (!pattern.test(res.text)) {
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.text}`);
}
});
});

it('wt2html, title-recision for a new page', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/User:Pchelolo%2FRESTBaseTestPage_transform/393301`,
body: {
wikitext: '== Heading =='
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/User:Pchelolo%2FRESTBaseTestPage_transform/393301`,
{ wikitext: '== Heading ==' }
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, contentTypes.html);
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.html]));
const pattern = /<h2.*>Heading<\/h2>/;
if (!pattern.test(res.body)) {
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.body}`);
if (!pattern.test(res.text)) {
throw new Error(`Expected pattern in response: ${pattern}\nSaw: ${res.text}`);
}
});
});

it('wt2html with body_only', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
body: {
wikitext: '== Heading ==',
body_only: true
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
{ wikitext: '== Heading ==', body_only: true }
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.contentType(res, contentTypes.html);
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.html]));
const pattern = /^<h2.*>Heading<\/h2>$/;
if (!pattern.test(res.body)) {
if (!pattern.test(res.text)) {
throw new Error(`Expected pattern in response: ${pattern
}\nSaw: ${res.body}`);
}\nSaw: ${res.text}`);
}
});
});


it('wt2lint', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/lint`,
body: {
wikitext: '== Heading =='
}
}).then((res) => {
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/lint`,
{ wikitext: '== Heading ==' }
).then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body, []);
});
});

it('wt2lint with errors', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/lint`,
body: {
wikitext: '<div>No div ending'
}
}).then((res) => {
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/lint`,
{ wikitext: '<div>No div ending' }
).then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body.length, 1);
});
});

it('html2wt, no-selser', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}`,
body: {
html: '<body>The modified HTML</body>'
}
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}`,
{ html: '<body>The modified HTML</body>' }

})
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body, 'The modified HTML');
assert.contentType(res, contentTypes.wikitext);
assert.deepEqual(res.text, 'The modified HTML');
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.wikitext]));
});
});

it('html2wt, selser', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
body: {
html: testPage.html
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
{ html: testPage.html }
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body, testPage.wikitext);
assert.contentType(res, contentTypes.wikitext);
assert.deepEqual(res.text, testPage.wikitext);
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.wikitext]));
});
});

it('html2wt with scrub_wikitext', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext`,
body: {
html: '<h2></h2>',
scrub_wikitext: 1
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext`,
{ html: '<h2></h2>', scrub_wikitext: true }
)
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body, '');
assert.deepEqual(res.text, '');
});
});

it('supports reversed order of properties in TimeUuid meta', () => {
const newHtml = testPage.html.replace(/<meta property="mw:TimeUuid" content="([^"]+)"\/?>/,
'<meta content="$1" property="mw:TimeUuid" />');
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
body: {
html: newHtml
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
{ html: newHtml }
)
.then((res) => {
assert.deepEqual(res.status, 200);
const pattern = /Selser test/;
if (!pattern.test(res.body)) {
if (!pattern.test(res.text)) {
throw new Error(`Expected pattern in response: ${pattern
}\nSaw: ${JSON.stringify(res, null, 2)}`);
}
assert.contentType(res, contentTypes.wikitext);
assert.match(res.headers['content-type'], mwUtil.constructRegex([contentTypes.wikitext]));
});
});

it('supports stashing content', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}/${testPage.revision}`,
body: {
wikitext: '== ABCDEF ==',
stash: true
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}/${testPage.revision}`,
{ wikitext: '== ABCDEF ==', stash: true }
)
.then((res) => {
assert.deepEqual(res.status, 200);
const etag = res.headers.etag;
assert.deepEqual(/\/stash"$/.test(etag), true);
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
headers: {
'if-match': etag
},
body: {
html: res.body.replace('>ABCDEF<', '>FECDBA<')
}
});
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
{ html: res.text.replace('>ABCDEF<', '>FECDBA<') },
{ 'if-match': etag }
);
})
.then((res) => {
assert.deepEqual(res.status, 200);
assert.deepEqual(res.body, '== FECDBA ==');
assert.deepEqual(res.text, '== FECDBA ==');
});
});

it('substitutes 0 as revision if not provided for stashing', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
body: {
wikitext: '== ABCDEF ==',
stash: true
}
})
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html/${testPage.title}`,
{ wikitext: '== ABCDEF ==', stash: true }
)
.then((res) => {
assert.deepEqual(res.status, 200);
const etag = res.headers.etag;
Expand All @@ -214,32 +192,23 @@ describe('transform api', function() {
});

it('does not allow stashing without title', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html`,
body: {
wikitext: '== ABCDEF ==',
stash: true
}
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/wikitext/to/html`,
{ wikitext: '== ABCDEF ==', stash: true }
)
.then((res) => {
assert.deepEqual(res.error.status, 400);
})
.then(() => {
throw new Error('Error should be thrown');
}, (e) => {
assert.deepEqual(e.status, 400);
});
});

it('does not allow to transform html with no tid', () => {
return preq.post({
uri: `${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
body: {
html: '<h1>A</h1>'
}
return client.post(
`${server.config.baseURL('en.wikipedia.beta.wmflabs.org')}/transform/html/to/wikitext/${testPage.title}/${testPage.revision}`,
{ html: '<h1>A</h1>' }
)
.then((res) => {
assert.deepEqual(res.error.status, 400);
})
.then(() => {
throw new Error('Error should be thrown');
}, (e) => {
assert.deepEqual(e.status, 400);
});
});
});