-
-
Notifications
You must be signed in to change notification settings - Fork 22.9k
Expand file tree
/
Copy pathres.set.js
More file actions
162 lines (128 loc) · 3.82 KB
/
res.set.js
File metadata and controls
162 lines (128 loc) · 3.82 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
'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)
})
})
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);
})
})
describe(".set() edge cases", function () {
it("should allow empty string header values", function (done) {
var app = express();
app.use(function (req, res) {
res.set("X-Empty", "").end();
});
request(app).get("/").expect("X-Empty", "").end(done);
});
it("should treat header field names as case-insensitive", function (done) {
var app = express();
app.use(function (req, res) {
res.set("content-type", "text/plain");
res.end();
});
request(app)
.get("/")
.expect("Content-Type", "text/plain; charset=utf-8")
.end(done);
});
it("should overwrite an existing header value", function (done) {
var app = express();
app.use(function (req, res) {
res.set("X-Test", "a");
res.set("X-Test", "b");
res.end();
});
request(app).get("/").expect("X-Test", "b").end(done);
});
});
})