summaryrefslogtreecommitdiffstats
path: root/test/redirect-pipeline.js
blob: e4be837c6e9940f62bd87c00c86bc1fae70206bf (plain)
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
46
47
48
49
50
'use strict'

const t = require('tap')
const { pipeline: undiciPipeline } = require('..')
const { pipeline: streamPipelineCb } = require('stream')
const { promisify } = require('util')
const { createReadable, createWritable } = require('./utils/stream')
const { startRedirectingServer } = require('./utils/redirecting-servers')

const streamPipeline = promisify(streamPipelineCb)

t.test('should not follow redirection by default if not using RedirectAgent', async t => {
  t.plan(3)

  const body = []
  const serverRoot = await startRedirectingServer(t)

  await streamPipeline(
    createReadable('REQUEST'),
    undiciPipeline(`http://${serverRoot}/`, {}, ({ statusCode, headers, body }) => {
      t.equal(statusCode, 302)
      t.equal(headers.location, `http://${serverRoot}/302/1`)

      return body
    }),
    createWritable(body)
  )

  t.equal(body.length, 0)
})

t.test('should not follow redirects when using RedirectAgent within pipeline', async t => {
  t.plan(3)

  const body = []
  const serverRoot = await startRedirectingServer(t)

  await streamPipeline(
    createReadable('REQUEST'),
    undiciPipeline(`http://${serverRoot}/`, { maxRedirections: 1 }, ({ statusCode, headers, body }) => {
      t.equal(statusCode, 302)
      t.equal(headers.location, `http://${serverRoot}/302/1`)

      return body
    }),
    createWritable(body)
  )

  t.equal(body.length, 0)
})