summaryrefslogtreecommitdiff
path: root/node_modules/request/tests/test-redirect.js
blob: dfa086dbf6d67b5e966a1b1f784b70ff82e87262 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var server = require('./server')
  , assert = require('assert')
  , request = require('../main.js')

var s = server.createServer()

s.listen(s.port, function () {
  var server = 'http://localhost:' + s.port;
  var hits = {}
  var passed = 0;

  bouncer(301, 'temp')
  bouncer(302, 'perm')
  bouncer(302, 'nope')

  function bouncer(code, label) {
    var landing = label+'_landing';

    s.on('/'+label, function (req, res) {
      hits[label] = true;
      res.writeHead(code, {'location':server + '/'+landing})
      res.end()
    })

    s.on('/'+landing, function (req, res) {
      hits[landing] = true;
      res.writeHead(200)
      res.end(landing)
    })
  }

  // Permanent bounce
  request(server+'/perm', function (er, res, body) {
    try {
      assert.ok(hits.perm, 'Original request is to /perm')
      assert.ok(hits.perm_landing, 'Forward to permanent landing URL')
      assert.equal(body, 'perm_landing', 'Got permanent landing content')
      passed += 1
    } finally {
      done()
    }
  })

  // Temporary bounce
  request(server+'/temp', function (er, res, body) {
    try {
      assert.ok(hits.temp, 'Original request is to /temp')
      assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
      assert.equal(body, 'temp_landing', 'Got temporary landing content')
      passed += 1
    } finally {
      done()
    }
  })

  // Prevent bouncing.
  request({uri:server+'/nope', followRedirect:false}, function (er, res, body) {
    try {
      assert.ok(hits.nope, 'Original request to /nope')
      assert.ok(!hits.nope_landing, 'No chasing the redirect')
      assert.equal(res.statusCode, 302, 'Response is the bounce itself')
      passed += 1
    } finally {
      done()
    }
  })

  var reqs_done = 0;
  function done() {
    reqs_done += 1;
    if(reqs_done == 3) {
      console.log(passed + ' tests passed.')
      s.close()
    }
  }
})