-
-
Notifications
You must be signed in to change notification settings - Fork 90
feat: add auth.format for formatting credentials
#65
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
+217
−7
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c2e430
feat: add `auth.format` for formatting credentials
Phillip9587 45220a1
Merge branch 'master' into format
blakeembrey 6d84129
Merge branch 'master' into format
blakeembrey 42a9f45
resolve review comments
Phillip9587 9994571
remove another missed eslint comment
Phillip9587 af1d90b
remove empty checks
Phillip9587 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { bench, describe } from 'vitest'; | ||
| import { format } from './index'; | ||
|
|
||
| describe('format', () => { | ||
| bench('format with simple credentials', () => { | ||
| const credentials = { name: 'user', pass: 'password' }; | ||
| format(credentials); | ||
| }); | ||
|
|
||
| bench('format with long credentials', () => { | ||
| const credentials = { | ||
| name: 'verylongusernameforbasicauth', | ||
| pass: 'verylongpasswordwithmanycharactersforbenchmark', | ||
| }; | ||
| format(credentials); | ||
| }); | ||
|
|
||
| bench('format with unicode credentials', () => { | ||
| const credentials = { name: 'jürgen', pass: 'pässwörd' }; | ||
| format(credentials); | ||
| }); | ||
|
|
||
| bench('format with special characters', () => { | ||
| const credentials = { name: 'user@domain', pass: 'p@ss!word#123' }; | ||
| format(credentials); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { describe, it, assert } from 'vitest'; | ||
| import { format } from './index'; | ||
|
|
||
| describe('format(credentials)', function () { | ||
| describe('arguments', function () { | ||
| describe('credentials', function () { | ||
| it('should be required', function () { | ||
| assert.throws( | ||
| () => (format as any)(), | ||
| /argument credentials is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should accept credentials', function () { | ||
| const header = format({ name: 'foo', pass: 'bar' }); | ||
| assert.strictEqual(header, 'Basic Zm9vOmJhcg=='); | ||
| }); | ||
|
|
||
| it('should reject null', function () { | ||
| assert.throws( | ||
| format.bind(null, null as any), | ||
| /argument credentials is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject a number', function () { | ||
| assert.throws( | ||
| format.bind(null, 42 as any), | ||
| /argument credentials is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject a string', function () { | ||
| assert.throws( | ||
| format.bind(null, '' as any), | ||
| /argument credentials is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject an object without name', function () { | ||
| assert.throws( | ||
| format.bind(null, { pass: 'bar' } as any), | ||
| /argument credentials is required to have name and pass properties/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject an object without pass', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 'foo' } as any), | ||
| /argument credentials is required to have name and pass properties/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject an object with non-string name', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 42, pass: 'bar' } as any), | ||
| /argument credentials is required to have name and pass properties/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject an object with non-string pass', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 'foo', pass: 42 } as any), | ||
| /argument credentials is required to have name and pass properties/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject userid containing colon', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 'foo:bar', pass: 'baz' }), | ||
| /must not contain a colon or control characters/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject control chars in userid', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 'foo\u0000bar', pass: 'baz' }), | ||
| /must not contain a colon or control characters/, | ||
| ); | ||
| }); | ||
|
|
||
| it('should reject control chars in password', function () { | ||
| assert.throws( | ||
| format.bind(null, { name: 'foo', pass: 'bar\u007f' }), | ||
| /must not contain control characters/, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with valid credentials', function () { | ||
| it('should return header', function () { | ||
| const header = format({ name: 'foo', pass: 'bar' }); | ||
| assert.strictEqual(header, 'Basic Zm9vOmJhcg=='); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with empty password', function () { | ||
| it('should throw', function () { | ||
| const header = format({ name: 'foo', pass: '' }); | ||
| assert.strictEqual(header, 'Basic Zm9vOg=='); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with empty userid', function () { | ||
| it('should throw', function () { | ||
| const header = format({ name: '', pass: 'pass' }); | ||
| assert.strictEqual(header, 'Basic OnBhc3M='); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with empty userid and pass', function () { | ||
| it('should throw', function () { | ||
| const header = format({ name: '', pass: '' }); | ||
| assert.strictEqual(header, 'Basic Og=='); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.