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

Commit

Permalink
Update getMetadata test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlockhart committed Aug 25, 2016
1 parent 9b6c2d4 commit 513e2a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 2 additions & 6 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,11 @@ const metadataRules = {
},
};

function getMetadata(doc, rules, url) {
function getMetadata(doc, rules, context) {
const metadata = {};
const ruleSet = rules || metadataRules;

const context = {};

if (url) {
context.url = url;
}
context = context || {};

Object.keys(ruleSet).map(metadataKey => {
const metadataRule = ruleSet[metadataKey];
Expand Down
21 changes: 21 additions & 0 deletions tests/getMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ describe('Get Metadata Tests', function() {
assert.equal(metadata.url, sampleUrl, `Unable to find ${sampleUrl} in ${sampleHtml}`);
});

it('uses absolute URLs when url parameter passed in through context', () => {
const relativeHtml = `
<html>
<head>
<meta property="og:description" content="${sampleDescription}" />
<link rel="icon" href="/favicon.ico" />
<meta property="og:image" content="/image.png" />
<meta property="og:title" content="${sampleTitle}" />
<meta property="og:type" content="${sampleType}" />
<meta property="og:url" content="${sampleUrl}" />
</head>
</html>
`;

const doc = stringToDom(relativeHtml);
const metadata = getMetadata(doc, metadataRules, {url: 'http://www.example.com/'});

assert.equal(metadata.icon_url, sampleIcon, `Unable to find ${sampleIcon} in ${sampleHtml}`);
assert.equal(metadata.image_url, sampleImageHTTP, `Unable to find ${sampleImageHTTP} in ${sampleHtml}`);
});

it('allows custom rules', () => {
const doc = stringToDom(sampleHtml);
const rules = {
Expand Down

0 comments on commit 513e2a6

Please sign in to comment.