forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.set.js
123 lines (98 loc) · 2.88 KB
/
res.set.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
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
var express = require('..');
var request = require('supertest');
describe('res', function(){
describe('.set(field, value)', function(){
it('should set the response header field', function(done){
var app = express();
app.use(function(req, res){
res.set('Content-Type', 'text/x-foo; charset=utf-8').end();
});
request(app)
.get('/')
.expect('Content-Type', 'text/x-foo; charset=utf-8')
.end(done);
})
it('should coerce to a string', function (done) {
var app = express();
app.use(function (req, res) {
res.set('X-Number', 123);
res.end(typeof res.get('X-Number'));
});
request(app)
.get('/')
.expect('X-Number', '123')
.expect(200, 'string', done);
})
})
describe('.set(field, values)', function(){
it('should set multiple response header fields', function(done){
var app = express();
app.use(function(req, res){
res.set('Set-Cookie', ["type=ninja", "language=javascript"]);
res.send(res.get('Set-Cookie'));
});
request(app)
.get('/')
.expect('["type=ninja","language=javascript"]', done);
})
it('should coerce to an array of strings', function (done) {
var app = express();
app.use(function (req, res) {
res.set('X-Numbers', [123, 456]);
res.end(JSON.stringify(res.get('X-Numbers')));
});
request(app)
.get('/')
.expect('X-Numbers', '123, 456')
.expect(200, '["123","456"]', done);
})
it('should not set a charset of one is already set', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'text/html; charset=lol');
res.end();
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=lol')
.expect(200, done);
})
it('should throw when Content-Type is an array', function (done) {
var app = express()
app.use(function (req, res) {
res.set('Content-Type', ['text/html'])
res.end()
});
request(app)
.get('/')
.expect(500, /TypeError: Content-Type cannot be set to an Array/, done)
})
})
describe('.set(object)', function(){
it('should set multiple fields', function(done){
var app = express();
app.use(function(req, res){
res.set({
'X-Foo': 'bar',
'X-Bar': 'baz'
}).end();
});
request(app)
.get('/')
.expect('X-Foo', 'bar')
.expect('X-Bar', 'baz')
.end(done);
})
it('should coerce to a string', function (done) {
var app = express();
app.use(function (req, res) {
res.set({ 'X-Number': 123 });
res.end(typeof res.get('X-Number'));
});
request(app)
.get('/')
.expect('X-Number', '123')
.expect(200, 'string', done);
})
})
})