Skip to content

Commit 2b3899d

Browse files
author
Matt Mason
committed
node case insensitive headers
1 parent 55572ff commit 2b3899d

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

WebJobs.Script.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
<BuildConfiguration Condition=" $(Configuration) != '' ">$(Configuration)</BuildConfiguration>
179179
</PropertyGroup>
180180

181-
<Target Name="MochaTest" DependsOnTargets="Build" AfterTargets="UnitTest">
181+
<Target Name="MochaTest" DependsOnTargets="TestBuild" AfterTargets="UnitTest">
182182
<Exec Command="npm i mocha chai $(AppveyorModules)" />
183183
<Exec Command=".\node_modules\.bin\mocha .\test\WebJobs.Script.Tests\TestScripts\Node\*.tests.js $(AppveyorReporter) --config=$(BuildConfiguration)" />
184184
</Target>

src/WebJobs.Script/Content/Script/http/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
module.exports = (context) => {
55
var req = context.req;
6-
req.get = (field) => req.headers[field];
6+
req.get = (field) => req.headers[field.toLowerCase()];
77
return req;
88
};

src/WebJobs.Script/Content/Script/http/response.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,73 @@ module.exports = (context) => {
66
headers: {},
77
body: undefined,
88

9+
// functions methods
10+
raw: (body) => {
11+
res.isRaw = true;
12+
return res.send(body);
13+
},
14+
15+
// node httpResponse methods
16+
setHeader: (field, val) => {
17+
res.headers[field.toLowerCase()] = val;
18+
return res;
19+
},
20+
21+
getHeader: (field) => {
22+
return res.headers[field.toLowerCase()];
23+
},
24+
25+
removeHeader: (field) => {
26+
delete res.headers[field.toLowerCase()];
27+
return res;
28+
},
29+
930
end: (body) => {
1031
if (body !== undefined) {
1132
res.body = body;
1233
}
34+
setContentType(res);
1335
context.done();
1436
return res;
1537
},
1638

39+
// express methods
1740
status: (statusCode) => {
1841
res.statusCode = statusCode;
1942
return res;
2043
},
2144

22-
set: (field, val) => {
23-
res.headers[field] = val;
24-
return res;
25-
},
26-
2745
sendStatus: (statusCode) => {
2846
return res.status(statusCode)
2947
.end();
3048
},
3149

3250
type: (type) => {
33-
return res.set('Content-Type', type);
51+
return res.set('content-type', type);
3452
},
3553

3654
json: (body) => {
3755
return res.type('application/json')
3856
.send(body);
39-
},
40-
41-
raw: (body) => {
42-
res.isRaw = true;
43-
return res.send(body);
44-
},
45-
46-
get: (field) => {
47-
return res.headers[field]
4857
}
4958
};
5059

5160
res.send = res.end;
52-
res.header = res.set;
61+
res.header = res.set = res.setHeader;
62+
res.get = res.getHeader;
5363

5464
return res;
5565
};
66+
67+
function setContentType(res) {
68+
if (res.body !== undefined) {
69+
if (res.get('content-type')) {
70+
// use user defined content type, if exists
71+
return;
72+
}
73+
74+
if (Buffer.isBuffer(res.body)) {
75+
res.type('application/octet-stream');
76+
}
77+
}
78+
}

test/WebJobs.Script.Tests/TestScripts/Node/functions.tests.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ describe('http', () => {
2222
res = response(context);
2323
});
2424

25+
it('setHeader', () => {
26+
res.setHeader('TEST', 'val');
27+
expect(res.headers.test).to.equal('val');
28+
});
29+
30+
it('getHeader', () => {
31+
res.setHeader('TEST', 'val');
32+
expect(res.getHeader('TeSt')).to.equal('val');
33+
});
34+
35+
it('removeHeader', () => {
36+
res.setHeader('TEST', 'val');
37+
res.removeHeader('teSt');
38+
expect(res.getHeader('test')).to.be.undefined;
39+
});
40+
2541
it('status', () => {
2642
res.status(200);
2743
expect(res.statusCode).to.equal(200);
@@ -46,6 +62,19 @@ describe('http', () => {
4662
expect(context.isDone).to.be.true;
4763
});
4864

65+
it('sends buffer', () => {
66+
res.send(new Buffer([0, 1]));
67+
expect(Buffer.isBuffer(res.body)).to.be.true;
68+
expect(res.get('content-type')).to.equal('application/octet-stream');
69+
});
70+
71+
it('send maintains set type', () => {
72+
res.send(new Buffer([0, 1]))
73+
.type('image/png');
74+
expect(Buffer.isBuffer(res.body)).to.be.true;
75+
expect(res.get('content-type')).to.equal('image/png');
76+
});
77+
4978
it('json', () => {
5079
res.json('test');
5180
expect(res.body).to.equal('test');
@@ -54,13 +83,13 @@ describe('http', () => {
5483
});
5584

5685
it('set', () => {
57-
res.set('header', 'val');
86+
res.set('Header', 'val');
5887
expect(res.headers.header).to.equal('val');
5988
expect(context.isDone).to.be.false;
6089
});
6190

6291
it('header', () => {
63-
res.header('header', 'val');
92+
res.header('Header', 'val');
6493
expect(res.headers.header).to.equal('val');
6594
expect(context.isDone).to.be.false;
6695
});
@@ -77,6 +106,12 @@ describe('http', () => {
77106
expect(context.isDone).to.be.false;
78107
});
79108

109+
it('get upper', () => {
110+
res.set('header', 'val');
111+
expect(res.get('HEADER')).to.equal('val');
112+
expect(context.isDone).to.be.false;
113+
});
114+
80115
it('raw', () => {
81116
res.raw('test');
82117
expect(res.body).to.equal('test');
@@ -100,8 +135,12 @@ describe('http', () => {
100135

101136
it('get', () => {
102137
expect(req.get('test')).to.equal('val');
103-
})
104-
});
138+
});
139+
140+
it('get upper', () => {
141+
expect(req.get('TEST')).to.equal('val');
142+
});
143+
});
105144
});
106145

107146
describe('functions', () => {
@@ -195,7 +234,6 @@ describe('functions', () => {
195234
});
196235

197236
func(context, () => {});
198-
199237
});
200238

201239
it('logs if promise and done', (done) => {

0 commit comments

Comments
 (0)