Skip to content

Commit 6c8b860

Browse files
authored
refactor: use express recommended api (#727)
1 parent 7309bbc commit 6c8b860

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/utils/handleRangeHeaders.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import parseRange from 'range-parser';
33
export default function handleRangeHeaders(context, content, req, res) {
44
// assumes express API. For other servers, need to add logic to access
55
// alternative header APIs
6-
res.setHeader('Accept-Ranges', 'bytes');
6+
res.set('Accept-Ranges', 'bytes');
77

8-
if (req.headers.range) {
9-
const ranges = parseRange(content.length, req.headers.range);
8+
const range = req.get('range');
9+
10+
if (range) {
11+
const ranges = parseRange(content.length, range);
1012

1113
// unsatisfiable
1214
if (ranges === -1) {
13-
res.setHeader('Content-Range', `bytes */${content.length}`);
15+
res.set('Content-Range', `bytes */${content.length}`);
1416
// eslint-disable-next-line no-param-reassign
15-
res.statusCode = 416;
17+
res.status(416);
1618
} else if (ranges === -2) {
1719
// malformed header treated as regular response
1820
context.logger.error(
@@ -29,8 +31,8 @@ export default function handleRangeHeaders(context, content, req, res) {
2931

3032
// Content-Range
3133
// eslint-disable-next-line no-param-reassign
32-
res.statusCode = 206;
33-
res.setHeader(
34+
res.status(206);
35+
res.set(
3436
'Content-Range',
3537
`bytes ${ranges[0].start}-${ranges[0].end}/${length}`
3638
);

test/utils/handleRangeHeaders.test.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ describe('handleRangeHeaders', () => {
1717
headers: {
1818
range: 'bytes=1-4',
1919
},
20+
get(field) {
21+
return this.headers[field];
22+
},
2023
};
2124

2225
const res = {
23-
setHeader: jest.fn(),
26+
set: jest.fn(),
27+
status(statusCode) {
28+
this.statusCode = statusCode;
29+
},
2430
};
2531

2632
const contentRes = handleRangeHeaders(context, content, req, res);
2733
expect(contentRes).toEqual('bcde');
2834
expect(res.statusCode).toEqual(206);
29-
expect(res.setHeader.mock.calls).toMatchSnapshot();
35+
expect(res.set.mock.calls).toMatchSnapshot();
3036
});
3137

3238
it('should handle malformed range header', () => {
@@ -35,17 +41,23 @@ describe('handleRangeHeaders', () => {
3541
headers: {
3642
range: 'abc',
3743
},
44+
get(field) {
45+
return this.headers[field];
46+
},
3847
};
3948

4049
const res = {
41-
setHeader: jest.fn(),
50+
set: jest.fn(),
51+
status(statusCode) {
52+
this.statusCode = statusCode;
53+
},
4254
};
4355

4456
const contentRes = handleRangeHeaders(context, content, req, res);
4557
expect(contentRes).toEqual('abcdef');
4658
expect(context.logger.error.mock.calls).toMatchSnapshot();
4759
expect(res.statusCode).toBeUndefined();
48-
expect(res.setHeader.mock.calls).toMatchSnapshot();
60+
expect(res.set.mock.calls).toMatchSnapshot();
4961
});
5062

5163
it('should handle unsatisfiable range', () => {
@@ -54,16 +66,22 @@ describe('handleRangeHeaders', () => {
5466
headers: {
5567
range: 'bytes=10-20',
5668
},
69+
get(field) {
70+
return this.headers[field];
71+
},
5772
};
5873

5974
const res = {
60-
setHeader: jest.fn(),
75+
set: jest.fn(),
76+
status(statusCode) {
77+
this.statusCode = statusCode;
78+
},
6179
};
6280

6381
const contentRes = handleRangeHeaders(context, content, req, res);
6482
expect(contentRes).toEqual('abcdef');
6583
expect(res.statusCode).toEqual(416);
66-
expect(res.setHeader.mock.calls).toMatchSnapshot();
84+
expect(res.set.mock.calls).toMatchSnapshot();
6785
});
6886

6987
it('should handle multiple ranges', () => {
@@ -72,16 +90,22 @@ describe('handleRangeHeaders', () => {
7290
headers: {
7391
range: 'bytes=1-2,4-5',
7492
},
93+
get(field) {
94+
return this.headers[field];
95+
},
7596
};
7697

7798
const res = {
78-
setHeader: jest.fn(),
99+
set: jest.fn(),
100+
status(statusCode) {
101+
this.statusCode = statusCode;
102+
},
79103
};
80104

81105
const contentRes = handleRangeHeaders(context, content, req, res);
82106
expect(contentRes).toEqual('abcdef');
83107
expect(context.logger.error.mock.calls).toMatchSnapshot();
84108
expect(res.statusCode).toBeUndefined();
85-
expect(res.setHeader.mock.calls).toMatchSnapshot();
109+
expect(res.set.mock.calls).toMatchSnapshot();
86110
});
87111
});

0 commit comments

Comments
 (0)