Skip to content

Commit 2db808b

Browse files
committed
Update error messages
1 parent 6e23775 commit 2db808b

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎src/format.spec.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ describe('format(credentials)', function () {
55
describe('arguments', function () {
66
describe('credentials', function () {
77
it('should be required', function () {
8-
assert.throws(() => (format as any)(), /credentials is required/);
8+
assert.throws(
9+
() => (format as any)(),
10+
/"credentials" must be an object/,
11+
);
912
});
1013

1114
it('should accept credentials', function () {
@@ -16,43 +19,49 @@ describe('format(credentials)', function () {
1619
it('should reject null', function () {
1720
assert.throws(
1821
format.bind(null, null as any),
19-
/credentials is required/,
22+
/"credentials" must be an object/,
2023
);
2124
});
2225

2326
it('should reject a number', function () {
24-
assert.throws(format.bind(null, 42 as any), /credentials is required/);
27+
assert.throws(
28+
format.bind(null, 42 as any),
29+
/"credentials" must be an object/,
30+
);
2531
});
2632

2733
it('should reject a string', function () {
28-
assert.throws(format.bind(null, '' as any), /credentials is required/);
34+
assert.throws(
35+
format.bind(null, '' as any),
36+
/"credentials" must be an object/,
37+
);
2938
});
3039

3140
it('should reject an object without name', function () {
3241
assert.throws(
3342
format.bind(null, { pass: 'bar' } as any),
34-
/credentials is required to have name and pass properties/,
43+
/"credentials" must have string properties "name" and "pass"/,
3544
);
3645
});
3746

3847
it('should reject an object without pass', function () {
3948
assert.throws(
4049
format.bind(null, { name: 'foo' } as any),
41-
/credentials is required to have name and pass properties/,
50+
/"credentials" must have string properties "name" and "pass"/,
4251
);
4352
});
4453

4554
it('should reject an object with non-string name', function () {
4655
assert.throws(
4756
format.bind(null, { name: 42, pass: 'bar' } as any),
48-
/credentials is required to have name and pass properties/,
57+
/"credentials" must have string properties "name" and "pass"/,
4958
);
5059
});
5160

5261
it('should reject an object with non-string pass', function () {
5362
assert.throws(
5463
format.bind(null, { name: 'foo', pass: 42 } as any),
55-
/credentials is required to have name and pass properties/,
64+
/"credentials" must have string properties "name" and "pass"/,
5665
);
5766
});
5867

@@ -87,21 +96,21 @@ describe('format(credentials)', function () {
8796
});
8897

8998
describe('with empty password', function () {
90-
it('should throw', function () {
99+
it('should return header', function () {
91100
const header = format({ name: 'foo', pass: '' });
92101
assert.strictEqual(header, 'Basic Zm9vOg==');
93102
});
94103
});
95104

96105
describe('with empty userid', function () {
97-
it('should throw', function () {
106+
it('should return header', function () {
98107
const header = format({ name: '', pass: 'pass' });
99108
assert.strictEqual(header, 'Basic OnBhc3M=');
100109
});
101110
});
102111

103112
describe('with empty userid and pass', function () {
104-
it('should throw', function () {
113+
it('should return header', function () {
105114
const header = format({ name: '', pass: '' });
106115
assert.strictEqual(header, 'Basic Og==');
107116
});

‎src/index.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,28 @@ export function parse(string: string): Credentials | undefined {
5353
*/
5454
export function format(credentials: Credentials): string {
5555
if (typeof credentials !== 'object' || credentials === null) {
56-
throw new TypeError('credentials is required to be an object');
56+
throw new TypeError('"credentials" must be an object');
5757
}
5858

5959
if (
6060
typeof credentials.name !== 'string' ||
6161
typeof credentials.pass !== 'string'
6262
) {
6363
throw new TypeError(
64-
'credentials is required to have name and pass properties',
64+
'"credentials" must have string properties "name" and "pass"',
6565
);
6666
}
6767

6868
// RFC 7617 disallows colon in username
6969
if (credentials.name.includes(':')) {
70-
throw new TypeError('name must not contain a colon');
70+
throw new TypeError('"name" must not contain a colon');
7171
}
7272

7373
const str = credentials.name + ':' + credentials.pass;
7474

7575
if (CONTROL_CHARS_REGEXP.test(str)) {
7676
throw new TypeError(
77-
'argument credentials must not contain control characters',
77+
'"name" and "pass" must not contain control characters',
7878
);
7979
}
8080

0 commit comments

Comments
 (0)