Skip to content

refactor: use express recommended api #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/utils/handleRangeHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import parseRange from 'range-parser';
export default function handleRangeHeaders(context, content, req, res) {
// assumes express API. For other servers, need to add logic to access
// alternative header APIs
res.setHeader('Accept-Ranges', 'bytes');
res.set('Accept-Ranges', 'bytes');

if (req.headers.range) {
const ranges = parseRange(content.length, req.headers.range);
const range = req.get('range');

if (range) {
const ranges = parseRange(content.length, range);

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

// Content-Range
// eslint-disable-next-line no-param-reassign
res.statusCode = 206;
res.setHeader(
res.status(206);
res.set(
'Content-Range',
`bytes ${ranges[0].start}-${ranges[0].end}/${length}`
);
Expand Down
40 changes: 32 additions & 8 deletions test/utils/handleRangeHeaders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ describe('handleRangeHeaders', () => {
headers: {
range: 'bytes=1-4',
},
get(field) {
return this.headers[field];
},
};

const res = {
setHeader: jest.fn(),
set: jest.fn(),
status(statusCode) {
this.statusCode = statusCode;
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual('bcde');
expect(res.statusCode).toEqual(206);
expect(res.setHeader.mock.calls).toMatchSnapshot();
expect(res.set.mock.calls).toMatchSnapshot();
});

it('should handle malformed range header', () => {
Expand All @@ -35,17 +41,23 @@ describe('handleRangeHeaders', () => {
headers: {
range: 'abc',
},
get(field) {
return this.headers[field];
},
};

const res = {
setHeader: jest.fn(),
set: jest.fn(),
status(statusCode) {
this.statusCode = statusCode;
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual('abcdef');
expect(context.logger.error.mock.calls).toMatchSnapshot();
expect(res.statusCode).toBeUndefined();
expect(res.setHeader.mock.calls).toMatchSnapshot();
expect(res.set.mock.calls).toMatchSnapshot();
});

it('should handle unsatisfiable range', () => {
Expand All @@ -54,16 +66,22 @@ describe('handleRangeHeaders', () => {
headers: {
range: 'bytes=10-20',
},
get(field) {
return this.headers[field];
},
};

const res = {
setHeader: jest.fn(),
set: jest.fn(),
status(statusCode) {
this.statusCode = statusCode;
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual('abcdef');
expect(res.statusCode).toEqual(416);
expect(res.setHeader.mock.calls).toMatchSnapshot();
expect(res.set.mock.calls).toMatchSnapshot();
});

it('should handle multiple ranges', () => {
Expand All @@ -72,16 +90,22 @@ describe('handleRangeHeaders', () => {
headers: {
range: 'bytes=1-2,4-5',
},
get(field) {
return this.headers[field];
},
};

const res = {
setHeader: jest.fn(),
set: jest.fn(),
status(statusCode) {
this.statusCode = statusCode;
},
};

const contentRes = handleRangeHeaders(context, content, req, res);
expect(contentRes).toEqual('abcdef');
expect(context.logger.error.mock.calls).toMatchSnapshot();
expect(res.statusCode).toBeUndefined();
expect(res.setHeader.mock.calls).toMatchSnapshot();
expect(res.set.mock.calls).toMatchSnapshot();
});
});