1
0
mirror of synced 2025-12-30 03:01:36 -05:00
Files
docs/tests/helpers/e2etest.js
Kevin Heis 303d5190db Create a translations test suite (#30000)
* Scaffold files for migration

* Move user-agent into unit suite

* Nothing to move from browser suite

* Migrate tests to translations/content

* Migrate existing translation test to meta

* No graphql tests to migrate

* Migrate lint-translation-reporter

* Migrate lint-translation-reporter

* Remove languages-schema, unused

* Restore languages-schema

* Restore languages-schema

* Migrate rendering

* Migrate routing

* Migrate most of unit

* Remove dead files, comment out tests that aren't expected to work yet

* Migrate from get-redirect

* Migrate page and pages

* Migrate linting code

* Fix lint issues

* Found a few more

* Run prettier

* Move crowdin-config test and helper

* Update crowdin-config.js

* Remove translation linting, crowdin config lint, reduce file count

* Remove test that's been skipped for a year

* Restore linting with note to remove later

* Update lint-translation-reporter.js

* Clean up rendering suite

* Update rendering.js

* Remove excessive describe blocks

* Redirect tests

* Clean up unit

* Remove test that's never called

* Don't compare early access

* Rename test suites

* Update "content" tests

* Update files.js

* Update search.js

* Update files.js

* Update files.js
2022-08-25 12:38:03 -07:00

93 lines
2.5 KiB
JavaScript

import cheerio from 'cheerio'
import got from 'got'
import { omitBy, isUndefined } from 'lodash-es'
export async function get(
route,
opts = {
method: 'get',
body: undefined,
followRedirects: false,
followAllRedirects: false,
headers: {},
}
) {
const method = opts.method || 'get'
const fn = got[method]
if (!fn || typeof fn !== 'function') throw new Error(`No method function for '${method}'`)
const absURL = `http://localhost:4000${route}`
const xopts = omitBy(
{
body: opts.body,
headers: opts.headers,
retry: { limit: 0 },
throwHttpErrors: false,
followRedirect: opts.followAllRedirects || opts.followRedirects,
},
isUndefined
)
const res = await fn(absURL, xopts)
// follow all redirects, or just follow one
if (opts.followAllRedirects && [301, 302].includes(res.status)) {
// res = await get(res.headers.location, opts)
throw new Error('A')
} else if (opts.followRedirects && [301, 302].includes(res.status)) {
// res = await get(res.headers.location)
throw new Error('B')
}
const text = res.body
const status = res.statusCode
const headers = res.headers
return {
text,
status,
statusCode: status, // Legacy
headers,
header: headers, // Legacy
url: res.url,
}
}
export async function head(route, opts = { followRedirects: false }) {
const res = await get(route, { method: 'head', followRedirects: opts.followRedirects })
return res
}
export function post(route, opts) {
return get(route, Object.assign({}, opts, { method: 'post' }))
}
export async function getDOM(
route,
{ headers, allow500s, allow404 } = {
headers: undefined,
allow500s: false,
allow404: false,
}
) {
const res = await 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 async function getJSON(route, opts) {
const res = await get(route, { ...opts, 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)
}