Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

supertest.js 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  1. // This file exports an object with utility functions for re-use in
  2. // multiple test files
  3. const cheerio = require('cheerio')
  4. const supertest = require('supertest')
  5. const app = require('../../lib/app')
  6. const helpers = {}
  7. const request = (method, route) => supertest(app)[method](route)
  8. helpers.get = async function (route, opts = { followRedirects: false, followAllRedirects: false, headers: {} }) {
  9. let res = (opts.headers) ? await request('get', route).set(opts.headers) : await request('get', route)
  10. // follow all redirects, or just follow one
  11. if (opts.followAllRedirects && [301, 302].includes(res.status)) {
  12. res = await helpers.get(res.headers.location, opts)
  13. } else if (opts.followRedirects && [301, 302].includes(res.status)) {
  14. res = await helpers.get(res.headers.location)
  15. }
  16. return res
  17. }
  18. helpers.head = async function (route, opts = { followRedirects: false }) {
  19. const res = await request('head', route).redirects(opts.followRedirects ? 10 : 0)
  20. return res
  21. }
  22. helpers.post = route => request('post', route)
  23. helpers.getDOM = async function (route, headers) {
  24. const res = await helpers.get(route, { followRedirects: true, headers })
  25. const $ = cheerio.load((res.text || ''), { xmlMode: true })
  26. $.res = Object.assign({}, res)
  27. return $
  28. }
  29. // For use with the ?json query param
  30. // e.g. await getJSON('/en?json=breadcrumbs')
  31. helpers.getJSON = async function (route) {
  32. const res = await helpers.get(route, { followRedirects: true })
  33. return JSON.parse(res.text)
  34. }
  35. module.exports = helpers
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...