import { unfurl } from '../../src/' import nock from 'nock' test('should noop and not throw for wrong content type', async () => { nock('http://localhost') .get('/oembed/image.png') .reply(200, '', { 'Content-Type': 'image/png' }) nock('http://localhost') .get('/html/oembed-broken') .replyWithFile(200, __dirname + '/oembed-broken.html', { 'Content-Type': 'text/html' }) const result = await unfurl('http://localhost/html/oembed-broken') expect(result.oEmbed).toEqual(undefined) }) test('width/height should be numbers', async () => { nock('http://localhost') .get('/html/oembed') .replyWithFile(200, __dirname + '/oembed.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/json/oembed.json') .replyWithFile(200, __dirname + '/oembed.json', { 'Content-Type': 'application/json' }) const result: any = await unfurl('http://localhost/html/oembed') expect(result.oEmbed.width).toEqual(640) expect(result.oEmbed.height).toEqual(640) expect(result.oEmbed.thumbnails[0].width).toEqual(200) expect(result.oEmbed.thumbnails[0].height).toEqual(200) }) test('should decode entities in OEmbed URL', async () => { nock('http://localhost') .get('/html/oembed') .replyWithFile(200, __dirname + '/oembed-entities.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/oembed-service?format=json&file=/json/oembed.json') .replyWithFile(200, __dirname + '/oembed.json', { 'Content-Type': 'application/json' }) const result: any = await unfurl('http://localhost/html/oembed') expect(result.oEmbed.width).toEqual(640) expect(result.oEmbed.height).toEqual(640) expect(result.oEmbed.thumbnails[0].width).toEqual(200) expect(result.oEmbed.thumbnails[0].height).toEqual(200) }) test('should prefer fetching JSON oEmbed', async () => { nock('http://localhost') .get('/html/oembed-multi') .replyWithFile(200, __dirname + '/oembed-multi.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/json/oembed.json') .replyWithFile(200, __dirname + '/oembed.json', { 'Content-Type': 'application/json' }) const result: any = await unfurl('http://localhost/html/oembed-multi') const expected = { version: '1.0', provider_url: 'https://gfycat.com', provider_name: 'Gfycat', type: 'video', title: 'The creation of a marble sculpture', html: `
`, height: 640, width: 640, thumbnails: [ { url: 'https://gfycat.com/uk/gifs/detail/impressionablewaterloggedabalone', width: 200, height: 200 } ] } expect(result.oEmbed).toEqual(expected) }) test('should upgrade to HTTPS if needed', async () => { nock('http://localhost') .get('/html/oembed-http') .replyWithFile(200, __dirname + '/oembed-http.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/json/oembed.json') .replyWithFile(403, __dirname + '/oembed-error.json', { 'Content-Type': 'application/json' }) nock('https://localhost') .get('/json/oembed.json') .replyWithFile(200, __dirname + '/oembed.json', { 'Content-Type': 'application/json' }) const result: any = await unfurl('http://localhost/html/oembed-http') expect(result.oEmbed.version).toEqual('1.0') }) test('should build oEmbed from JSON', async () => { nock('http://localhost') .get('/html/oembed') .replyWithFile(200, __dirname + '/oembed.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/json/oembed.json') .replyWithFile(200, __dirname + '/oembed.json', { 'Content-Type': 'application/json' }) const result: any = await unfurl('http://localhost/html/oembed') const expected = { version: '1.0', provider_url: 'https://gfycat.com', provider_name: 'Gfycat', type: 'video', title: 'The creation of a marble sculpture', html: `
`, height: 640, width: 640, thumbnails: [ { url: 'https://gfycat.com/uk/gifs/detail/impressionablewaterloggedabalone', width: 200, height: 200 } ] } expect(result.oEmbed).toEqual(expected) }) test('should build oEmbed from XML', async () => { nock('http://localhost') .get('/html/oembed-xml') .replyWithFile(200, __dirname + '/oembed-xml.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/xml/oembed.xml') .replyWithFile(200, __dirname + '/oembed.xml', { 'Content-Type': 'text/xml' }) const result: any = await unfurl('http://localhost/html/oembed-xml') const expected = { html: '', author_name: 'EminemVEVO', author_url: 'https://www.youtube.com/user/EminemVEVO', height: 270, provider_name: 'YouTube', provider_url: 'https://www.youtube.com/', thumbnails: [ { height: 360, url: 'https://i.ytimg.com/vi/mvSItvjFE1c/hqdefault.jpg', width: 480 } ], title: 'Eminem - Lucky You ft. Joyner Lucas', type: 'video', version: '1.0', width: 480 } expect(result.oEmbed).toEqual(expected) }) test('should build oEmbed from XML with CDATA', async () => { nock('http://localhost') .get('/html/oembed-xml-cdata') .replyWithFile(200, __dirname + '/oembed-xml-cdata.html', { 'Content-Type': 'text/html' }) nock('http://localhost') .get('/xml/oembed-cdata.xml') .replyWithFile(200, __dirname + '/oembed-cdata.xml', { 'Content-Type': 'text/xml' }) const result: any = await unfurl('http://localhost/html/oembed-xml-cdata') const expected = { height: 400, title: "Bugle 179 - Playas gon play by The Bugle", type: "rich", version: "1.0", width: 100, html: '', } expect(result.oEmbed).toEqual(expected) })