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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
var common = require('../common');
var assert = require('assert');
var http = require('http');
// Simple test of Node's HTTP Client mutable headers
// OutgoingMessage.prototype.setHeader(name, value)
// OutgoingMessage.prototype.getHeader(name)
// OutgoingMessage.prototype.removeHeader(name, value)
// ServerResponse.prototype.statusCode
// <ClientRequest>.method
// <ClientRequest>.path
var testsComplete = 0;
var test = 'headers';
var content = 'hello world\n';
var cookies = [
'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT',
'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
];
var s = http.createServer(function(req, res) {
switch (test) {
case 'headers':
assert.throws(function() { res.setHeader() });
assert.throws(function() { res.setHeader('someHeader') });
assert.throws(function() { res.getHeader() });
assert.throws(function() { res.removeHeader() });
res.setHeader('x-test-header', 'testing');
res.setHeader('X-TEST-HEADER2', 'testing');
res.setHeader('set-cookie', cookies);
res.setHeader('x-test-array-header', [1, 2, 3]);
var val1 = res.getHeader('x-test-header');
var val2 = res.getHeader('x-test-header2');
assert.equal(val1, 'testing');
assert.equal(val2, 'testing');
res.removeHeader('x-test-header2');
break;
case 'contentLength':
res.setHeader('content-length', content.length);
assert.equal(content.length, res.getHeader('Content-Length'));
break;
case 'transferEncoding':
res.setHeader('transfer-encoding', 'chunked');
assert.equal(res.getHeader('Transfer-Encoding'), 'chunked');
break;
case 'writeHead':
res.statusCode = 404;
res.setHeader('x-foo', 'keyboard cat');
res.writeHead(200, { 'x-foo': 'bar', 'x-bar': 'baz' });
break;
}
res.statusCode = 201;
res.end(content);
});
s.listen(common.PORT, nextTest);
function nextTest() {
if (test === 'end') {
return s.close();
}
var bufferedResponse = '';
http.get({ port: common.PORT }, function(response) {
console.log('TEST: ' + test);
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ');
console.dir(response.headers);
switch (test) {
case 'headers':
assert.equal(response.statusCode, 201);
assert.equal(response.headers['x-test-header'],
'testing');
assert.equal(response.headers['x-test-array-header'],
[1, 2, 3].join(', '));
assert.deepEqual(cookies,
response.headers['set-cookie']);
assert.equal(response.headers['x-test-header2'] !== undefined, false);
// Make the next request
test = 'contentLength';
console.log('foobar');
break;
case 'contentLength':
assert.equal(response.headers['content-length'], content.length);
test = 'transferEncoding';
break;
case 'transferEncoding':
assert.equal(response.headers['transfer-encoding'], 'chunked');
test = 'writeHead';
break;
case 'writeHead':
assert.equal(response.headers['x-foo'], 'bar');
assert.equal(response.headers['x-bar'], 'baz');
assert.equal(200, response.statusCode);
test = 'end';
break;
default:
throw Error('?');
}
response.setEncoding('utf8');
response.on('data', function(s) {
bufferedResponse += s;
});
response.on('end', function() {
assert.equal(content, bufferedResponse);
testsComplete++;
nextTest();
});
});
}
process.on('exit', function() {
assert.equal(4, testsComplete);
});
|