Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #278 from adobe/xml-extension-points
Browse files Browse the repository at this point in the history
XML extension points
  • Loading branch information
trieloff authored Apr 24, 2019
2 parents 84f38a2 + 88a558e commit 8545010
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/defaults/xml.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const check = require('../xml/check-xml');
const parseFrontmatter = require('../html/parse-frontmatter');

/* eslint no-param-reassign: off */
/* eslint newline-per-chained-call: off */

const xmlpipe = (cont, payload, action) => {
action.logger = action.logger || log;
Expand All @@ -38,21 +39,20 @@ const xmlpipe = (cont, payload, action) => {
pipe
.every(dump).when(() => !production())
.every(validate).when(() => !production())
.before(fetch)
.before(parse)
.before(fetch).expose('fetch')
.before(parse).expose('parse')
.before(parseFrontmatter)
.before(smartypants)
.before(sections)
.before(meta)
.before(meta).expose('meta')
.once(cont)
.after(emit)
.after(emit).expose('xml')
.after(type('application/xml'))
.after(check)
.after(cache)
.when(uncached)
.after(key)
.after(flag)
.when(esi) // flag ESI when there is ESI in the response
.after(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
.error(selectStatus(production()));

action.logger.log('debug', 'Running XML pipeline');
Expand Down
70 changes: 69 additions & 1 deletion test/testXML.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,75 @@ describe('Testing XML Pipeline', () => {
assert.equal(result.response.body, expectedXML);
});

it('xmp.pipe does not overwrite existing respone body', async () => {
it('xml.pipe can be extended', async () => {
const myfunc = ({ content }) => ({
content: {
xml: {
document: {
title: {
'#text': content.title,
'@level': 1,
},
},
},
},
});

let calledfoo = false;
let calledbar = false;
let calledbaz = false;
function foo() {
assert.equal(calledfoo, false, 'foo has not yet been called');
assert.equal(calledbar, false, 'bar has not yet been called');
calledfoo = true;
}

function bar() {
assert.equal(calledfoo, true, 'foo has been called');
assert.equal(calledbar, false, 'bar has not yet been called');
calledbar = true;
}

function baz() {
calledbaz = true;
}

function shouttitle(p) {
const { title } = p.content.xml.document;
title['#text'] = `${title['#text'].toUpperCase()}!!!`;
return p;
}

myfunc.before = {
fetch: foo,
xml: shouttitle,
};

myfunc.after = {
esi: bar,
// after the metadata has been retrieved, make sure that
// the title is being shouted
never: baz,
};

const res = await pipe(
myfunc,
{ },
{
request: { params },
secrets,
logger,
},
);

assert.equal(calledfoo, true, 'foo has been called');
assert.equal(calledbar, true, 'bar has been called');
assert.equal(calledbaz, false, 'baz has never been called');

assert.ok(res.response.body.match(/FUTURE!!!/));
});

it('xmp.pipe does not overwrite existing response body', async () => {
const result = await pipe(
() => {},
payload,
Expand Down

0 comments on commit 8545010

Please sign in to comment.