Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: content-type is undefined #187

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const fetch = PCancelable.fn(
return {
headers: res.headers,
html:
res.headers['content-type'].startsWith('text/html') ||
(res.headers['content-type'] ?? '').startsWith('text/html') ||
!isMediaUrl(url)
? await toEncode(res.body, res.headers['content-type'])
: res.body,
Expand Down
18 changes: 1 addition & 17 deletions test/encoding.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
'use strict'
'use strict'

const { default: listen } = require('async-listen')
const { createServer } = require('http')
const { promisify } = require('util')
const test = require('ava')

const { fixture, initBrowserless } = require('./util')
const { runFixtureServer, initBrowserless } = require('./util')
const getHTML = require('..')

const closeServer = server => promisify(server.close)

const runFixtureServer = async (t, fixturePath) => {
const server = createServer((_, res) => {
res.setHeader('content-type', 'text/html')
res.end(fixture(fixturePath))
})
const url = await listen(server)
t.teardown(() => closeServer(server))
return url
}

const getBrowserless = initBrowserless(test)

;[false, true].forEach(prerender => {
Expand Down
13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PCancelable = require('p-cancelable')
const cheerio = require('cheerio')
const test = require('ava')

const { initBrowserless, prettyHtml } = require('./util')
const { initBrowserless, runServer, prettyHtml } = require('./util')
const getHTML = require('..')

const getBrowserless = initBrowserless(test)
Expand Down Expand Up @@ -140,6 +140,17 @@ test('from big image URL', async t => {
t.is(stats.mode, 'fetch')
})

test('from URL with no content type', async t => {
const targetUrl = await runServer(t, (_, res) => {
res.end('<!doctype html><title>.</title>')
})
const { stats } = await getHTML(targetUrl, {
getBrowserless,
prerender: false
})
t.is(stats.mode, 'fetch')
})

test('from image URL that returns HTML markup', async t => {
const targetUrl =
'https://www.europapress.es/chance/gente/%7B%7BrutaFoto%7D%7D%7B%7Bfechor%7D%7D_%7B%7BanchoFoto%7D%7D_%7B%7BaltoFoto%7D%7D%7B%7BversionFoto%7D%7D.jpg'
Expand Down
59 changes: 59 additions & 0 deletions test/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict'

const test = require('ava')

const { initBrowserless, runServer, prettyHtml } = require('./util')
const getHTML = require('..')

const getBrowserless = initBrowserless(test)

;[false, true].forEach(prerender => {
const mode = prerender ? 'prerender' : 'fetch'
test(`${mode} » as string`, async t => {
const url = await runServer(t, (_, res) =>
res.end('<!doctype html><title>.</title>')
)
const { html } = await getHTML(url.toString(), {
getBrowserless,
prerender,
puppeteerOpts: { adblock: false, animations: true }
})

t.is(
prettyHtml(html),
prettyHtml(`<!DOCTYPE html>
<html>
<head>
<title>.</title>
<meta name="date" content="{DATE}">
<link rel="canonical" href="${url.toString()}">
</head>
<body></body>
</html>`)
)
})

test(`${mode} » as WHATWG URL object`, async t => {
const url = await runServer(t, (_, res) =>
res.end('<!doctype html><title>.</title>')
)
const { html } = await getHTML(url, {
getBrowserless,
prerender,
puppeteerOpts: { adblock: false, animations: true }
})

t.is(
prettyHtml(html),
prettyHtml(`<!DOCTYPE html>
<html>
<head>
<title>.</title>
<meta name="date" content="{DATE}">
<link rel="canonical" href="${url.toString()}">
</head>
<body></body>
</html>`)
)
})
})
29 changes: 25 additions & 4 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'

const { default: listen } = require('async-listen')
const createBrowserless = require('browserless')
const dateRegex = require('regex-iso-date')
const { createServer } = require('http')
const { promisify } = require('util')
const pretty = require('pretty')
const path = require('path')
const fs = require('fs')

const closeServer = server => promisify(server.close)

const fixture = name =>
fs.readFileSync(path.join(__dirname, '/fixtures/', name))

Expand All @@ -15,9 +20,25 @@ const initBrowserless = test => {
return () => browserlessFactory.createContext()
}

const runServer = async (t, fn) => {
const server = createServer(fn)
const url = await listen(server)
t.teardown(() => closeServer(server))
return url
}

const runFixtureServer = async (t, fixturePath) =>
runServer(t, (_, res) => {
res.setHeader('content-type', 'text/html')
res.end(fixture(fixturePath))
})

const prettyHtml = html =>
pretty(html, { ocd: true }).replace(dateRegex(), '{DATE}')

module.exports = {
prettyHtml: html =>
pretty(html, { ocd: true }).replace(dateRegex(), '{DATE}'),
fixture,
initBrowserless
initBrowserless,
prettyHtml,
runFixtureServer,
runServer
}