/* 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', 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', custom: [ {'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: [ {'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' + '2013 Dylan Green' + 'Dylan Green' + 'Dylan Green' + 'http://example.com/rss/docs.html' + '' + '60' + 'Category 1' + 'Category 2' + 'Category 3' + '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(); }); });