File: test-piped-redirect.js

package info (click to toggle)
node-request 2.88.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 960 kB
  • sloc: javascript: 9,687; sh: 46; makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download | duplicates (3)
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
51
52
53
54
55
'use strict'

var http = require('http')
var request = require('../index')
var tape = require('tape')

var port1
var port2

var s1 = http.createServer(function (req, resp) {
  if (req.url === '/original') {
    resp.writeHeader(302, {
      'location': '/redirected'
    })
    resp.end()
  } else if (req.url === '/redirected') {
    resp.writeHeader(200, {
      'content-type': 'text/plain'
    })
    resp.write('OK')
    resp.end()
  }
})

var s2 = http.createServer(function (req, resp) {
  var x = request('http://localhost:' + port1 + '/original')
  req.pipe(x)
  x.pipe(resp)
})

tape('setup', function (t) {
  s1.listen(0, function () {
    port1 = this.address().port
    s2.listen(0, function () {
      port2 = this.address().port
      t.end()
    })
  })
})

tape('piped redirect', function (t) {
  request('http://localhost:' + port2 + '/original', function (err, res, body) {
    t.equal(err, null)
    t.equal(body, 'OK')
    t.end()
  })
})

tape('cleanup', function (t) {
  s1.close(function () {
    s2.close(function () {
      t.end()
    })
  })
})