File: test-onelineproxy.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 (61 lines) | stat: -rw-r--r-- 1,352 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
56
57
58
59
60
61
'use strict'

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

var server = http.createServer(function (req, resp) {
  resp.statusCode = 200
  if (req.url === '/get') {
    assert.equal(req.method, 'GET')
    resp.write('content')
    resp.end()
    return
  }
  if (req.url === '/put') {
    var x = ''
    assert.equal(req.method, 'PUT')
    req.on('data', function (chunk) {
      x += chunk
    })
    req.on('end', function () {
      assert.equal(x, 'content')
      resp.write('success')
      resp.end()
    })
    return
  }
  if (req.url === '/proxy') {
    assert.equal(req.method, 'PUT')
    req.pipe(request(server.url + '/put')).pipe(resp)
    return
  }
  if (req.url === '/test') {
    request(server.url + '/get').pipe(request.put(server.url + '/proxy')).pipe(resp)
    return
  }
  throw new Error('Unknown url', req.url)
})

tape('setup', function (t) {
  server.listen(0, function () {
    server.url = 'http://localhost:' + this.address().port
    t.end()
  })
})

tape('chained one-line proxying', function (t) {
  request(server.url + '/test', function (err, res, body) {
    t.equal(err, null)
    t.equal(res.statusCode, 200)
    t.equal(body, 'success')
    t.end()
  })
})

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