/
test-unix.js
74 lines (64 loc) 路 1.91 KB
/
test-unix.js
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')
, http = require('http')
, fs = require('fs')
, rimraf = require('rimraf')
, assert = require('assert')
, tape = require('tape')
, url = require('url')
var rawPath = [null, 'raw', 'path'].join('/')
, queryPath = [null, 'query', 'path'].join('/')
, searchString = '?foo=bar'
, socket = [__dirname, 'tmp-socket'].join('/')
, expectedBody = 'connected'
, 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()
})
})
})