* experimenting with redirects * cleanup developer.json * wip * clean console.log * progress * some progress * progress * much progress * debugging tests * hacky progress * ditch latest -> number redirects * minor * hacky progress * lots of progress * some small fixes * fix rendering tests * small fixes * progress * undo debugging * better * routing tests OK * more cleaning * unit tests * undoing lineending edit * undoing temporary debugging * don't ever set this.redirects on Page * cope with archived version redirects * adding code comments on the major if statements * address all feedback * update README about redirects * delete invalid test * fix feedback
65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
import cheerio from 'cheerio'
|
|
import supertest from 'supertest'
|
|
import createApp from '../../lib/app.js'
|
|
// This file makes an object with utility functions for re-use in
|
|
// multiple test files
|
|
|
|
const app = createApp()
|
|
|
|
const helpers = {}
|
|
|
|
const request = (method, route) => supertest(app)[method](route)
|
|
|
|
export const get = (helpers.get = async function (
|
|
route,
|
|
opts = { followRedirects: false, followAllRedirects: false, headers: {} }
|
|
) {
|
|
let res = opts.headers
|
|
? await request('get', route).set(opts.headers)
|
|
: await request('get', route)
|
|
// follow all redirects, or just follow one
|
|
if (opts.followAllRedirects && [301, 302].includes(res.status)) {
|
|
res = await helpers.get(res.headers.location, opts)
|
|
} else if (opts.followRedirects && [301, 302].includes(res.status)) {
|
|
res = await helpers.get(res.headers.location)
|
|
}
|
|
|
|
return res
|
|
})
|
|
|
|
export const head = (helpers.head = async function (route, opts = { followRedirects: false }) {
|
|
const res = await request('head', route).redirects(opts.followRedirects ? 10 : 0)
|
|
return res
|
|
})
|
|
|
|
export const post = (helpers.post = (route) => request('post', route))
|
|
|
|
export const getDOM = (helpers.getDOM = async function (
|
|
route,
|
|
{ headers, allow500s, allow404 } = { headers: undefined, allow500s: false, allow404: false }
|
|
) {
|
|
const res = await helpers.get(route, { followRedirects: true, headers })
|
|
if (!allow500s && res.status >= 500) {
|
|
throw new Error(`Server error (${res.status}) on ${route}`)
|
|
}
|
|
if (!allow404 && res.status === 404) {
|
|
throw new Error(`Page not found on ${route}`)
|
|
}
|
|
const $ = cheerio.load(res.text || '', { xmlMode: true })
|
|
$.res = Object.assign({}, res)
|
|
return $
|
|
})
|
|
|
|
// For use with the ?json query param
|
|
// e.g. await getJSON('/en?json=breadcrumbs')
|
|
export const getJSON = (helpers.getJSON = async function (route) {
|
|
const res = await helpers.get(route, { followRedirects: true })
|
|
if (res.status >= 500) {
|
|
throw new Error(`Server error (${res.status}) on ${route}`)
|
|
}
|
|
if (res.status >= 400) {
|
|
console.warn(`${res.status} on ${route} and the response might not be JSON`)
|
|
}
|
|
return JSON.parse(res.text)
|
|
})
|