forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.set.js
More file actions
199 lines (159 loc) · 4.76 KB
/
res.set.js
File metadata and controls
199 lines (159 loc) · 4.76 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict'
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)
})
it('should set charset for known Content-Type', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'text/html');
res.end();
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, done);
})
it('should add charset for shorthand Content-Type', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'html');
res.end();
});
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, done);
})
it('should keep original value when Content-Type is unknown', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'some-custom-type');
res.end();
});
request(app)
.get('/')
.expect('Content-Type', 'some-custom-type')
.expect(200, done);
})
it('should not set header to false for unrecognized Content-Type', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'unknown-type');
var value = res.get('Content-Type');
res.end(value);
});
request(app)
.get('/')
.expect(function (res) {
if (res.text === 'false') {
throw new Error('Content-Type was set to literal string "false"');
}
})
.expect(200, done);
})
it('should preserve full MIME type with slash in Content-Type', function (done) {
var app = express();
app.use(function (req, res) {
res.set('Content-Type', 'application/vnd.custom+json');
res.end();
});
request(app)
.get('/')
.expect('Content-Type', 'application/vnd.custom+json')
.expect(200, 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);
})
})
})