From d0935730b8fa2a9684c0b1e3fbe1e613a703e4a5 Mon Sep 17 00:00:00 2001 From: Dylan Greene Date: Fri, 19 Dec 2014 12:56:24 -0500 Subject: [PATCH] rebuilt tests using tape, prova. --- .npmignore | 4 + Gruntfile.js | 19 +- lib/{rss.js => index.js} | 20 +- package.json | 35 +- readme.md | 10 +- templates/readme/usage.md | 7 +- test/expectedOutput/customElements.xml | 38 ++ test/expectedOutput/customNamespaces.xml | 38 ++ test/expectedOutput/default.xml | 2 + test/expectedOutput/defaultOneItem.xml | 2 + test/expectedOutput/enclosures.xml | 38 ++ test/expectedOutput/latLong.xml | 30 ++ test/expectedOutput/pubSubHubbub.xml | 12 + test/expectedOutput/simpleFeed.xml | 2 + test/expectedOutput/simpleFeedFormated.xml | 71 ++++ test/index.js | 302 ++++++++++++++ test/rss.test.js | 462 --------------------- 17 files changed, 597 insertions(+), 495 deletions(-) create mode 100644 .npmignore rename lib/{rss.js => index.js} (94%) create mode 100644 test/expectedOutput/customElements.xml create mode 100644 test/expectedOutput/customNamespaces.xml create mode 100644 test/expectedOutput/default.xml create mode 100644 test/expectedOutput/defaultOneItem.xml create mode 100644 test/expectedOutput/enclosures.xml create mode 100644 test/expectedOutput/latLong.xml create mode 100644 test/expectedOutput/pubSubHubbub.xml create mode 100644 test/expectedOutput/simpleFeed.xml create mode 100644 test/expectedOutput/simpleFeedFormated.xml create mode 100644 test/index.js delete mode 100644 test/rss.test.js diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..02268d6 --- /dev/null +++ b/.npmignore @@ -0,0 +1,4 @@ +/test/ +/.idea/ +/templates/ +*.log \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js index 3622e9e..dc98e1d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -15,15 +15,6 @@ module.exports = function(grunt) { require('time-grunt')(grunt); grunt.initConfig({ - mochaTest: { - notify: { - src: 'test/**/*.test.js', - options: { - reporter: 'spec' - } - } - }, - jshint: { options: { jshintrc: '.jshintrc' @@ -39,17 +30,17 @@ module.exports = function(grunt) { require('load-grunt-tasks')(grunt); - grunt.registerTask('test', [ - 'jshint', - 'mochaTest' + grunt.registerTask('lint', [ + 'jshint' ]); grunt.registerTask('default', [ - 'test' + 'lint' ]); grunt.registerTask('pre-publish', [ - 'test', + 'lint', + 'repos', 'readme' ]); }; diff --git a/lib/rss.js b/lib/index.js similarity index 94% rename from lib/rss.js rename to lib/index.js index 8199b62..e9fa9ce 100755 --- a/lib/rss.js +++ b/lib/index.js @@ -1,8 +1,9 @@ 'use strict'; -var xml = require('xml'), - mime = require('mime'), - fs = require('fs'); +var mime = require('mime-types'); +var xml = require('xml'); +var fs = require('fs'); + function ifTruePush(bool, array, data) { if (bool) { @@ -20,6 +21,13 @@ function ifTruePushArray(bool, array, dataArray) { }); } +function getSize(filename) { + if (typeof fs === 'undefined') { + return 0; + } + return fs.statSync(filename).size; +} + function generateXML (data){ var channel = []; @@ -31,7 +39,7 @@ function generateXML (data){ channel.push({ image: [ {url: data.image_url}, {title: data.title}, {link: data.site_url} ] }); } channel.push({ generator: data.generator }); - channel.push({ lastBuildDate: new Date().toGMTString() }); + channel.push({ lastBuildDate: new Date().toUTCString() }); ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); ifTruePush(data.author, channel, { 'author': { _cdata: data.author } }); @@ -72,13 +80,13 @@ function generateXML (data){ ifTruePush(item.lat, item_values, {'geo:lat': item.lat}); ifTruePush(item.long, item_values, {'geo:long': item.long}); - if( item.enclosure && item.enclosure.url ) { + if( item.enclosure && item.enclosure.url) { if( item.enclosure.file ) { item_values.push({ enclosure : { _attr : { url : item.enclosure.url, - length : fs.statSync(item.enclosure.file).size, + length : item.enclosure.size || getSize(item.enclosure.file), type : mime.lookup(item.enclosure.file) } } diff --git a/package.json b/package.json index 3c539c1..4e47d2a 100644 --- a/package.json +++ b/package.json @@ -12,9 +12,11 @@ "feed builder", "rss feed" ], - "main": "lib/rss.js", + "main": "lib/index", "scripts": { - "test": "grunt test" + "test": "tape test", + "lint": "jshint", + "test:browser": "prova -b" }, "homepage": "http://github.com/dylang/node-rss", "author": { @@ -39,22 +41,43 @@ "bugs": { "url": "http://github.com/dylang/node-rss/issues" }, + "testling": { + "files": "test/*.js", + "browsers": [ + "ie/9..latest", + "chrome/latest", + "firefox/latest", + "safari/latest", + "opera/latest", + "iphone/latest", + "ipad/latest", + "android-browser/latest" + ] + }, "dependencies": { - "mime": "^1.2.11", + "folderify": "^0.6.0", + "mime-types": "^2.0.3", "xml": "^1.0.0" }, "devDependencies": { - "chai": "^1.9.1", + "folderify": "^0.6.0", "grunt": "^0.4.5", "grunt-contrib-jshint": "^0.10.0", - "grunt-mocha-test": "^0.12.1", "grunt-release": "^0.9.0", "grunt-templates-dylang": "^1.0.0", + "include-folder": "^0.7.0", "load-grunt-tasks": "^1.0.0", - "mocha": "^2.0.1", + "mockdate": "^1.0.0", + "prova": "^2.0.3", "q": "^1.0.0", + "tape": "^3.0.3", "time-grunt": "^1.0.0", "xml2js": "^0.4.1" }, + "browserify": { + "transform": [ + "folderify" + ] + }, "licenses": "MIT" } diff --git a/readme.md b/readme.md index bbc2bef..bafa300 100644 --- a/readme.md +++ b/readme.md @@ -70,13 +70,15 @@ feed.item(itemOptions); ##### Feed XML ```js -var xml = feed.xml(indent); +var xml = feed.xml({indent: true}); ``` This returns the XML as a string. -`indent` _optional_ **string** What to use as a tab. Defaults to no tabs (compressed). - For example you can use `'\t'` for tab character, or `' '` for two-space tabs. +`indent` _optional_ **boolean or string** What to use as a tab. Defaults to no tabs (compressed). +For example you can use `'\t'` for tab character, or `' '` for two-space tabs. If you set it to +`true` it will use four spaces. + ### Example Usage @@ -215,5 +217,5 @@ Released under the [MIT license](https://tldrlegal.com/license/mit-license). Screenshots are [CC BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) (Attribution-ShareAlike). *** -_Generated using [grunt-readme](https://github.com/assemble/grunt-readme) with [grunt-templates-dylang](https://github.com/dylang/grunt-templates-dylang) on Monday, December 8, 2014._ +_Generated using [grunt-readme](https://github.com/assemble/grunt-readme) with [grunt-templates-dylang](https://github.com/dylang/grunt-templates-dylang) on Friday, December 19, 2014._ diff --git a/templates/readme/usage.md b/templates/readme/usage.md index 18c369b..fe0528e 100644 --- a/templates/readme/usage.md +++ b/templates/readme/usage.md @@ -59,10 +59,11 @@ feed.item(itemOptions); #### Feed XML ```js -var xml = feed.xml(indent); +var xml = feed.xml({indent: true}); ``` This returns the XML as a string. -`indent` _optional_ **string** What to use as a tab. Defaults to no tabs (compressed). - For example you can use `'\t'` for tab character, or `' '` for two-space tabs. \ No newline at end of file +`indent` _optional_ **boolean or string** What to use as a tab. Defaults to no tabs (compressed). +For example you can use `'\t'` for tab character, or `' '` for two-space tabs. If you set it to +`true` it will use four spaces. diff --git a/test/expectedOutput/customElements.xml b/test/expectedOutput/customElements.xml new file mode 100644 index 0000000..fa3d1aa --- /dev/null +++ b/test/expectedOutput/customElements.xml @@ -0,0 +1,38 @@ + + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Wed, 10 Dec 2014 19:04:57 GMT + + + Sun, 20 May 2012 04:00:00 GMT + + 60 + A show about everything + John Doe + All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store + + John Doe + john.doe@example.com + + + + + + + <![CDATA[item 1]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + John Doe + A short primer on table spices + + 7:04 + + + \ No newline at end of file diff --git a/test/expectedOutput/customNamespaces.xml b/test/expectedOutput/customNamespaces.xml new file mode 100644 index 0000000..fc3684d --- /dev/null +++ b/test/expectedOutput/customNamespaces.xml @@ -0,0 +1,38 @@ + + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Wed, 10 Dec 2014 19:04:57 GMT + + + Sun, 20 May 2012 04:00:00 GMT + + 60 + A show about everything + John Doe + All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store + + John Doe + john.doe@example.com + + + + + + + <![CDATA[item 1]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + John Doe + A short primer on table spices + + 7:04 + + + \ No newline at end of file diff --git a/test/expectedOutput/default.xml b/test/expectedOutput/default.xml new file mode 100644 index 0000000..3f2ba69 --- /dev/null +++ b/test/expectedOutput/default.xml @@ -0,0 +1,2 @@ + +<![CDATA[Untitled RSS Feed]]>http://github.com/dylang/node-rssRSS for NodeWed, 10 Dec 2014 19:04:57 GMT \ No newline at end of file diff --git a/test/expectedOutput/defaultOneItem.xml b/test/expectedOutput/defaultOneItem.xml new file mode 100644 index 0000000..422e49f --- /dev/null +++ b/test/expectedOutput/defaultOneItem.xml @@ -0,0 +1,2 @@ + +<![CDATA[Untitled RSS Feed]]>http://github.com/dylang/node-rssRSS for NodeWed, 10 Dec 2014 19:04:57 GMT<![CDATA[No title]]>No title \ No newline at end of file diff --git a/test/expectedOutput/enclosures.xml b/test/expectedOutput/enclosures.xml new file mode 100644 index 0000000..de9d6d2 --- /dev/null +++ b/test/expectedOutput/enclosures.xml @@ -0,0 +1,38 @@ + + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Wed, 10 Dec 2014 19:04:57 GMT + + + + <![CDATA[item 1]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + + + <![CDATA[item 2]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + + + + <![CDATA[item 3]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + + + + \ No newline at end of file diff --git a/test/expectedOutput/latLong.xml b/test/expectedOutput/latLong.xml new file mode 100644 index 0000000..5c1d827 --- /dev/null +++ b/test/expectedOutput/latLong.xml @@ -0,0 +1,30 @@ + + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Wed, 10 Dec 2014 19:04:57 GMT + + + + <![CDATA[item 1]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + 12232 + 13333.23323 + + + <![CDATA[item 2]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + + + \ No newline at end of file diff --git a/test/expectedOutput/pubSubHubbub.xml b/test/expectedOutput/pubSubHubbub.xml new file mode 100644 index 0000000..185c64d --- /dev/null +++ b/test/expectedOutput/pubSubHubbub.xml @@ -0,0 +1,12 @@ + + + + <![CDATA[title]]> + + http://example.com + RSS for Node + Wed, 10 Dec 2014 19:04:57 GMT + + + + \ No newline at end of file diff --git a/test/expectedOutput/simpleFeed.xml b/test/expectedOutput/simpleFeed.xml new file mode 100644 index 0000000..a63efad --- /dev/null +++ b/test/expectedOutput/simpleFeed.xml @@ -0,0 +1,2 @@ + +<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comExample GeneratorWed, 10 Dec 2014 19:04:57 GMTSun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT \ No newline at end of file diff --git a/test/expectedOutput/simpleFeedFormated.xml b/test/expectedOutput/simpleFeedFormated.xml new file mode 100644 index 0000000..aa5df69 --- /dev/null +++ b/test/expectedOutput/simpleFeedFormated.xml @@ -0,0 +1,71 @@ + + + + <![CDATA[title]]> + + http://example.com + + http://example.com/icon.png + title + http://example.com + + Example Generator + Wed, 10 Dec 2014 19:04:57 GMT + + + Sun, 20 May 2012 04:00:00 GMT + + + + + http://example.com/rss/docs.html + 60 + + + + + <![CDATA[item 1]]> + + http://example.com/article1 + http://example.com/article1 + + Thu, 24 May 2012 04:00:00 GMT + + + <![CDATA[item 2]]> + + http://example.com/article2 + http://example.com/article2 + + Fri, 25 May 2012 04:00:00 GMT + + + <![CDATA[item 3]]> + + http://example.com/article3 + item3 + + Sat, 26 May 2012 04:00:00 GMT + + + <![CDATA[item 4 & html test with <strong>]]> + html]]> + http://example.com/article4?this&that + http://example.com/article4?this&that + + Sun, 27 May 2012 04:00:00 GMT + + + <![CDATA[item 5 & test for categories]]> + + http://example.com/article5 + http://example.com/article5 + + + + + + Mon, 28 May 2012 04:00:00 GMT + + + \ No newline at end of file diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..36dec7a --- /dev/null +++ b/test/index.js @@ -0,0 +1,302 @@ +// prova is a wrapper for tape +// use npm run test:browser to run tests in a browser +var test = require('prova'); + +var RSS = require('..'); +var xml2js = require('xml2js'); +var q = require('q'); + +var includeFolder = require('include-folder'); +var expectedOutput = includeFolder(__dirname + '/expectedOutput', /.*\.xml$/); + +// Dates in XML files will always be this value. +require('mockdate').set('Wed, 10 Dec 2014 19:04:57 GMT'); + +test('empty feed', function(t) { + t.plan(2); + var feed = new RSS(); + t.equal(feed.xml(), expectedOutput.default); + feed.item(); + t.equal(feed.xml(), expectedOutput.defaultOneItem); +}); + +test('indentation', function(t) { + t.plan(4); + var feed = new RSS({ + title: 'title', + description: 'description', + generator: 'Example Generator', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + image_url: 'http://example.com/icon.png', + author: 'Dylan Greene', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + docs: 'http://example.com/rss/docs.html', + copyright: '2013 Dylan Green', + language: 'en', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + ttl: '60' + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 2', + description: 'description 2', + url: 'http://example.com/article2', + date: 'May 25, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 3', + description: 'description 3', + url: 'http://example.com/article3', + guid: 'item3', + date: 'May 26, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 4 & html test with ', + description: 'description 4 uses some html', + url: 'http://example.com/article4?this&that', + author: 'Guest Author', + date: 'May 27, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 5 & test for categories', + description: 'description 5', + url: 'http://example.com/article5', + categories: ['Category 1','Category 2','Category 3','Category 4'], + author: 'Guest Author', + date: 'May 28, 2012 04:00:00 GMT' + }); + + var qParseXml = q.nbind(xml2js.parseString, xml2js); + + var xmlWithoutIndents = feed.xml({indent: false}); + var xmlWithIndents = feed.xml({indent: true}); + + t.notEqual(xmlWithoutIndents, xmlWithIndents); + t.equal(xmlWithoutIndents, expectedOutput.simpleFeed); + t.equal(xmlWithIndents, expectedOutput.simpleFeedFormated); + + q.all([ + qParseXml(xmlWithoutIndents), + qParseXml(xmlWithIndents) + ]) + .spread(function (fromWithoutIndents, fromWithIndents){ + t.deepEqual(JSON.stringify(fromWithoutIndents), JSON.stringify(fromWithIndents)); + }); +}); + +test('enclosure', function(t) { + //if (typeof window) return; + + t.plan(1); + + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + author: 'Dylan Greene' + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + enclosure : 'incorrect value' + }); + + feed.item({ + title: 'item 2', + description: 'description 2', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + enclosure : {url: '/media/some-file.flv'} + }); + + feed.item({ + title: 'item 3', + description: 'description 3', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + enclosure : { + url: '/media/image.png', + file: __dirname + '/image.png', + size: 16650 // this is optional + } + }); + + t.equal(feed.xml({indent: true}), expectedOutput.enclosures); +}); + + +test('geoRSS', function(t) { + t.plan(1); + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + author: 'Dylan Greene' + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + lat: 12232, + long: 13333.23323 + }); + + feed.item({ + title: 'item 2', + description: 'description 2', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT' + }); + + t.equal(feed.xml({indent: true}), expectedOutput.latLong); +}); + +test('PubSubHubbub hub', function(t) { + t.plan(1); + + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + hub: 'http://example.com/hub' + }); + + t.equal(feed.xml({indent: true}), expectedOutput.pubSubHubbub); +}); + + +test('custom elements', function(t) { + t.plan(1); + + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + author: 'Dylan Greene', + pubDate: 'May 20, 2012 04:00:00 GMT', + language: 'en', + ttl: '60', + custom_elements: [ + {'itunes:subtitle': 'A show about everything'}, + {'itunes:author': 'John Doe'}, + {'itunes:summary': 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store'}, + {'itunes:owner': [ + {'itunes:name': 'John Doe'}, + {'itunes:email': 'john.doe@example.com'} + ]}, + {'itunes:image': { + _attr: { + href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg' + } + }}, + {'itunes:category': [ + {_attr: { + text: 'Technology' + }}, + {'itunes:category': { + _attr: { + text: 'Gadgets' + } + }} + ]} + ] + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + custom_elements: [ + {'itunes:author': 'John Doe'}, + {'itunes:subtitle': 'A short primer on table spices'}, + {'itunes:image': { + _attr: { + href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg' + } + }}, + {'itunes:duration': '7:04'} + ] + }); + + t.equal(feed.xml({indent: true}), expectedOutput.customElements); +}); + +test('custom namespaces', function(t) { + t.plan(1); + + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + author: 'Dylan Greene', + pubDate: 'May 20, 2012 04:00:00 GMT', + language: 'en', + ttl: '60', + custom_namespaces: { + 'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd' + }, + custom_elements: [ + {'itunes:subtitle': 'A show about everything'}, + {'itunes:author': 'John Doe'}, + {'itunes:summary': 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store'}, + {'itunes:owner': [ + {'itunes:name': 'John Doe'}, + {'itunes:email': 'john.doe@example.com'} + ]}, + {'itunes:image': { + _attr: { + href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg' + } + }}, + {'itunes:category': [ + {_attr: { + text: 'Technology' + }}, + {'itunes:category': { + _attr: { + text: 'Gadgets' + } + }} + ]} + ] + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + custom_elements: [ + {'itunes:author': 'John Doe'}, + {'itunes:subtitle': 'A short primer on table spices'}, + {'itunes:image': { + _attr: { + href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg' + } + }}, + {'itunes:duration': '7:04'} + ] + }); + + t.equal(feed.xml({indent: true}), expectedOutput.customNamespaces); +}); diff --git a/test/rss.test.js b/test/rss.test.js deleted file mode 100644 index 39df2d6..0000000 --- a/test/rss.test.js +++ /dev/null @@ -1,462 +0,0 @@ -/* - use `npm test` to run tests using mocha -*/ - -var RSS = require('../lib/rss'); -var expect = require('chai').expect; -var xml2js = require('xml2js'); -var Q = require('q'); - -describe('rss module', function(done) { - - it('should work with an empty feed', function(done) { - var feed = new RSS(); - expect(feed.xml()).to.equal('\n<![CDATA[Untitled RSS Feed]]>http://github.com/dylang/node-rssRSS for Node' + new Date().toUTCString() +''); - feed.item(); - expect(feed.xml()).to.equal('\n<![CDATA[Untitled RSS Feed]]>http://github.com/dylang/node-rssRSS for Node' + new Date().toUTCString() +'<![CDATA[No title]]>No title'); - done(); - }); - - var simpleFeed = function() { - var feed = new RSS({ - title: 'title', - description: 'description', - generator: 'Example Generator', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - image_url: 'http://example.com/icon.png', - author: 'Dylan Greene', - categories: ['Category 1','Category 2','Category 3'], - pubDate: 'May 20, 2012 04:00:00 GMT', - docs: 'http://example.com/rss/docs.html', - copyright: '2013 Dylan Green', - language: 'en', - managingEditor: 'Dylan Green', - webMaster: 'Dylan Green', - ttl: '60' - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 2', - description: 'description 2', - url: 'http://example.com/article2', - date: 'May 25, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 3', - description: 'description 3', - url: 'http://example.com/article3', - guid: 'item3', - date: 'May 26, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 4 & html test with ', - description: 'description 4 uses some html', - url: 'http://example.com/article4?this&that', - author: 'Guest Author', - date: 'May 27, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 5 & test for categories', - description: 'description 5', - url: 'http://example.com/article5', - categories: ['Category 1','Category 2','Category 3','Category 4'], - author: 'Guest Author', - date: 'May 28, 2012 04:00:00 GMT' - }); - return feed; - } - - it('should work with indentation', function(done) { - var feed = simpleFeed(); - var xml = feed.xml(true); - var parseString = Q.nbind(xml2js.parseString,xml2js); - - var feedToXml = parseString(feed.xml()); - var feedToXmlIndent = parseString(feed.xml(true)); - Q.all([feedToXml,feedToXmlIndent]).spread(function (xml,xmlIndent){ - expect(JSON.stringify(xml)).to.equal(JSON.stringify(xmlIndent)); - }).done(done()); - }); - - it('should work with an easy test', function(done) { - var feed = simpleFeed(); - var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comExample Generator' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - it('should work without image_url', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - author: 'Dylan Greene', - categories: ['Category 1','Category 2','Category 3'], - pubDate: 'May 20, 2012 04:00:00 GMT', - docs: 'http://example.com/rss/docs.html', - copyright: '2013 Dylan Green', - language: 'en', - managingEditor: 'Dylan Green', - webMaster: 'Dylan Green', - ttl: '60' - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 2', - description: 'description 2', - url: 'http://example.com/article2', - date: 'May 25, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 3', - description: 'description 3', - url: 'http://example.com/article3', - guid: 'item3', - date: 'May 26, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 4 & html test with ', - description: 'description 4 uses some html', - url: 'http://example.com/article4?this&that', - author: 'Guest Author', - date: 'May 27, 2012 04:00:00 GMT' - }) - .item({ - title: 'item 5 & test for categories', - description: 'description 5', - url: 'http://example.com/article5', - categories: ['Category 1','Category 2','Category 3','Category 4'], - author: 'Guest Author', - date: 'May 28, 2012 04:00:00 GMT' - }); - - var expectedResult = '\n<![CDATA[title]]>http://example.comRSS for Node' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - it('should work with an enclosure', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - author: 'Dylan Greene' - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - enclosure : 'incorrect value' - }); - - feed.item({ - title: 'item 2', - description: 'description 2', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - enclosure : {url: '/media/some-file.flv'} - }); - - feed.item({ - title: 'item 3', - description: 'description 3', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - enclosure : {url: '/media/image.png', file : __dirname+'/image.png'} - }); - - var expectedResult = '\n<![CDATA[title]]>http://example.comRSS for Node' + new Date().toUTCString() +''+ - '' + - '<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ - '<![CDATA[item 2]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ - '<![CDATA[item 3]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ - ''; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - - it('should work with geoRSS', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - author: 'Dylan Greene' - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - lat: 12232, - long: 13333.23323 - }); - - feed.item({ - title: 'item 2', - description: 'description 2', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT' - }); - - var expectedResult = '\n<![CDATA[title]]>http://example.comRSS for Node' + new Date().toUTCString() +''+ - '' + - '<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT1223213333.23323'+ - '<![CDATA[item 2]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ - ''; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - it('should work with PubSubHubbub hub', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - hub: 'http://example.com/hub' - }); - - var expectedResult = '\n'+ - ''+ - ''+ - '<![CDATA[title]]>'+ - ''+ - 'http://example.com'+ - 'RSS for Node'+ - '' + new Date().toUTCString() +''+ - ''+ - ''+ - ''+ - ''; - - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - it('should work with custom elements', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - author: 'Dylan Greene', - pubDate: 'May 20, 2012 04:00:00 GMT', - language: 'en', - ttl: '60', - custom_elements: [ - {'itunes:subtitle': 'A show about everything'}, - {'itunes:author': 'John Doe'}, - {'itunes:summary': 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store'}, - {'itunes:owner': [ - {'itunes:name': 'John Doe'}, - {'itunes:email': 'john.doe@example.com'} - ]}, - {'itunes:image': { - _attr: { - href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg' - } - }}, - {'itunes:category': [ - {_attr: { - text: 'Technology' - }}, - {'itunes:category': { - _attr: { - text: 'Gadgets' - } - }} - ]} - ] - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - custom_elements: [ - {'itunes:author': 'John Doe'}, - {'itunes:subtitle': 'A short primer on table spices'}, - {'itunes:image': { - _attr: { - href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg' - } - }}, - {'itunes:duration': '7:04'} - ] - }); - - var expectedResult ='\n'+ - ''+ - '' + - '<![CDATA[title]]>' + - '' + - 'http://example.com' + - 'RSS for Node' + - '' + new Date().toUTCString() +'' + - '' + - '' + - 'Sun, 20 May 2012 04:00:00 GMT' + - '' + - '60' + - 'A show about everything' + - 'John Doe' + - 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store' + - '' + - 'John Doe' + - 'john.doe@example.com' + - '' + - '' + - '' + - '' + - '' + - '' + - '<![CDATA[item 1]]>' + - '' + - 'http://example.com/article1' + - 'http://example.com/article1' + - '' + - 'Thu, 24 May 2012 04:00:00 GMT' + - 'John Doe' + - 'A short primer on table spices' + - '' + - '7:04' + - '' + - '' + - ''; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); - - it('should work with custom namespaces', function(done) { - var feed = new RSS({ - title: 'title', - description: 'description', - feed_url: 'http://example.com/rss.xml', - site_url: 'http://example.com', - author: 'Dylan Greene', - pubDate: 'May 20, 2012 04:00:00 GMT', - language: 'en', - ttl: '60', - custom_namespaces: { - 'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd' - }, - custom_elements: [ - {'itunes:subtitle': 'A show about everything'}, - {'itunes:author': 'John Doe'}, - {'itunes:summary': 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store'}, - {'itunes:owner': [ - {'itunes:name': 'John Doe'}, - {'itunes:email': 'john.doe@example.com'} - ]}, - {'itunes:image': { - _attr: { - href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg' - } - }}, - {'itunes:category': [ - {_attr: { - text: 'Technology' - }}, - {'itunes:category': { - _attr: { - text: 'Gadgets' - } - }} - ]} - ] - }); - - feed.item({ - title: 'item 1', - description: 'description 1', - url: 'http://example.com/article1', - date: 'May 24, 2012 04:00:00 GMT', - custom_elements: [ - {'itunes:author': 'John Doe'}, - {'itunes:subtitle': 'A short primer on table spices'}, - {'itunes:image': { - _attr: { - href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg' - } - }}, - {'itunes:duration': '7:04'} - ] - }); - - var expectedResult ='\n'+ - ''+ - '' + - '<![CDATA[title]]>' + - '' + - 'http://example.com' + - 'RSS for Node' + - '' + new Date().toUTCString() +'' + - '' + - '' + - 'Sun, 20 May 2012 04:00:00 GMT' + - '' + - '60' + - 'A show about everything' + - 'John Doe' + - 'All About Everything is a show about everything. Each week we dive into any subject known to man and talk about it as much as we can. Look for our podcast in the Podcasts app or in the iTunes Store' + - '' + - 'John Doe' + - 'john.doe@example.com' + - '' + - '' + - '' + - '' + - '' + - '' + - '<![CDATA[item 1]]>' + - '' + - 'http://example.com/article1' + - 'http://example.com/article1' + - '' + - 'Thu, 24 May 2012 04:00:00 GMT' + - 'John Doe' + - 'A short primer on table spices' + - '' + - '7:04' + - '' + - '' + - ''; - var result = feed.xml(); - - expect(result).to.equal(expectedResult); - done(); - }); -});