Skip to content

Commit

Permalink
feat: add support for a default status code (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Jul 16, 2020
1 parent 4c5f869 commit b386769
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 18 additions & 3 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('#getStatusCode()', () => {
expect(getStatusCode(500)).toStrictEqual({ code: 500, message: 'Internal Server Error', success: false });
});

it('should support a default status code', () => {
expect(getStatusCode('default')).toStrictEqual({
code: '', // Since there's no HTTP status code that can really match up with this, `code` should be empty.
message: 'Default',
success: true,
});
});

it('should throw an error for an unknown status code', () => {
expect(() => {
return getStatusCode(1000);
Expand All @@ -33,9 +41,12 @@ describe('#getStatusCode()', () => {
});

describe('#isStatusCodeSuccessful()', () => {
it.each([['1XX'], [100], ['2XX'], [200], ['3XX'], [300]])('should return true for a %s status code', code => {
expect(isStatusCodeSuccessful(code)).toBe(true);
});
it.each([['default'], ['1XX'], [100], ['2XX'], [200], ['3XX'], [300]])(
'should return true for a %s status code',
code => {
expect(isStatusCodeSuccessful(code)).toBe(true);
}
);

it.each([['4XX'], [400], ['5XX'], [500]])('should return false for a %s status code', code => {
expect(isStatusCodeSuccessful(code)).toBe(false);
Expand All @@ -51,6 +62,10 @@ describe('#isStatusCodeValid()', () => {
expect(isStatusCodeValid(200)).toBe(true);
});

it('should return true for a default status code', () => {
expect(isStatusCodeValid('default')).toBe(true);
});

it('should return false for an invalid status code', () => {
expect(isStatusCodeValid(1000)).toBe(false);
});
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// https://github.com/nodejs/node/blob/master/lib/_http_server.js

const codes = {
default: ['Default', true],

'1XX': ['Informational', true],
100: ['Continue', true],
101: ['Switching Protocols', true],
Expand Down Expand Up @@ -111,7 +113,8 @@ function getStatusCode(code) {
}

return {
code,
// Since there's no HTTP status code that can really match up with `default`, code should just be empty.
code: code === 'default' ? '' : code,
message: codes[code][0],
success: codes[code][1],
};
Expand Down

0 comments on commit b386769

Please sign in to comment.