File: test-unix.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 (74 lines) | stat: -rw-r--r-- 1,961 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
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict'

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

var rawPath = [null, 'raw', 'path'].join('/')
var queryPath = [null, 'query', 'path'].join('/')
var searchString = '?foo=bar'
var socket = [__dirname, 'tmp-socket'].join('/')
var expectedBody = 'connected'
var statusCode = 200

rimraf.sync(socket)

var s = http.createServer(function (req, res) {
  var incomingUrl = url.parse(req.url)
  switch (incomingUrl.pathname) {
    case rawPath:
      assert.equal(incomingUrl.pathname, rawPath, 'requested path is sent to server')
      break

    case queryPath:
      assert.equal(incomingUrl.pathname, queryPath, 'requested path is sent to server')
      assert.equal(incomingUrl.search, searchString, 'query string is sent to server')
      break

    default:
      assert(false, 'A valid path was requested')
  }
  res.statusCode = statusCode
  res.end(expectedBody)
})

tape('setup', function (t) {
  s.listen(socket, function () {
    t.end()
  })
})

tape('unix socket connection', function (t) {
  request('http://unix:' + socket + ':' + rawPath, function (err, res, body) {
    t.equal(err, null, 'no error in connection')
    t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response')
    t.equal(body, expectedBody, 'expected response body is received')
    t.end()
  })
})

tape('unix socket connection with qs', function (t) {
  request({
    uri: 'http://unix:' + socket + ':' + queryPath,
    qs: {
      foo: 'bar'
    }
  }, function (err, res, body) {
    t.equal(err, null, 'no error in connection')
    t.equal(res.statusCode, statusCode, 'got HTTP 200 OK response')
    t.equal(body, expectedBody, 'expected response body is received')
    t.end()
  })
})

tape('cleanup', function (t) {
  s.close(function () {
    fs.unlink(socket, function () {
      t.end()
    })
  })
})