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

Commit

Permalink
test(embed): provide integration test for internal embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Apr 18, 2019
1 parent 8ceef49 commit 73cb0fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils/embed-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
* Handles `embed` MDAST nodes by converting them into `<esi:include>` tags
* @param {string} EMBED_SERVICE the URL of an embedding service compatible with https://github.com/adobe/helix-embed that returns HTML
*/
const URI = require('uri-js');

function embed({ EMBED_SERVICE }) {
return function handler(h, node) {
const { url } = node;
const props = {
src: EMBED_SERVICE + url,
// prepend the embed service for absolute URLs
src: (URI.parse(url).reference === 'absolute' ? EMBED_SERVICE : '') + url,
};
const retval = h(node, 'esi:include', props);
return retval;
Expand Down
30 changes: 30 additions & 0 deletions test/testEmbedHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ https://www.youtube.com/watch?v=KOxbO0EI4MA
assert.equal(result.response.body, `<p>Hello World
Here comes an embed.</p>
<esi:include src="https://example-embed-service.com/https://www.youtube.com/watch?v=KOxbO0EI4MA"></esi:include>
<p><img src="easy.png" alt="Easy!" srcset="easy.png?width=480&amp;auto=webp 480w,easy.png?width=1384&amp;auto=webp 1384w,easy.png?width=2288&amp;auto=webp 2288w,easy.png?width=3192&amp;auto=webp 3192w,easy.png?width=4096&amp;auto=webp 4096w" sizes="100vw"></p>`);
});

it('html.pipe processes internal embeds', async () => {
const result = await pipe(
({ content }) => ({ response: { status: 201, body: content.document.body.innerHTML } }),
{
request: crequest,
content: {
body: `Hello World
Here comes an embed.
./foo.md
![Easy!](easy.png)
`,
},
},
{
request: { params },
secrets,
logger,
},
);

assert.equal(result.response.status, 201);
assert.equal(result.response.headers['Content-Type'], 'text/html');
assert.equal(result.response.body, `<p>Hello World
Here comes an embed.</p>
<esi:include src="/test/foo.embed.html"></esi:include>
<p><img src="easy.png" alt="Easy!" srcset="easy.png?width=480&amp;auto=webp 480w,easy.png?width=1384&amp;auto=webp 1384w,easy.png?width=2288&amp;auto=webp 2288w,easy.png?width=3192&amp;auto=webp 3192w,easy.png?width=4096&amp;auto=webp 4096w" sizes="100vw"></p>`);
});
});

0 comments on commit 73cb0fc

Please sign in to comment.