const config = require('@/config').value; const got = require('@/utils/got'); const wait = require('@/utils/wait'); let cacheIndex = 0; module.exports = async (ctx) => { if (ctx.params.id === 'error') { throw Error('Error test'); } if (ctx.params.id === 'httperror') { await got({ method: 'get', url: `https://google.com/404`, }); } let item = []; if (ctx.params.id === 'filter') { item = [ { title: 'Filter Title1', description: 'Description1', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/-1`, author: `DIYgod0`, category: ['Category0', 'Category1'], }, { title: 'Filter Title2', description: 'Description2', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, category: ['Category0', 'Category1', 'Category2'], }, { title: 'Filter Title3', description: 'Description3', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/1`, author: `DIYgod0`, category: 'Category3', }, ]; } else if (ctx.params.id === 'long') { item.push({ title: `Long Title `.repeat(50), description: `Long Description `.repeat(10), pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, }); } else if (ctx.params.id === 'cache') { const description = await ctx.cache.tryGet( 'test', () => ({ text: `Cache${++cacheIndex}`, }), config.cache.routeExpire * 2 ); item.push({ title: 'Cache Title', description: description.text, pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, }); } else if (ctx.params.id === 'refreshCache') { let refresh = await ctx.cache.get('refreshCache'); let noRefresh = await ctx.cache.get('noRefreshCache', false); if (!refresh) { refresh = '0'; await ctx.cache.set('refreshCache', '1'); } if (!noRefresh) { noRefresh = '0'; await ctx.cache.set('noRefreshCache', '1', undefined); } item.push({ title: 'Cache Title', description: refresh + ' ' + noRefresh, pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, }); } else if (ctx.params.id === 'cacheUrlKey') { const description = await ctx.cache.tryGet( new URL('https://rsshub.app'), () => ({ text: `Cache${++cacheIndex}`, }), config.cache.routeExpire * 2 ); item.push({ title: 'Cache Title', description: description.text, pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, }); } else if (ctx.params.id === 'complicated') { item.push({ title: `Complicated Title`, description: ` `, pubDate: new Date(`2019-3-1`).toUTCString(), link: `//mock.com/DIYgod/RSSHub`, author: `DIYgod`, }); item.push({ title: `Complicated Title`, description: ` `, pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://mock.com/DIYgod/RSSHub`, author: `DIYgod`, }); } else if (ctx.params.id === 'multimedia') { item.push({ title: `Multimedia Title`, description: ` `, pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://mock.com/DIYgod/RSSHub`, author: `DIYgod`, }); } else if (ctx.params.id === 'sort') { item.push({ title: `Sort Title 0`, link: `https://github.com/DIYgod/RSSHub/issues/s1`, author: `DIYgod0`, }); item.push({ title: `Sort Title 1`, link: `https://github.com/DIYgod/RSSHub/issues/s1`, author: `DIYgod0`, }); item.push({ title: `Sort Title 2`, link: `https://github.com/DIYgod/RSSHub/issues/s2`, pubDate: new Date(1546272000000 - 10 * 10 * 1000).toUTCString(), author: `DIYgod0`, }); item.push({ title: `Sort Title 3`, link: `https://github.com/DIYgod/RSSHub/issues/s3`, pubDate: new Date(1546272000000).toUTCString(), author: `DIYgod0`, }); } else if (ctx.params.id === 'mess') { item.push({ title: `Mess Title`, link: `/DIYgod/RSSHub/issues/0`, pubDate: 1546272000000, author: `DIYgod0`, }); } else if (ctx.params.id === 'opencc') { item.push({ title: '小可愛', description: '宇宙無敵', link: `/DIYgod/RSSHub/issues/0`, pubDate: new Date(1546272000000).toUTCString(), author: `DIYgod0`, }); } else if (ctx.params.id === 'json') { item.push( { title: 'Title0', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/-3`, }, { title: 'Title1', description: 'Description1', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/-2`, author: `DIYgod0 `, category: 'Category0', }, { title: 'Title2 HTML in description', description: 'RSSHub', pubDate: new Date(`2019-3-1`).toUTCString(), updated: new Date(`2019-3-2`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/-1`, author: [{ name: ' DIYgod1' }, { name: 'DIYgod2 ' }], category: ['Category0', 'Category1'], }, { title: 'Title3 HTML in content', content: { html: 'DIYgod/RSSHub', }, pubDate: new Date(`2019-3-1`).toUTCString(), updated: new Date(`2019-3-2`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/0`, author: [{ name: ' DIYgod3' }, { name: 'DIYgod4 ' }, { name: 'DIYgod5 ' }], category: ['Category1'], enclosure_url: 'https://github.com/DIYgod/RSSHub/issues/0', enclosure_type: 'image/jpeg', enclosure_length: 3661, itunes_duration: 36610, }, { title: 'Title4 author is null', pubDate: new Date(`2019-3-1`).toUTCString(), link: `https://github.com/DIYgod/RSSHub/pull/11555`, author: null, } ); } for (let i = 1; i < 6; i++) { item.push({ title: `Title${i}`, description: `Description${i}`, pubDate: new Date((ctx.params.id === 'current_time' ? new Date() : 1546272000000) - i * 10 * 1000).toUTCString(), link: `https://github.com/DIYgod/RSSHub/issues/${i}`, author: `DIYgod${i}`, }); } if (ctx.params.id === 'empty') { item = null; } if (ctx.params.id === 'allow_empty') { item = null; } if (ctx.params.id === 'enclosure') { item = [ { title: '', link: 'https://github.com/DIYgod/RSSHub/issues/1', enclosure_url: 'https://github.com/DIYgod/RSSHub/issues/1', enclosure_length: 3661, itunes_duration: 36610, }, ]; } if (ctx.params.id === 'slow') { await wait(1000); } if (ctx.query.mode === 'fulltext') { item = [ { title: '', link: 'https://m.thepaper.cn/newsDetail_forward_4059298', }, ]; } ctx.state.data = { title: `Test ${ctx.params.id}`, itunes_author: ctx.params.id === 'enclosure' ? 'DIYgod' : null, link: 'https://github.com/DIYgod/RSSHub', item, allowEmpty: ctx.params.id === 'allow_empty', description: ctx.params.id === 'complicated' ? '' : ctx.params.id === 'multimedia' ? '' : 'A test route for RSSHub', }; };