|
| 1 | +var FeedParser = require('..'); |
| 2 | + |
| 3 | +describe('async iterator usage', function () { |
| 4 | + it('should work as an async iterator', async function () { |
| 5 | + var feedparser = new FeedParser(); |
| 6 | + var feed = __dirname + '/feeds/rss2sample.xml'; |
| 7 | + var items = []; |
| 8 | + |
| 9 | + fs.createReadStream(feed).pipe(feedparser); |
| 10 | + |
| 11 | + for await (const item of feedparser) { |
| 12 | + items.push(item); |
| 13 | + } |
| 14 | + |
| 15 | + assert.equal(items.length, 4); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should surface errors via try/catch', async function () { |
| 19 | + var feedparser = new FeedParser(); |
| 20 | + var feed = __dirname + '/feeds/notafeed.html'; |
| 21 | + fs.createReadStream(feed).pipe(feedparser); |
| 22 | + |
| 23 | + var caught = null; |
| 24 | + try { |
| 25 | + for await (const item of feedparser) {} // eslint-disable-line no-empty, no-unused-vars |
| 26 | + } catch (err) { |
| 27 | + caught = err; |
| 28 | + } |
| 29 | + |
| 30 | + assert.ok(caught instanceof Error); |
| 31 | + assert.equal(caught.message, 'Not a feed'); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('resume_saxerror behavior', function () { |
| 35 | + var feed = __dirname + '/feeds/saxerror.xml'; |
| 36 | + |
| 37 | + it('should continue iterating past SAX errors by default (resume_saxerror: true)', async function () { |
| 38 | + var feedparser = new FeedParser({ strict: true }); |
| 39 | + fs.createReadStream(feed).pipe(feedparser); |
| 40 | + var items = []; |
| 41 | + |
| 42 | + for await (const item of feedparser) { |
| 43 | + items.push(item.title); |
| 44 | + } |
| 45 | + |
| 46 | + assert.equal(items.length, 3); |
| 47 | + assert.deepEqual(items, ['Good Item', 'Bad Item', 'Item After Error']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw on SAX errors when (resume_saxerror: false)', async function () { |
| 51 | + var feedparser = new FeedParser({ strict: true, resume_saxerror: false }); |
| 52 | + fs.createReadStream(feed).pipe(feedparser); |
| 53 | + var items = []; |
| 54 | + |
| 55 | + var caught = null; |
| 56 | + try { |
| 57 | + for await (const item of feedparser) { |
| 58 | + items.push(item.title); |
| 59 | + } |
| 60 | + } catch (err) { |
| 61 | + caught = err; |
| 62 | + } |
| 63 | + |
| 64 | + assert.ok(caught instanceof Error); |
| 65 | + assert.equal(items.length, 0); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments